我使用listview
创建了自定义SimpleAdapter
,并且在列表视图的每一行中,我放了一个按钮具有单个ID。
我希望每行的position
都能通过按钮,但我每行都有一个按钮ID 我希望当我点击按钮时它找到的位置排并开始另一项活动
请帮帮我
public void click(View v){
//RelativeLayout navi = (RelativeLayout)findViewById(R.layout.custom_row_view);
TextView tv = (TextView)findViewById(R.id.text1);
ImageButton im = (ImageButton)findViewById(R.id.imageButton1);
ListView lv=(ListView)findViewById(android.R.id.list);
int position = 0;
Long id=Long.parseLong((String) adapter.getItem(position));
Intent i=null;
switch(position){
case 1:
i=new Intent(this, ButtonActivity.class);
startActivity(i);
break;
case 2:
i = new Intent(this, PickerActivity.class);
startActivity(i);
break;
}
答案 0 :(得分:0)
在列表视图中,您可以使用setOnItemClickListener
list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parentView, View childView, int position, long id) {
//Put Your code here
}
public void onNothingSelected(AdapterView parentView) {
}
});
答案 1 :(得分:0)
我不确定这一点,但让我们尝试一下,并告诉我你的成功......
行int position = listView.getPositionForView(v.getParent());
public void click(View v){
// This line is important
int position = listView.getPositionForView(v.getParent());
//RelativeLayout navi = (RelativeLayout)findViewById(R.layout.custom_row_view);
TextView tv = (TextView)findViewById(R.id.text1);
ImageButton im = (ImageButton)findViewById(R.id.imageButton1);
ListView lv=(ListView)findViewById(android.R.id.list);
int position = 0;
Long id=Long.parseLong((String) adapter.getItem(position));
Intent i=null;
switch(position){
case 1:
i=new Intent(this, ButtonActivity.class);
startActivity(i);
break;
case 2:
i = new Intent(this, PickerActivity.class);
startActivity(i);
break;
}
}
如果这不起作用,则只需选择自定义适配器,然后在适配器的onClick()
中将按钮的getView()
写入位置变量可访问。
答案 2 :(得分:0)
试试这个
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
final ProgressDialog progressBar = ProgressDialog.show(
VechiclesDetails.this, "", loadingString);
progressBar.setIndeterminate(true);
progressBar.setIndeterminateDrawable(getResources()
.getDrawable(R.anim.progressbar_handler));
new Thread(new Runnable() {
public void run() {
try {
Intent carDetail = new Intent(
Details.this,
Screen.class);
carDetail.putExtra("index", arg2);
carDetail.putExtra("sellerId", SellerId);
startActivity(carDetail);
progressBar.dismiss();
} catch (Exception e) {
progressBar.dismiss();
}
}
}).start();
}
});