我有一个应用程序可以访问 REST 服务,获取一些 json,然后将其放入列表视图中。我的主要活动扩展了 Activity 而不是 ListActivity。我可以滚动列表项,但我不能选择任何(至少通过触摸)。我通过 findViewBYId 进入列表视图,并拥有一个扩展 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;
}
}
}