通过活动1时,在活动2中刷新列表视图

时间:2014-03-05 09:23:45

标签: android android-listview android-cursoradapter

在我的应用程序中有两个活动,在第二个活动中有listview。使用自定义游标适配器填充列表视图。

我的要求是当我从活动1转到第二个活动时,我希望第二个活动中的列表视图被填充。

我尝试过: 1.创建对象到第二个活动并尝试但没有工作也知道创建对象到活动类是错误的,因为活动类在内部被认为是线程。所以这样我就不会继续了。 2.从第二次活动的构造函数调用列表视图但是当我将参数作为构造函数和上下文传递时...我得到了例外

Note: I am unable to provide the logcat here

以下是代码:

主要活动:

package com.example.testdb;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Database d=new Database(this);

final Button A = (Button) findViewById(R.id.button1);

A.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                intent i=new intent(v.this,abc.class)
        startactivity(i);

            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

第二次激活:

public class abc extends Activity{


    String getentry;
    private int storeID=0;
    DatabaseObject d;
    StockTable st;
    private String getstocks;
    public Cursor a1;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stockmanager);
        d=new DatabaseObject(getApplicationContext());
        st=new StockTable(getApplicationContext());
    final Button AddStock=(Button) findViewById(R.id.button1);
         final EditText entry=(EditText) findViewById(R.id.editText1);
        final Button BroDetail=(Button) findViewById(R.id.button2);
        final ListView popstocks=(ListView) findViewById(R.id.listView1);

        AddStock.setOnClickListener(new OnClickListener() {

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

                getentry=entry.getText().toString();
                //st.insert(getentry);
                System.out.println(getentry);
                //d.db.rawQuery(st.select(), null);
                d.d.db.insert(st.tablename, null,st.insert(getentry));
                //populatelist populatestocks=new populatelist();

                getstocks="Select " + st.column1 + " as _ID, " + st.column2 + " From "+ st.tablename;
                 System.out.println(getstocks);
                a1=d.d.db.rawQuery(getstocks, null);
                if(a1.moveToNext()){

                    System.out.println(a1.getCount());  
                }
                else{
                    System.out.println("can't open database");
                }

                poplist populatestocks=new poplist(getApplicationContext(),a1) ;
                popstocks.setAdapter(populatestocks);

            }
        });
                }


    public class poplist extends CursorAdapter{

        public poplist(Context context, Cursor c) {
            super(context, c);
            // TODO Auto-generated constructor stub
        }
        //StockTable st1=new StockTable(getApplicationContext());
        //Database d1=new Database(getApplicationContext());


        @Override
        public void bindView(View view, Context context, Cursor c) {
            // TODO Auto-generated method stub
            final CheckBox cb=(CheckBox) view.findViewById(R.id.checkBox1);
            final Button view1=(Button) view.findViewById(R.id.button1); 

            if(c.moveToFirst()){

                //cb.setText(a1.getString(a1.getColumnIndex(st1.column2)));
                    //do{
                        //cb.setText(a1.getString(a1.getColumnIndexOrThrow(st.column2)));
                cb.setText(c.getString(1));
                //  }while (a1.moveToNext());

                }

        }

        @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;
        }


    }

    public void Declerations(){


    }


}

当我使用intent从第一个活动开始定向时,任何人都可以向我提供一些有关如何在第二个活动中刷新列表视图的信息。

1 个答案:

答案 0 :(得分:0)

您是否尝试使用Intent附加功能? 像这样:

Intent i = new Intent(this, abc.class);
i.putExtra("loadData", true);
i.putExtra("data", listData);
//add any data you need
...
startActivity(i);

然后在活动abc中检查onCreateonResume

中是否有传递的数据
Bundle data = getIntent().getExtras();
if(data != null) {
    //get the data
    boolean loadData = data.getBoolean("loadData");
    ...
}

修改
您需要在第二个活动的onCreate中填充您的列表 如果屏幕方向发生变化,使用onSaveInstanceStateonRestoreInstanceState保存/恢复数据也很有用。
检查一下:http://developer.android.com/training/basics/activity-lifecycle/recreating.html