ListView的每个项目都有两个按钮。我想控制Activity控件。我应该在Activity中操作它,并且不能直接将监听器设置为按钮。
所以我设置了这段代码:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
// TODO Auto-generated method stub
}
});
如何知道单击哪个项目按钮?
答案 0 :(得分:0)
只需在xml-layout
中为这些按钮设置不同的ID@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
switch (view.getId) {
case R.id.btn1: ...
case R.id.btn2: ...
}
}
答案 1 :(得分:0)
你必须在适配器类中处理它。这将是最合适的方式所以制作你的自定义适配器,然后在你的getView方法中尝试这种方式:
public View getView(final int position, View convertView,ViewGroup parent)
{
if(convertView == null)
{
LayoutInflater inflater = getLayoutInflater();
convertView = (LinearLayout)inflater.inflate(R.layout.YOUR_LAYOUT, null);
}
Button Button1= (Button) convertView.findViewById(R.id.BUTTON1_ID);
Button Button2= (Button) convertView.findViewById(R.id.BUTTON2_ID);
Button1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// Your code that you want to execute on this button click
}
});
Button2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// Your code that you want to execute on this button click
}
});
return convertView ;
}