0

自 1 周以来,我一直遇到一个问题。我制作了一个自定义列表视图。单击按钮将项目添加到列表视图中。所有项目都正常显示在列表视图中。但是10秒后,当设备灯关闭并滑动屏幕时,里面的项目就ListView消失了。我在 xml 中的列表视图:

<ListView
    android:id="@+id/lvEditItem"
    android:layout_width="fill_parent"
    android:layout_height="100dip"
    android:overScrollMode="never"
    android:layout_below="@+id/tvSelectToEdit"
    android:cacheColorHint="@android:color/transparent"
    android:paddingRight="10dip"
    android:scrollbars="none" >
</ListView>

我的活动文件是:-

ListView mView;
mView=(ListView)findViewById(R.id.lvEditItem);
mView.setDivider(null);
mView.setOnItemClickListener(new OnItemClickListener() {

private View lastSelectedView = null;

public void clearSelection()
{
    if(lastSelectedView != null)
       lastSelectedView.setBackgroundDrawable(getResources().getDrawable(android.R.color.transparent));
}


@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
   clearSelection();
   lastSelectedView = view;
   view.setBackgroundResource(R.color.lightGreen);

   String items=arrayList.get(position).toString();
   String s[]=items.split("~");

   System.out.println("value of Array>>>>>>>=="+s.length);

   tvGender.setVisibility(View.VISIBLE);

   tvCategory.setVisibility(View.VISIBLE);

   tvSubCategory.setVisibility(View.VISIBLE);

   tvStyle.setVisibility(View.VISIBLE);

   tvSize.setVisibility(View.VISIBLE);

   tvColor.setVisibility(View.VISIBLE);

   tvGender.setText(s[0]);
   GenderData.gender=s[0];
   tvGender.setTypeface(face);

   tvCategory.setText(s[1]);
   CategoryData.category=s[1];
   tvCategory.setTypeface(face);

   tvSubCategory.setText(s[2]);
   SubCategoryData.subCategory=s[2];
   tvSubCategory.setTypeface(face);

   brand.setText(s[3]);

   tvStyle.setText(s[4]);
   StyleData.style=s[4];
   tvStyle.setTypeface(face);

   tvSize.setText(s[5]);
   SizeData.size=s[5];
   tvSize.setTypeface(face);

   tvColor.setText(s[6]);
   ColorData.color=s[6];
   tvColor.setTypeface(face);

   String as=s[7].replace("$", "");
   price.setText(as);

   originalRetailer.setText(s[8]);

   originalLink.setText(s[9]);
});

我的适配器类是:-

public class CustomEditListAdapter extends BaseAdapter{

private LayoutInflater mInflater;
ArrayList<String> mArrayList;
ArrayList<String> mshowItemsOnListView;
Typeface face;
public CustomEditListAdapter(Context context,ArrayList<String> mArrayList,ArrayList<String> mshowItemsOnListView ) {
    mInflater = LayoutInflater.from(context);
    this.mArrayList=mArrayList;
    this.mshowItemsOnListView=mshowItemsOnListView;
    face=Typeface.createFromAsset(context.getAssets(), "tahoma.ttf");
     System.out.println("Final ArrayList which has to be post>>>"+mArrayList.toString());
}

@Override
public int getCount() {
   return ConcatData.mSet.size();
}

@Override
public Object getItem(int arg0) {
   return mshowItemsOnListView.get(arg0);
}

@Override
public long getItemId(int position) {
   return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   ViewHolder holder;


   if (convertView == null) {
      convertView = mInflater.inflate(R.layout.custom_select_item_view, null);
      holder = new ViewHolder();
      holder.txtName = (TextView) convertView.findViewById(R.id.textView1);
      holder.txtName.setTypeface(face);

      convertView.setTag(holder);
   }
   else {
      holder = (ViewHolder) convertView.getTag();
   }

   // holder.txtName.setText(mArrayList.get(position));
   holder.txtName.setText(mshowItemsOnListView.get(position));
   return convertView;
}

static class ViewHolder {
   TextView txtName;
}
}

custom_select_item_view.xml 是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

 <ImageView 
         android:id="@+id/ivArrow"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/list_arrow_my_item_box"
         android:layout_alignParentLeft="true"
         android:layout_alignTop="@+id/textView1"
         android:padding="5dip"
         />

提前致谢!!

4

1 回答 1

0

这可能是由于活动生命周期,请参阅Android 活动生命周期 - 所有这些方法的用途是什么?了解更多信息。

简而言之,如果您的应用程序被挂起或暂停,当应用程序再次恢复时,其资源将被粉碎并重建。

在此过程中,您添加的项目列表将被销毁并重建。在重建添加的项目时,也需要(明确地)恢复!因此,请让他们将项目信息存储在“安全”位置,以便您能够使用此存储的信息重建列表项目。

有关如何执行此操作的更多信息,请查看有关保存持久状态的 Android 文档。

于 2012-12-20T11:50:14.557 回答