0

我浏览了各种链接,但无法解决列表视图中内容丢失的问题,

消息适配器.java

class MessageAdapter extends BaseAdapter {

     ViewHolder viewHolder;
     LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);

     public int getCount() {
            // TODO Auto-generated method stub
            return Constant_Variables.Sms_Status_list.size();
        }

     public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

     @Override
     public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }



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

         if (convertView == null) {
             convertView = inflater.inflate(R.layout.messagelist_row, null);

             viewHolder=new ViewHolder();

             viewHolder.sms_from_row=(TextView)convertView.findViewById(R.id.sms_from_txtview);
             viewHolder.sms_to_row=(TextView)convertView.findViewById(R.id.sms_sendto_txtview);
             convertView.setTag(viewHolder);

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

         if(Constant_Variables.Sms_Status_list.get(position).equalsIgnoreCase(Twilioresponse_Utils.sms_sent))
         {
             viewHolder.sms_from_row.setVisibility(View.GONE);
             viewHolder.sms_to_row.setText("Sent: "+Constant_Variables.Sms_from_list.get(position));

         }
         else if(Constant_Variables.Sms_Status_list.get(position).equalsIgnoreCase(Twilioresponse_Utils.sms_received))
         {
             viewHolder.sms_to_row.setVisibility(View.GONE);
             viewHolder.sms_from_row.setText("Received: "+Constant_Variables.Sms_to_list.get(position));
         }
         else
         {
             viewHolder.sms_from_row.setText("Message in queued.");
         }


         convertView.setTag(viewHolder);


         return convertView;
     }
 }


    class ViewHolder {
         TextView sms_from_row,sms_to_row;

     }

当我没有将可见性设置为消失时,列表数据不会丢失,但是当我将可见性设置为消失时,列表数据会丢失。

这是我用来显示文本的 XML 文件:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/sms_from_txtview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:text="From" />

    <TextView
        android:id="@+id/sms_sendto_txtview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:text="To" />
</LinearLayout>
4

2 回答 2

0

您还需要在其他部分设置View.VISIBLE

else
{
    viewHolder.sms_from_row.setText("Message in queued.");
    viewHolder.sms_to_row.setVisibility(View.VISIBLE);
}
于 2013-01-23T12:23:05.867 回答
0

你做

viewHolder.sms_from_row.setVisibility(View.GONE);
viewHolder.sms_to_row.setVisibility(View.GONE);

但你从来没有

viewHolder.sms_from_row.setVisibility(View.Visible);
viewHolder.sms_to_row.setVisibility(View.Visible);

更改您的代码:

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

// ADD THIS TWO LINES
viewHolder.sms_from_row.setVisibility(View.Visible);
viewHolder.sms_to_row.setVisibility(View.Visible);

     if(Constant_Variables.Sms_Status_list.get(position).equalsIgnoreCase(Twilioresponse_Utils.sms_sent))
     {
于 2013-01-23T12:25:16.850 回答