我已经浏览了各种链接,但我无法解决从列表视图中丢失内容的问题,
MessageAdapter.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>
答案 0 :(得分:0)
你需要在其他部分设置 View.VISIBLE
else
{
viewHolder.sms_from_row.setText("Message in queued.");
viewHolder.sms_to_row.setVisibility(View.VISIBLE);
}
答案 1 :(得分: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))
{