如何在textview中列出视图行更改android中的文本?

时间:2013-05-10 07:41:32

标签: android listview view

我在自定义行创建列表视图。但是我使用列表按钮单击打开警报对话框并选择单选按钮然后对话框被忽略而我调用更新功能但是如何更改行文本。我的代码如下。请给我解决方案。并保存状态所有行。

public class AlMessagesAdapter extends ArrayAdapter<DtoAllMessages> {

        private LayoutInflater inflator;
        private ArrayList<DtoAllMessages> userlist;

        public AlMessagesAdapter(Activity context,
                ArrayList<DtoAllMessages> list) {
            super(context, R.layout.custom_list, list);

            this.userlist = list;
            inflator = context.getLayoutInflater();
        }

        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {

            ViewHolder holder = null;
            if (convertView == null) {
                convertView = inflator.inflate(R.layout.custom_list, null);
                holder = new ViewHolder();
                holder.title = (TextView) convertView.findViewById(R.id.tvName);
                holder.date_cr = (TextView) convertView
                        .findViewById(R.id.tvDate);
                holder.img = (ImageView) convertView.findViewById(R.id.ivIcon);
                holder.tokenBtn = (TextView) convertView
                        .findViewById(R.id.tokenBtn);
                convertView.setTag(holder);
                convertView.setTag(R.id.tvName, holder.title);
                convertView.setTag(R.id.tvDate, holder.date_cr);
                convertView.setTag(R.id.ivIcon, holder.img);
                convertView.setTag(R.id.tokenBtn, holder.tokenBtn);

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

             token = userlist.get(position).getToken();


            token = token.substring(0, token.length() - 3);

            holder.title.setText(userlist.get(position).getName() + "(" + token
                    + ")");

            String type_data = userlist.get(position).getType().toString();

            if ((type_data.equals("text")) || (type_data.equals("photo"))) {
                holder.date_cr.setText(userlist.get(position).getType()
                        + ":Received "
                        + userlist.get(position).getCreated_date());
                holder.tokenBtn.setVisibility(View.VISIBLE);
                list.setItemsCanFocus(true);
            } else if (type_data.equals("out")) {
                holder.date_cr.setText(userlist.get(position).getType()
                        + ":Sent " + userlist.get(position).getCreated_date());
                holder.tokenBtn.setVisibility(View.GONE);
            }

            if (type_data.equals("text")) {
                holder.img.setBackgroundResource(R.drawable.chatmessage);

            } else if (type_data.equals("photo")) {
                holder.img.setBackgroundResource(R.drawable.photomessage);

            } else if (type_data.equals("out")) {

                holder.img.setBackgroundResource(R.drawable.outmessafe);
            }

            if (position % 2 == 0) {
                convertView.setBackgroundResource(R.drawable.whitebackground);
            } else {
                convertView.setBackgroundResource(R.drawable.greybackground);
            }

            holder.tokenBtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    AlertBoxShow(position);
                }
            });

            return convertView;
        }

        class ViewHolder {
            protected ImageView img;
            protected TextView date_cr;
            protected TextView title;
            protected TextView tokenBtn;
        }
    }

并且holder.tokenButton单击然后打开下面的警报对话框::

public void AlertBoxShow(int position) {
        final int pos=position;
        Log.v("log_tag"," position ::"+pos);
        final CharSequence[] items = { " 5 ", " 10 ", " 15 "};

        AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
        alt_bld.setSingleChoiceItems(items, -1,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,  int which) {

                        switch (which) {
                        case 0:
                            updateStatus(pos,5);
                            dialog.dismiss();
                            // Your code when first option seletced
                            break;
                        case 1:
                            updateStatus(pos,10);
                            // Your code when 2nd option seletced
                            dialog.dismiss();
                            break;
                        case 2:
                            updateStatus(pos,15);
                            dialog.dismiss();
                            // Your code when 3rd option seletced
                            break;
                        }

                    }

                });
        AlertDialog alert = alt_bld.create();
        alert.show();
    }

并调用updatestatus函数,我希望holder.title setText然后刷新列表行如何可能。我的更新状态函数在下面。

private void updateStatus(int index, int radioSelectValue) {

        holder.title.setText(userlist.get(index).getName() + "(" + token
                + ")");


    }

2 个答案:

答案 0 :(得分:0)

您可以使用setTag()getTag()来获取点击的行项目的位置,

以下是setTag()

convertView.setTag(R.id.tokenBtn, postion);

然后在getTag() holder.tokenBtn

onClick()
int pos = (Integer)v.getTag(R.id.tokenBtn);
TextView txtview = (TextView)v.getTag(R.id.tvName); // use this TextView 
                                                            to update the value.
AlertBoxShow(pos);

答案 1 :(得分:0)

据我了解您的问题,我认为您正在尝试根据用户在警告框中所做的选择来更新列表项的数据。如果是,则更改适配器中的基础数据。不要尝试在适配器外更新textview。只是尝试更新数据,然后在适配器上调用notifyDataSetChanged()方法。就是这样。