如何更新每个listview项中的按钮文本?(Android)

时间:2014-06-20 12:47:20

标签: android listview

我想创建一个listview,它的项目如下: 我不能发布图片,对不起。这是图像:

https://lh3.googleusercontent.com/-tQ0Uh8-YhkE/U6Qsz9Y0FAI/AAAAAAAADPc/dDerCtq_6jQ/w487-h865-no/Screenshot_2014-06-20-20-40-07.png

点击它下载文件的下载按钮,我想要做的是,当下载完成时,它想要将按钮的文本设置为“Use It”。 请帮忙,我不知道那样做。 这是我的适配器:

class SaveListAdapter extends BaseAdapter {
    private List<SaveModel> savesList;
    private Context context;

    public SaveListAdapter(Context context) {

        this.context = context;
    }

    public void addItem(SaveModel model) {
        if (null != savesList && !savesList.contains(model)) {
            this.savesList.add(savesList.size(), model);
        } else if (null == savesList) {
            this.savesList = new ArrayList<SaveModel>();
            this.savesList.add(model);
        }
    }

    public void addItems(List<SaveModel> modelList) {
        if (null != savesList) {
            Iterator<SaveModel> listIterator = modelList.iterator();
            while (listIterator.hasNext()) {
                SaveModel model = listIterator.next();
                addItem(model);
            }
        } else {
            this.savesList = modelList;
        }
    }

    @Override
    public int getCount() {
        if (null != savesList) {
            return savesList.size();
        } else
            return 0;
    }

    public void removeAllItems() {
        if (null != this.savesList) {
            this.savesList.clear();
        }
    }

    @Override
    public SaveModel getItem(int position) {
        if (null != savesList)
            return savesList.get(position);
        else
            return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        final SaveModel model = savesList.get(position);
        final ViewHolder viewHolder;
        if (null == convertView) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_card, null);
            viewHolder.gamePicIMG = (ImageView) convertView
                    .findViewById(R.id.item_card_save_pic_Image);
            viewHolder.saveTitleTV = (TextView) convertView
                    .findViewById(R.id.item_card_save_title_TV);
            viewHolder.saveDownloadBtn = (Button) convertView
                    .findViewById(R.id.item_card_save_download_Btn);
            viewHolder.saveDescTV = (TextView) convertView
                    .findViewById(R.id.item_card_save_desc_TV);
            viewHolder.saveAuthorTV = (TextView) convertView
                    .findViewById(R.id.item_card_save_author_TV);
            viewHolder.saveUpdateTimeTV = (TextView) convertView
                    .findViewById(R.id.item_card_save_update_time_TV);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (!TextUtils.isEmpty(model.getSaveTitle())) {
            viewHolder.saveTitleTV.setText(model.getSaveTitle());
        } else {
            viewHolder.saveTitleTV.setText("UnKonw");
        }
        viewHolder.saveDescTV.setText("Description:\n" + model.getSaveDesc());
        if (!TextUtils.isEmpty(model.getSaveAuthor())) {
            viewHolder.saveAuthorTV.setText(model.getSaveAuthor());
        } else {
            viewHolder.saveAuthorTV.setText("Someone");
        }

        viewHolder.saveUpdateTimeTV.setText("Sometime");

        viewHolder.saveDownloadBtn.setText("Download ");
        viewHolder.saveDownloadBtn
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showDialog("Download", "Downloading...");
                        Message msg = new Message();
                        msg.what=Constants.MSG_DOWNLOAD_COMPLETE;
                        handler.sendMessageDelayed(msg, 5000);
                    }
                });

        return convertView;
    }

    private class ViewHolder {
        private ImageView gamePicIMG;
        private TextView saveTitleTV;
        private Button saveDownloadBtn;
        private TextView saveDescTV;
        private TextView saveAuthorTV;
        private TextView saveUpdateTimeTV;

    }
}

我有一个Handler来处理下载完成时的消息。

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case Constants.MSG_DOWNLOAD_COMPLETE:
            if(null!=dialog){
                dialog.dismiss();
            }
            //TODO 怎么更新点击的按个按钮状态为“使用”   how could i set  "Use It" to the button which i clicked. 
            break;
        }
    }
};

我来自中国。对不起我的英语。请帮助,谢谢。

2 个答案:

答案 0 :(得分:0)

将下载的项目保留在List的某个位置,并在完成下载功能后,调用adapter.notifyDataSetChanged()重新加载适配器视图。在此,通过检查列表中的下载位置来设置按钮标题。

答案 1 :(得分:0)

SaveModel添加变量以存储此状态。下载完成后,更改列表项中的值并调用notifySetDataChanged()。在SaveListAdapter中,将按钮文本设置为此值的函数。