我想点击ListView
并在onItemClickListener
侦听器中执行某些操作,同样在每一行我都有一个按钮来执行其他操作。
我怎么能这样做?
答案 0 :(得分:4)
你可以试试这个:
public class buttonWithList extends ListActivity {
/** Called when the activity is first created. */
String[] items={"azhar","j"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new bsAdapter(this));
}
public class bsAdapter extends BaseAdapter
{
Activity cntx;
public bsAdapter(Activity context)
{
// TODO Auto-generated constructor stub
this.cntx=context;
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return items.length;
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return items[position];
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return items.length;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View row=null;
// TODO Auto-generated method stub
/*if(convertView==null)
{
}*/
LayoutInflater inflater=cntx.getLayoutInflater();
row=inflater.inflate(R.layout.row, null);
TextView tv=(TextView)row.findViewById(R.id.txtRow);
Button btn=(Button)row.findViewById(R.id.btnRow);
tv.setText(items[position]);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onListItemClick(this, this, position, null);
}
});
return row;
}
protected void onListItemClick(OnClickListener onClickListener,
OnClickListener onClickListener2, int position, Object object) {
// TODO Auto-generated method stub
//Toast.makeText(this, items[position], 3000);
System.out.println(items[position]);
}
}
}
答案 1 :(得分:1)
您需要实施ListAdapter
。这个对象让你定义每一行的元素。在成瘾中,您将使用ViewBinder来指定行的操作。因此,您将为每一行添加一个按钮,并使用ViewBinder
设置其onClickItemListener。