下面我有两个java类来创建一个customListView。我从某个人那里得到了代码,但是我并不真正了解她的代码。我想添加一些按钮,这些按钮会在您点击它们时带您进入新活动,并将"" + itemname [positionof]的描述更改为每个部分的不同(她将它们全部分组)所以我不能单独改变每个。)
我知道你可以做点什么
public void onLeaderboardClick(View view)
{
Intent intent = new Intent(MainActivity.this , Leaderboard.class);
startActivity(intent);
}
开始新的活动,但正如我上面所说的,我不确定如何将它们分开,所以每个人都可以有不同的onClickValue
public class ListMainActivity extends Activity {
ListView list;
String[] itemname ={
"Capture The Flag",
"Leaderboard",
"Log out"};
Integer[] imgId={
R.drawable.capturetheflag,
R.drawable.leaderboard,
R.drawable.logout,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_main);
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
String Selecteditem= itemname[+position];
Toast.makeText(getApplicationContext(), Selecteditem, Toast.LENGTH_SHORT).show();
}
});
}
}
和
public class CustomListAdapter extends ArrayAdapter<String>{
private final Activity context;
private final String[] itemname;
private final Integer[] imgid;
public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid) {
super(context, R.layout.mylist, itemname);
this.context=context;
this.itemname=itemname;
this.imgid=imgid;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.mylist, null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);
txtTitle.setText(itemname[position]);
imageView.setImageResource(imgid[position]);
extratxt.setText("Description of "+itemname[position]);
return rowView;
};
}
答案 0 :(得分:0)
您可以通过在自定义适配器
中指定单个侦听器,将单个侦听器添加到列表项中的每个元素How do i call onclick listener of a button which resides in listview item
查看此问题可能对您有所帮助
答案 1 :(得分:0)
我认为你应该在行layut布局中添加一个按钮。 然后在Adapter类的getView()方法中:
Button btnClick= (Button) rowView.findViewById(R.id.btnClick);
btnClick.setOnClickListener(@Override
public void onClick(View v) {
//todo something with each button
});
答案 2 :(得分:0)
对于任何回顾这个问题的人,我最终都做了一个转换声明
答案 3 :(得分:0)
0
int: 1
int: 2
7
答案 4 :(得分:0)
您将必须在类自定义适配器的getViewMethod中使用rowView.setOnClickListener()
,然后在其中可以根据视图的位置指定要进行的活动。