列表视图从列表视图移动到另一个活动时消失

时间:2014-05-14 15:14:20

标签: android android-listview android-activity

我面临一些奇怪的问题,当我从Activity 1移动到Activity 2列表视图时,活动1会自动消失再次查看列表视图我需要再次启动Activity 1

我将在活动1的列表视图中从button开始活动2

我不理解这种行为,我也不确定问题和解决方案。

请帮我解决此问题。

以下代码:

活动1:

public class stockmanager extends Activity{

    String getentry;
    private int storeID=0;
    //Database d;
    StockTable st;
    StockList sl;
    Database d;
    private String getstocks;
    public Cursor a1;
    Intent bd;
    Intent sd;
    String s,AllStocks;
    private String deletestock_sl,deletestock_st;
     TextView displaystocks;
     ListView popstocks;
     poplist populatestocks;
     int stockid;
     Cursor getid;

    static class ViewHolder {

        CheckBox cb;
        Button view1; 
    }


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stockmanager);

        d=new Database(getApplicationContext());
        st=new StockTable(getApplicationContext());
        sl=new StockList(getApplicationContext());


        new Dboperation().execute();


    }
    public class poplist extends CursorAdapter{


        public poplist(Context context, Cursor c) {
            super(context, c);
            // TODO Auto-generated constructor stub
        }


        @Override
        public void bindView(View view, Context context, Cursor c) {
            // TODO Auto-generated method stub

            final ViewHolder myviewholder=new ViewHolder();
            myviewholder.cb=(CheckBox) view.findViewById(R.id.checkBox1);
            myviewholder.view1=(Button) view.findViewById(R.id.button1); 


            myviewholder.cb.setText(c.getString(c.getColumnIndexOrThrow(sl.Column2)));


            myviewholder.cb.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    if(myviewholder.cb.isChecked())
                    {
                        a.add((String) myviewholder.cb.getText());

                    }

                }
            });

            myviewholder.view1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    sd=new Intent(v.getContext(),Detail.class);
                    sd.putExtra("StockName", myviewholder.cb.getText());

                    startActivity(sd);      //Activity 2 triggered here


                }
            });
        }

        @Override
        public View newView(Context context, Cursor c, ViewGroup parent) {
            // TODO Auto-generated method stub
            LayoutInflater inflater = LayoutInflater.from(context);

            View v = inflater.inflate(R.layout.stocklist, parent, false);
                    bindView(v, context, c);
                   return v;
//          return null;
        }


    }

private class Dboperation extends AsyncTask<Void, Void, Cursor>{

        ProgressDialog pd;
        @Override
        protected Cursor doInBackground(Void... params) {
            // TODO Auto-generated method stub

            a1=Database.getInstance(getApplicationContext()).getWritableDatabase().rawQuery(sl.getstocks, null);

            return a1;
        }


        protected void onPostExecute(Cursor result) {
            if(a1.moveToNext())
            {
                displaystocks.setVisibility(View.INVISIBLE);
            }
            else
            {
                displaystocks.setVisibility(View.VISIBLE);
            }
            //System.out.println("Entered onpostexecute");  
            final poplist populatestocks=new poplist(getApplicationContext(),a1) ;
            popstocks.setAdapter(populatestocks);

            if(pd!=null)
                pd.dismiss();

        }

        @Override
        protected void onPreExecute() {

            pd=new ProgressDialog(stockmanager.this);
            pd.setMessage("Please wait.... Loading Data");
            pd.show(); 
        }

        protected void onProgressUpdate(Void... values) {
        }


    }

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
super.onDestroy();
if(a1!=null){
    a1.close();
}
if(Database.getInstance(getApplicationContext())!=null){
    Database.getInstance(getApplicationContext()).close();
    }

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    if(a1!=null){
        a1.close();
    }
    if(Database.getInstance(getApplicationContext())!=null){
        Database.getInstance(getApplicationContext()).close();
        }
}
}

1 个答案:

答案 0 :(得分:0)

这是预期的行为。在这种情况下,Documentation for Activity可以帮助您。此链接应该告诉您需要了解的有关您为何看到此行为的所有信息。

虽然可以同时向用户显示两个活动,但我很少看到使用Fragments以外的任何其他活动,我不会推荐它。如果您想要的是在按钮上弹出一个显示新数据的窗口,那么您可能应该使用类似于切换其可见性的布局,或者可能是对话框。