简体   繁体   English

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

[英]Pass data from one activity to another activity listview

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). 我想将数据从一个活动传递到另一个活动列表视图..在这里,我有第一个活动列表视图SecondActivity-一些edittextboxes(当我单击第一个活动列表视图时,从数据库中获取与ID相关的详细信息,并在第二个中的edittextboxes中进行设置活动)ThirdActivity-一个列表视图(当我单击第二个活动中的按钮时,我只想将当前数据移动到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.. 在这里我的问题是firstactivity listview中的所有数据也显示在第三个活动中..谁能帮我解决这个问题...我是android中的新手。

SecondActivity: 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: 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: 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);
    } }

I would just use the Bundle class that is send with the intent to retrieve the data that is passed. 我只使用意图发送的Bundle类来检索所传递的数据。 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 请看以下链接以获取更多参考: 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");

Use a class that handle all your necessary data variables, then make for all get/set methods. 使用一个处理所有必要数据变量的类,然后使用所有get / set方法。 Instead using putExtra(that is not recommended) just set your value and in the second activity just use get. 而是使用putExtra(不推荐使用)来设置您的值,而在第二个活动中只需使用get。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM