Can't get my ListView items to be highlighted when they are selected
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("ti开发者_StackOverflow社区tle"));
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;
}
}
}
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:
<?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>
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
android:background="@drawable/list_row_background"
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
One solution is to change your Activity to a ListActivity.
When the user touches a list item this override is called:
@Override protected void onListItemClick(ListView l, View v, int position, long id)
the v is the cell if you want to alter it.
The position is the index into adapter.
精彩评论