选中它们时,无法使我的ListView项目突出显示

时间:2011-02-09 18:54:35

标签: android listview clickable

我有一个应用程序可以访问REST服务,获取一些json,然后将其放入listview。我的主要活动扩展了Activity而不是ListActivity。我可以滚动列表项,但我不能选择任何(至少通过触摸)。我通过findViewBYId访问listview,并有一个扩展SimpleAdapter的适配器。我已将适配器的getVIew()内的convertView设置为可点击。但是当我这样做时,我再也无法点击listView中的列表项了?我做错了吗?

public class App extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        jobsListView = (ListView) findViewById(R.id.jobs_list);
        jobsListView.setAdapter(new JobItemAdapter(jobsListView.getContext(), 
                jobsList, 
                R.layout.list_item, 
                new String[] {"title","location"},
                new int[] {R.id.job_title, R.id.job_location}));
        jobsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                Log.i("jobsListView.setOnItemClickListener()"," item clicked: " + arg0.getSelectedItemPosition());
            }

        });

    }

    public class JobItemAdapter extends SimpleAdapter {
        private List<HashMap<String,String>> jobsList;

        public JobItemAdapter(Context context, List<HashMap<String,String>> jobs, int resource, String[] from, int[] to)
        {   
            super(context, jobs, resource, from, to);
            jobsList = jobs;            
        }

        public View getView(int position, View convertView, ViewGroup parent){
            Log.i(TAG," in  JobItemAdapter getView() ");

            if (convertView == null) {
                //no previous views, inflate views
                convertView = getLayoutInflater().inflate(R.layout.list_item, parent, false);
            }

            //update textviews in layout
            TextView title = (TextView)convertView.findViewById(R.id.job_title);
            title.setText(jobsList.get(position).get("title"));

            TextView location = (TextView)convertView.findViewById(R.id.job_location);
            location.setText(jobsList.get(position).get("location"));
            //make the view clickable
            convertView.setClickable(true);
            return convertView;
        }

    }

}

2 个答案:

答案 0 :(得分:2)

可能发生的事情是,您正在为R.layout.list_item定义的行定义背景颜色,并且背景颜色不是选择器中定义的颜色,例如:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" > 
        <shape android:shape="rectangle"> 
            <solid
                android:color="@color/blue_button_press"/> 
        </shape> 
    </item> 
    <item android:state_selected="true" > 
        <shape android:shape="rectangle"> 
            <solid
                android:color="@color/blue_button_focus" /> 
        </shape> 
    </item> 
    <item android:state_focused="true" > 
        <shape android:shape="rectangle"> 
            <solid
                android:color="@color/blue_button_focus" /> 
        </shape> 
    </item> 
    <item> 
        <shape android:shape="rectangle" >
            <solid
                android:color="@color/white" /> 
        </shape> 
    </item> 
</selector>

在其中,您可以为视图的每个状态定义颜色,在这种情况下:聚焦和按下。 为了使用它,在R.layout.list_item中,您可以正常设置背景,例如

android:background="@drawable/list_row_background"

考虑到上面描述的xml文件文件名为list_row_background,它位于drawable文件夹中

并且您不应将其设置为可点击,因为它已经可以点击。 我希望能解决你的问题:D 干杯

答案 1 :(得分:0)

一种解决方案是将您的活动更改为ListActivity。

当用户触摸列表项时,会调用此覆盖:

  

@Override protected void   onListItemClick(ListView l,View v,   int position,long id)

如果您想改变它,则v是单元格 该位置是适配器的索引。