Android Listview Questions
I have the following class in my program:
public class RZoom extends Activity {
private ArrayList<FItem> m_FItems;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fchooser);
TextView tvName = (TextView)findViewById(R.id.txtTitleShow);
m_fItems = ((ArrayList<FItem>)this.getIntent().getSerializableExtra("fItems"));
tvName.setText(this.getIntent().getExtras().getString("RName"));
ListView lv = (ListView)findViewById(R.开发者_StackOverflowid.listView1);
FItemAdapter adapter = new FItemAdapter(this, R.layout.row, m_FItems);
lv.setAdapter(adapter);
}
}
This class displays a custom Listview and everything works fine. My questions are:
How do I capture an item click on the Listview? All the examples I've seen seem to inherit ListActivity instead of Activity, like I'm doing.
Is there a way to iterate through the items in the listview and set the background color of the item to Red, depending on the actual item? (In other words, I want to programmically highlight an item depending on the actual item)
Any help would be appreciated!
- lv.setOnItemClickListener()
- Override FItemAdapter's getView() to change colors depending on the item/position
For your first question you need to grab the ListView from its ID, as you are inheriting from Activity and not from ListActivity:
ListView lv_GetViewList = (ListView)findViewById(R.id.myListView); lv_GetViewList.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { // Your Code here } });
2 . You can override the getView() function and then grab the desired control of the row depending upon the conditions you want to use for a particular Control or all of the controls, say, a TextView. And you can set its background color:
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
TextView tv_Text = (TextView) v.findViewById(R.id.myRowTextView);
if(tv_Text != null)
{
tv_Text.setBackgroundColor(Color.RED);
}
return v;
}
精彩评论