Android - 动态按钮列表的动作侦听器
我目前有一个列表视图,其中每一行都有一个“下载”按钮,但我似乎无法正确设置每个按钮的操作侦听器。我的代码当前将每个按钮设置为相同的操作。
List<MyListItemModel> myListModel = new ArrayList<MyListItemModel>();
....
MyListItemModel item = new MyListItemModel();
JSONObject e = catalogue.getJSONObject(i);
item.id = i;
item.key = e.getString("key");
bookKey = (e.getString("key"));
item.setTitle(e.getString("title"));
item.setDescription(e.getString("description"));
// change the button action to the right download address
item.listener = new OnClickListener(){
public void onClick (View v){
downloadBook(bookKey);
}
};
我还有一个 MyListItemModel 类,它保存每个图书项目和一个 MyListAdapter ,其 getView 方法具有以下代码
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
//convertView = renderer;
convertView = mInflater.inflate(R.layout.shelfrow, null);
}
MyListItemModel item = items.get(position);
TextView label = (TextView)convertView.findViewById(R.id.item_title);
label.setText(item.getTitle());
TextView label2 = (TextView)convertView.findViewById(R.id.item_subtitle);
label2.setText(item.getDescription());
Button button = (Button)convertView.findViewById(R.id.btn_download);
button.setOnClickListener(item.listener);
return convertView;
}
I currently have a listview where each row has a 'download' button but I cannot seem to be able to set the action listener for each button correctly. My code currently sets EVERY button to the same action..
List<MyListItemModel> myListModel = new ArrayList<MyListItemModel>();
....
MyListItemModel item = new MyListItemModel();
JSONObject e = catalogue.getJSONObject(i);
item.id = i;
item.key = e.getString("key");
bookKey = (e.getString("key"));
item.setTitle(e.getString("title"));
item.setDescription(e.getString("description"));
// change the button action to the right download address
item.listener = new OnClickListener(){
public void onClick (View v){
downloadBook(bookKey);
}
};
I also have a MyListItemModel class which holds each book item AND a MyListAdapter with the following code for its getView method
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
//convertView = renderer;
convertView = mInflater.inflate(R.layout.shelfrow, null);
}
MyListItemModel item = items.get(position);
TextView label = (TextView)convertView.findViewById(R.id.item_title);
label.setText(item.getTitle());
TextView label2 = (TextView)convertView.findViewById(R.id.item_subtitle);
label2.setText(item.getDescription());
Button button = (Button)convertView.findViewById(R.id.btn_download);
button.setOnClickListener(item.listener);
return convertView;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
基本上,您已经传递了对 bookKey 变量的引用,因此每次更改它时,每个 onClick 侦听器都会更改它。
http://www.javacoffeebreak.com/articles/toptenerrors.html 请参阅第 6 条
Try this:
Basically you've been passing a reference to the bookKey variable and so each time you change it it will change for each onClick Listener.
http://www.javacoffeebreak.com/articles/toptenerrors.html See number 6