将数据从一个活动传递到另一个活动列表视图

时间:2014-03-12 07:06:06

标签: android

我想将数据从一个活动传递到其他活动列表视图..这里我有,First Activity-listview SecondActivity-一些edittextboxes(当我点击第一个活动listview时,从数据库中获取id相关详细信息并在第二个活动中的edittextbox)第三个活动 - 一个列表视图(当我单击第二个活动中的按钮时,我想将当前数据仅移动到ThirdActivity Listview)。这里我的问题是第一个活动列表视图中的所有数据也显示在第三个活动中..任何人都可以帮我解决这个问题...我是android的新手..

SecondActivity:

 protected void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            accountId = this.getIntent().getStringExtra("accountid");
            Log.v("one",accountId);
            db=new databaseone(getApplicationContext());
            Cursor cur=null;
            cur=db.viewdata(accountId);

            // cur = db.query(db.TABLE_NAME, null,
                //" id = ?", new String[] { accountId }, null, null, null);
            //startManagingCursor(accounts);
            if (cur.moveToFirst()) {

                // update view
                cheqgiven.setText(cur.getString(cur
                        .getColumnIndex("cheqgiven")));
                cheqamt.setText(cur.getString(cur
                        .getColumnIndex("cheqamt")));
                cheqdate.setText(cur.getString(cur
                        .getColumnIndex("cheqdate")));
                cheqno.setText(cur.getString(cur
                        .getColumnIndex("cheqno")));
                remarks.setText(cur.getString(cur
                        .getColumnIndex("remarks")));
                }
                //while(cur.moveToNext());{
            cur.close();    
            db.close();
            }
    public void returned(View v){

            Intent intent = new Intent(getApplicationContext(), Returned.class);

            intent.putExtra("in", accountId);

            startActivity(intent);
        }

ThirdActivity:

    public class Returned extends Activity {
        String chequeid;
        ListView lv;
        ArrayList<Map<String, String>> lv2; 
        databaseone db;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.returned);
            lv=(ListView) this.findViewById(R.id.listCheque2);
            lv2 = new ArrayList<Map<String, String>>();
        }
        @Override
        protected void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            chequeid = this.getIntent().getStringExtra("in");
            lv2.removeAll(lv2);
            db=new databaseone(getApplicationContext());
            Cursor c=null;

            //array=new ArrayList<String>();
            c=db.viewdata(chequeid);
            while (c.moveToNext()) {
                LinkedHashMap<String, String> tran = new LinkedHashMap<String, String>();
                String n=c.getString(c.getColumnIndex("id"));
                String p=c.getString(c.getColumnIndex("cheqgiven"));
                String a=c.getString(c.getColumnIndex("cheqamt"));
                tran.put("id", n);
                tran.put("cheqgiven", p);
                tran.put("cheqamt", a);
                lv2.add(tran);
            }

            c.close();
            db.close();

            SimpleAdapter adapter = new SimpleAdapter(this, lv2,
                    R.layout.chequelist, new String[] { "id",
                            "cheqgiven","cheqamt" }, new int[] { R.id.textAccNo, R.id.textName,R.id.textTransType});

            lv.setAdapter(adapter);

        }

数据库:

    public Cursor viewdata(String acct) {
            // TODO Auto-generated method stub
            sdb = this.getReadableDatabase();
            String where = (acct == null) ? "" : " where id = " + acct;
            Cursor c=sdb.rawQuery("select * from " + TABLE_NAME + where, null);
            return c;

        }

FirstActivity:

public class PendingForPayment extends Activity {
    //ListView lv;
    String accountId;
    ListView lv;
    //TextView tv;
    //String tv1;
    //String cheqgiven1,amount1;
    //ArrayList<String> array;
    ArrayList<Map<String, String>> lv2; 
    databaseone db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pending_for_payment);
        //accNo= this.getIntent().getStringExtra("accnum");
        //Name= this.getIntent().getStringExtra("name");
        //tv=(TextView)findViewById(R.id.textAccNo);
        lv=(ListView) this.findViewById(R.id.listCheque);

        lv2 = new ArrayList<Map<String, String>>();
        lv.setOnItemClickListener( new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View selectedView, int arg2,long arg3) {
                TextView  textAccountId = (TextView) selectedView.findViewById(R.id.textAccNo);
                //Log.d("Accounts", "Selected Account Id : " + textAccountId.getText().toString());
                Intent intent = new Intent(getApplicationContext(), Marking.class);
                //tv1=tv.getText().toString();
                intent.putExtra("accountid", lv2 .get(arg2).get("id"));
                //intent.putExtra("accountid", textAccountId.getText().toString());
                //Log.v("1", "entered");
                //intent.putExtra("accountid",textAccountId);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();

        lv2.removeAll(lv2);
        db=new databaseone(getApplicationContext());
        Cursor c=null;


        c=db.viewdata(accountId);


    while (c.moveToNext()) {
            LinkedHashMap<String, String> tran = new LinkedHashMap<String, String>();
            String n=c.getString(c.getColumnIndex("id"));
            String p=c.getString(c.getColumnIndex("cheqgiven"));
            String a=c.getString(c.getColumnIndex("cheqamt"));
            tran.put("id", n);
            tran.put("cheqgiven", p);
            tran.put("cheqamt", a);
            lv2.add(tran);
        }

        c.close();
        db.close();

        SimpleAdapter adapter = new SimpleAdapter(this, lv2,
                R.layout.chequelist, new String[] { "id",
                        "cheqgiven","cheqamt" }, new int[] { R.id.textAccNo, R.id.textName,R.id.textTransType});

        lv.setAdapter(adapter);
    } }

2 个答案:

答案 0 :(得分:1)

我只想使用发送的Bundle类来检索传递的数据。请记住,使用原始类型(字符串,布尔值,整数,双精度,浮点数等)值是合法的。如果你想传递对象,你可以使用持久对象。请查看以下链接以获取进一步参考:http://developer.android.com/guide/faq/framework.html#3

在您的示例中,您可以使用以下代码检索捆绑的值: //发送活动:

Bundle bundle = new Bundle();
bundle.putString("bla", "this string will be send");
Intent myIntent = new Intent(this, Returned.class);
myIntent.putExtras(bundle);
startActivity(myIntent);

//在接收活动中:

Bundle bundle = this.getIntent().getExtras();  
String stringSend = bundle.getString("bla");

答案 1 :(得分:0)

使用一个处理所有必要数据变量的类,然后使用所有get / set方法。相反,使用putExtra(不推荐)只需设置您的值,在第二个活动中只使用get。