我浏览了各种链接,但无法解决列表视图中内容丢失的问题,
消息适配器.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>