简体   繁体   English

如何在TextView中的ListView行更改Android中的文本?

[英]How to listview row in textview change text in android?

i develop listview in custom row created.but i used list button click open the alert dialog box and selected radio button then dialog is dismiss and i call update function but how to change row text .my code is below.please give me solution.and saved state all row. 我在创建的自定义行中开发了listview。但是我使用列表按钮单击打开警报对话框并选择单选按钮,然后关闭对话框并调用更新功能,但是如何更改行文本。我的代码在下面。请给我解决方案。保存状态全行。

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;
        }
    }

and holder.tokenButton click then open the alertdialog box incode below:: 和holder.tokenButton单击,然后打开以下代码中的alertdialog框:

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

and call updatestatus function in i want to holder.title setText and then refresh list row how to possible.and my update status function in below. 并在我想要holder.title setText中调用updatestatus功能,然后如何刷新列表行。以及下面的我的更新状态功能。

private void updateStatus(int index, int radioSelectValue) {

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


    }

You can use setTag() and getTag() to get the position of row item clicked, 您可以使用setTag()getTag()获取被点击的行项目的位置,

Below is to setTag() 下面是setTag()

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

And then getTag() inside holder.tokenBtn onClick() 然后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);

As far as i understand your question, i think you are trying to update list item's data according to the selection made by the user in alert box. 据我了解您的问题,我认为您正在尝试根据用户在警报框中所做的选择来更新列表项的数据。 if so, then change the underlying data in adapter. 如果是这样,则更改适配器中的基础数据。 Do not try to update your textview outside of the adapter. 不要尝试在适配器之外更新textview。 just try to update the data and then call notifyDataSetChanged() method on an adapter. 只需尝试更新数据,然后在适配器上调用notifyDataSetChanged()方法即可。 thats it. 而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM