1

I want to pass the data from one activity to other activity listview.. here i have, First Activity-listview SecondActivity-some edittextboxes(when i click the first activity listview that id related details are fetched from database and set in the edittextboxes in second activity) ThirdActivity-one listview(when i click the button in second activity i want to move the current data only to ThirdActivity Listview). Here my problem is All the data in firstactivity listview is also displayed in third activity..can anyone help me to solve this...i am new in 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);

        }

Database:

    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 Answers2

2

I would just use the Bundle class that is send with the intent to retrieve the data that is passed. Keep in mind that it is legit to use for primitive types (string, boolean, int, double, float etc.) values. if you want to pass objects you could use persistent objects for example. Take a look at the following link for further reference: http://developer.android.com/guide/faq/framework.html#3

In your example, you could retrieve the bundled value using the following code: //sending activity:

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

//in the receiving activity:

Bundle bundle = this.getIntent().getExtras();  
String stringSend = bundle.getString("bla");
Bram
  • 4,533
  • 6
  • 29
  • 41
0

Use a class that handle all your necessary data variables, then make for all get/set methods. Instead using putExtra(that is not recommended) just set your value and in the second activity just use get.

Piyush
  • 18,895
  • 5
  • 32
  • 63
Lucian Novac
  • 1,255
  • 12
  • 18
  • i have a class that contains getters and setters. – user3400666 Mar 12 '14 at 07:32
  • Then you don't need to use putExtra. just use a singleton for your class and you will have all your data there. – Lucian Novac Mar 12 '14 at 07:36
  • what is the problem using the Bundle that is sended with the intent? It was the recommended way of passing primitive types a few years ago? Only for large objects other methods of data passing are necessary to my knowledge. http://stackoverflow.com/questions/11179936/android-send-data-from-activity-to-activity-using-bundle – Bram Mar 12 '14 at 07:37
  • am juz a beginner that why asking a help..don't know what to do clearly. – user3400666 Mar 12 '14 at 07:38
  • Becouse of Lifecycle app. Get/set is a standard java type to access your data in different contexts. – Lucian Novac Mar 12 '14 at 07:42