当我的 ListView 项目被选中时,无法突出显示它们
我有一个应用程序,它访问 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;
}
}
}
I have an app that hits a REST service, gets some json and then places it into a listview. My main activity extends Activity and not ListActivity. I can scroll through the list items, but I cannot select any (at least by touch). I get to the listview through findViewBYId and have an adapter that extends SimpleAdapter. I have set the convertView inside the adapter's getVIew() as clickable. However when I do this, I can no longer click on the list items in the listView? Did I do this wrong?
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能发生的情况是,您正在为 R.layout.list_item 中定义的行定义背景颜色,并且该背景颜色不是选择器中定义的颜色,例如:
在其中,您可以定义以下颜色:视图的每种状态,在本例中:聚焦和按下。
为了使用它,在 R.layout.list_item 中,您可以正常设置背景,例如
考虑到上述 xml 文件名为 list_row_background 且位于可绘制文件夹中
,并且您不应该将其设置为可点击,因为它已经可以点击了。
我希望能解决你的问题:D
干杯
Probably what can be happening is that you are defining a background color for the row, defined in R.layout.list_item, and that background color is not a color defined in a selector, for example:
In it, you can define the color for each state of the View, in this case: focused and pressed.
In order to use it, in R.layout.list_item, you set the background normaly, for example
considering that the xml file file described aboved is named list_row_background and its in the drawable folder
And you shouldn't set it as clickable, since it is already clickable.
I hope that solves your problem :D
Cheers
一种解决方案是将您的 Activity 更改为 ListActivity。
当用户触摸列表项时,将调用此覆盖:
如果您想更改它,则 v 是单元格。
该位置是适配器的索引。
One solution is to change your Activity to a ListActivity.
When the user touches a list item this override is called:
the v is the cell if you want to alter it.
The position is the index into adapter.