Android - 动态按钮列表的动作侦听器

发布于 2024-11-09 05:08:15 字数 1376 浏览 0 评论 0原文

我目前有一个列表视图,其中每一行都有一个“下载”按钮,但我似乎无法正确设置每个按钮的操作侦听器。我的代码当前将每个按钮设置为相同的操作。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少钕鈤記 2024-11-16 05:08:15

试试这个:

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(new String(bookKey));
    } 

};

基本上,您已经传递了对 bookKey 变量的引用,因此每次更改它时,每个 onClick 侦听器都会更改它。

http://www.javacoffeebreak.com/articles/toptenerrors.html 请参阅第 6 条

Try this:

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(new String(bookKey));
    } 

};

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文