1

下面是我的代码和图像。我正在使用 arraylist 填充列表视图,runOnUIThread()其中onpostexecute() 具有从 doInBackground() 中的远程服务器获取的值。但问题是元素只有在焦点位于特定项目上时才可见。我一直在尝试用不同的东西来设置元素可见,但一切都是徒劳的。有人可以建议我如何让这些物品可见。注意:我无法扩展 ListActivity,因为我有另一个需要扩展的类,它是活动的子类。

runOnUiThread(new Runnable() {

     public void run() {
             //Updating parsed json data to Listview

            ListView listView = (ListView)findViewById(R.id.listsubcategory);


    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1, subCategoryList);
                        listView.setAdapter(arrayAdapter); 


                        listView.setOnItemClickListener(new OnItemClickListener() {

                                        @Override
                                        public void onItemClick(AdapterView<?> parent, View view,
                                                        int position, long id) {



                         String selectedSubcategory = subCategoryList.get(position);
                         Toast.makeText(getApplicationContext(), "You clicked on "+selectedSubcategory, Toast.LENGTH_SHORT).show();


                                        }
                                });


                 }
             });

在此处输入图像描述

4

1 回答 1

5

问题是列表项的样式。在正常状态下,您的项目具有白色背景和白色文本颜色,因此您看不到它们。当状态变为焦点时,颜色会发生变化。您可以通过使用自定义列表项而不是系统的android.R.layout.simple_list_item_1.

为项目定义一个布局,它可以是这样的:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/my_item_background"
    android:textColor="@color/my_text_color"
/>

现在,如果项目的布局是res/layout/my_list_item.xml,请以这种方式创建适配器:

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getContext(),
                                    R.layout.my_list_item, subCategoryList);
于 2012-12-26T14:28:10.233 回答