gpt4 book ai didi

android - 如何处理 Button 单击和 Listview 单击单行?

转载 作者:行者123 更新时间:2023-11-29 15:31:54 24 4
gpt4 key购买 nike

我想点击 ListView 并在 onItemClickListener 监听器中执行一些操作,而且在每一行上我都有一个按钮来执行一些其他操作。我该怎么做?

最佳答案

你可以试试这个:

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]);
}
}
}

关于android - 如何处理 Button 单击和 Listview 单击单行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5726709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com