当设备处于唤醒状态时,列表项消失

时间:2012-12-20 11:39:12

标签: android android-listview android-lifecycle

自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"
         />

    

提前致谢!!

1 个答案:

答案 0 :(得分:0)

可能由于活动生命周期,请参阅Android activity life cycle - what are all these methods for?了解更多信息。

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

在此过程中,您的已添加项目列表将被销毁并重建。虽然重建添加的项目也需要(明确)恢复!因此,请将项目信息存储在“安全”状态。位置,以便您可以使用此存储的信息重建列表项。

有关如何操作的详细信息,请查看Saving Persistent State上的Android文档。