【问题标题】:Pass data from one activity to another activity listview将数据从一个活动传递到另一个活动列表视图
【发布时间】:2014-03-12 17:29:02
【问题描述】:

我想将数据从一个活动传递到另一个活动列表视图..在这里我有,第一个活动列表视图第二个活动-一些编辑文本框(当我单击第一个活动列表视图时,从数据库中获取与 id 相关的详细信息并在第二个活动中的edittextboxes)ThirdActivity-one listview(当我单击第二个活动中的按钮时,我只想将当前数据移动到ThirdActivity Listview)。这里我的问题是 firstactivity listview 中的所有数据也显示在第三个活动中..谁能帮我解决这个问题...我是 android 新手..

第二个活动:

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

第三个活动:

    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;

        }

第一个活动:

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

【问题讨论】:

  • 谁能帮帮我..我不知道该怎么办。
  • 在上面的代码中我应该做些什么改变。

标签: android


【解决方案1】:

我只会使用发送的 Bundle 类来检索传递的数据。请记住,用于原始类型(字符串、布尔值、int、double、float 等)值是合法的。例如,如果你想传递对象,你可以使用持久对象。请查看以下链接以供进一步参考: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");

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我有一个包含 getter 和 setter 的类。
    • 那么就不需要使用putExtra了。只需为您的课程使用单例,您将拥有所有数据。
    • 使用意图发送的 Bundle 有什么问题?这是几年前传递原始类型的推荐方式?据我所知,仅对于大型对象,其他数据传递方法是必要的。 stackoverflow.com/questions/11179936/…
    • 我只是一个初学者,为什么要寻求帮助..不知道该怎么做。
    • 因为 Lifecycle 应用程序。 Get/set 是一种标准的 java 类型,用于在不同的上下文中访问您的数据。
    猜你喜欢
    • 2012-07-05
    • 2013-08-14
    • 2013-07-17
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    相关资源
    最近更新 更多