0

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

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 单击然后打开下面的警报对话框 incode::

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

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

private void updateStatus(int index, int radioSelectValue) {

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


    }
4

2 回答 2

0

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

下面是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);
于 2013-05-10T07:48:02.787 回答
0

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

于 2013-05-10T09:43:56.800 回答