在onClick in listview中更改行内容视图

时间:2012-08-16 07:15:19

标签: android listview android-arrayadapter

我有一个listview,其中我使用了两种不同的布局(mycontentmtall或mycontenmotall)。我想在点击时更改这两个布局(再次点击一行中的一个对象,从我创建的两个新布局中选择一个(mycontentmtallexapand或mycontentmoallexpand))。

我遇到的问题是,当我点击listview中的某个项目时,我看不到布局的变化。

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

                 final ViewHolder holder;
                // int type = getItemViewType(position);
                final Message message = getItem(position);

                if (convertView == null) {

                    holder = new ViewHolder();
                    // if (type == TYPE_MT) {
                    if (!message.getIsMO()) {
                        convertView = mInflater.inflate(R.layout.mycontentmtall,
                                null);
                        holder.body = (TextView) convertView
                                .findViewById(R.id.bodyMT);
                        holder.date = (TextView) convertView
                                .findViewById(R.id.dateMT);
                        holder.from = (TextView) convertView
                                .findViewById(R.id.fromMT);
                        holder.status = (TextView) convertView
                                .findViewById(R.id.statusMT);
                    } else {
                        convertView = mInflater.inflate(R.layout.mycontentmoall,
                                null);
                        holder.body = (TextView) convertView
                                .findViewById(R.id.bodyMO);
                        holder.date = (TextView) convertView
                                .findViewById(R.id.dateMO);
                        holder.from = (TextView) convertView
                                .findViewById(R.id.fromMO);
                        holder.status = (TextView) convertView
                                .findViewById(R.id.statusMO);
                    }
                    convertView.setTag(holder);

                }

                else {

                    holder = (ViewHolder) convertView.getTag();
                }
            //  Boolean flag = false;


            //  final String body = message.getBody();
                convertView.setOnClickListener(new OnClickListener() {
                    private int pos = position;

                    @Override
                    public void onClick(View v) {

                        Toast.makeText(context, "Click-" + pos, Toast.LENGTH_SHORT)
                                .show();



                        View newConvertView = null;
                        if (!message.getIsMO()){
                        newConvertView = mInflater.inflate(R.layout.mycontentmtallexapand,
                                null);
                        holder.body = (TextView) newConvertView
                                .findViewById(R.id.bodyMTExpand);
                        holder.date = (TextView) newConvertView
                                .findViewById(R.id.dateMTExpand);
                        holder.from = (TextView) newConvertView
                                .findViewById(R.id.fromMTExpand);
                        holder.status = (TextView) newConvertView
                                .findViewById(R.id.statusMTExpand);


                        }
                        else
                        {
                            newConvertView = mInflater.inflate(R.layout.mycontentmoallexpand,
                                    null);
                            holder.body = (TextVi`enter code here`ew) newConvertView
                                    .findViewById(R.id.bodymoExpand);
                            holder.date = (TextView) newConvertView
                                    .findViewById(R.id.datemoExpand);
                            holder.from = (TextView) newConvertView
                                    .findViewById(R.id.frommoExpand);
                            holder.status = (TextView) newConvertView
                                    .findViewById(R.id.statusmoExpand);
                        }

    //                  newConvertView
    //                              .setLayoutParams(new LayoutParams(
    //                                      LinearLayout.LayoutParams.MATCH_PARENT,
    //                                      LinearLayout.LayoutParams.WRAP_CONTENT));
                        holder.body.setText(message.getBody());
                        holder.date.setText(message.getDate());
                        holder.from.setText(message.getPhoneNumber());
                        holder.status.setText(String.valueOf(message.getStatus()));
    //                  flag = true;

                    }
                });
//  s=body.substring(0, 20);
                holder.body.setText(message.getBody());
            holder.date.setText(message.getDate());
            holder.from.setText(message.getPhoneNumber());
            holder.status.setText(String.valueOf(message.getStatus()));

            return convertView;

        }

1 个答案:

答案 0 :(得分:1)

在convertView的onclick()中尝试使用convertView而不是创建新视图(newConvertview),首先删除convertView中的元素,然后在其上展开新的布局。

希望这对你有所帮助..