android - 重新加载后ListView消失了

标签 android listview arraylist

我有 3 个数组列表,我将它们组合起来显示在 ListView 中。当我单击以生成 ListView 时,它第一次运行良好,但是当我返回并再次单击该按钮时, ListView 什么也不显示。不确定是什么原因造成的。我检查了其他帖子,但找不到答案。我不太擅长 Arraylist,所以任何细节将不胜感激。

我在 Log cat 中也注意到了这条消息。不知道这是什么意思。

onVisibilityChanged() 被调用,可见性:0

public class Edit extends Activity implements OnItemClickListener {

private int pic;
 public String filename ="User Info";
  //Declaring SHareddPreference as userprofile
    static SharedPreferences userprofile;
    ListView listView;
    List<RowItem> rowItems;
//  String[] titles, descriptions;
    File imgpath=null;
    Context context=this;
    CustomListAdapter adapter;
    private List<String> Titles = new ArrayList<String>();
    private List<String> Actions = new ArrayList<String>();
    private List<Bitmap> Images = new ArrayList<Bitmap>();

int x;
    int y=1;
 int z=1;
   static int a=1;


      @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.aname);

            listView = (ListView)findViewById(R.id.listing);


            userprofile = getSharedPreferences(filename,0); 
            Intent pdf=getIntent();
            pic= userprofile.getInt("lastpic",pic);
                 x=pic;
                 Log.d("editpic",new Integer(pic).toString());


            while(y!=x){

                String comment = commentresult();
                Titles.add(comment);
                y++;
                Log.d("y",new Integer(y).toString());
                }

            while(z!=x){

                String act = actionresult();
                Actions.add(act);
                z++;
                Log.d("z",new Integer(z).toString());}

            while(a!=x){

                Bitmap photo = getbitmap();
                Images.add(photo);
                a++;
                Log.d("a",new Integer(a).toString());}




            Titles.toArray();
            Actions.toArray();
            Images.toArray();



        rowItems = new ArrayList<RowItem>();
        for (int i = 0; i < Images.size(); i++) {
            RowItem item = new RowItem(Images.get(i), Titles.get(i),Actions.get(i));
            rowItems.add(item);
        }

        Log.d("TAG", "listview null? " + (listView == null));
        CustomListAdapter adapter = new CustomListAdapter(this,
                R.layout.aname_list_item, rowItems);
        Log.d("TAG", "adapter=null? " + (adapter == null));




        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        listView.setOnItemClickListener(this);

    }


    public static Bitmap getbitmap() {

        String photo1 =userprofile.getString("picpath"+a, "");
        File imgpath=new File(photo1);

         ByteArrayOutputStream stream = new ByteArrayOutputStream();

            Bitmap bmp=DecodeImage.decodeFile(imgpath, 800, 1000, true);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
        Bitmap photo2=bmp;

    return photo2;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        Toast toast = Toast.makeText(getApplicationContext(),
            "Item " + (position + 1) + ": " + rowItems.get(position),
            Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
        toast.show();
    }






 public String commentresult()
 { 
//   String com2 = null;
//   while(y!=x){

    String comment=userprofile.getString("comment"+y, "");
    String com1=comment;
    String com2=com1;
//   }

 return com2;
 }

 public String actionresult()
 { 
// String act2 = null;
//   while(y!=x){
    String action=userprofile.getString("action"+z, "");
     String act1=action;
    String act2=act1;
//  }
     return act2;
 }



 private static final long delay = 2000L;
    private boolean mRecentlyBackPressed = false;
    private Handler mExitHandler = new Handler();
    private Runnable mExitRunnable = new Runnable() {

        @Override
        public void run() {
            mRecentlyBackPressed=false;   
        }
    };
    @Override
    public void onBackPressed() {

        //You may also add condition if (doubleBackToExitPressedOnce || fragmentManager.getBackStackEntryCount() != 0) // in case of Fragment-based add
        if (mRecentlyBackPressed) {
            mExitHandler.removeCallbacks(mExitRunnable);
            mExitHandler = null;
            super.onBackPressed();


        }
        else
        {
            mRecentlyBackPressed = true;
            Toast.makeText(this, "press again to exit", Toast.LENGTH_SHORT).show();
            mExitHandler.postDelayed(mExitRunnable, delay);
        }
    }

 @Override
  public void onDestroy() {
    // Destroy the AdView.

    super.onDestroy();
 }

自定义列表适配器:

 public class CustomListAdapter extends ArrayAdapter<RowItem> {

    Context context;
    List<RowItem> items;
    public CustomListAdapter(Context context, int resourceId,
        List<RowItem> items) {
    super(context, resourceId, items);
    this.context = context;
    this.items = items;
  }

 @Override
 public int getCount() {
    // TODO Auto-generated method stub
    return items.size();
}

  @Override
    public RowItem getItem(int position) {
        // TODO Auto-generated method stub
        return items.get(position);
    }

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

/*private view holder class*/
private class ViewHolder {
    ImageView imageView;
    TextView txtTitle;
    TextView txtDesc;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    RowItem rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.aname_list_item, null);

        holder = new ViewHolder();
        holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
        holder.txtTitle = (TextView) convertView.findViewById(R.id.rab);
        holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();


//  String name=items.get(position).getDesc();

    holder.txtDesc.setText(rowItem.getDesc());
    holder.txtTitle.setText(rowItem.getTitle());
    holder.imageView.setImageBitmap(rowItem.getImageId());
//   holder.imageView.setImageResource(Images.get(position) .getPlaceholderleft());

    return convertView;
}
 }

最佳答案

看起来这是因为您已经将变量 x、y、z 和 a 设置为全 static,这意味着该类的所有实例共享一个变量实例。因此,当您第二次调用 onCreate 时,您所有的 while 循环终止条件都已经满足,因此 while 循环永远不会执行。我不清楚为什么要将这些 static 设为静态,因此除非您需要它们,否则您应该删除这些变量的 static 关键字。

关于android - 重新加载后ListView消失了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25264774/

相关文章:

android - ListView 中的性能缓慢

java - 我想在对象数组的对象中打印一个方法,但不能。为什么?

java - 按每个汉堡的成本降序排序,这是一个带有 Collections.sort 的 ArrayList

java - Android,创建 .mp3 文件的数组列表?

android - 相对布局中缺少项目

android - android中Base64中的OutOfmemory异常

vb6 - 在运行时将项目添加到 ListView

android - 如何优化具有不同项目布局的 ListView

android - 在 Android 上分享时,为什么主题标签适用于浏览器,而不适用于 Twitter 应用程序本身?

Android 应用程序快捷方式不起作用。获取错误 "Failed to read the app data cannot start the app"