【问题标题】:Pass a Single ID from a for loop to another Activity in android?将单个 ID 从 for 循环传递到 android 中的另一个 Activity?
【发布时间】:2013-11-19 08:46:55
【问题描述】:

我有一排如图。该行在 for 循环中。所以现在根据响应增加行数。我有View 作为按钮和onclick 我想打开一个新活动,它将传递该索引的ID。我在String Array 中获得了ID。每一行我都有一个ID,现在我只想当用户点击任何视图时,相应的ID 应该被传递给新的活动。 Say Like FB SDK 有一个单独的 ID,Ford Escape 有一个单独的 ID,根据用户相应 ID 的点击,只会传递给另一个活动。 我在String array 中有 ID。 `

for(int i=0;i<jsonarray.length();i++){
                    JSONObject jsonObject= jsonarray.getJSONObject(i);

                    Name[i]=jsonObject.getString("Name");
                    Attachments[i]=jsonObject.getString("attachments");
                    ProjID[i]=jsonObject.getString("ProjectID");

                    View v = new View(getApplicationContext());
                    v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
                    v.setBackgroundColor(Color.GRAY);
                //  v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT));
                    row = new TableRow(getApplicationContext());
                    //v.setLayoutParams(new LayoutParams());            
                    t1 = new TextView(getApplicationContext());
                //  t2 = new TextView(getApplicationContext());
                    t3 = new TextView(getApplicationContext());
                b1= new Button(getApplicationContext());

                    t1.setText(Name[i]);
                    if(Name[i].contains("null")){
                        t1.setText("");
                    }
                    t1.setTextColor(Color.BLACK);
                    //t2.setText("View");
                    b1.setText("View");
                    b1.setBackgroundColor(Color.TRANSPARENT);
                //  t2.setTextColor(Color.BLACK);
                    t3.setText("View");
                    t3.setTextColor(Color.BLACK);



                    /*if(Attachments[i].contains("null")){
                        t3.setText("");
                    }*/

                    t1.setTextSize(15);
                    //t2.setTextSize(15);
                    b1.setTextSize(15);
                    t3.setTextSize(15);

                    t1.setGravity(Gravity.LEFT);
                //  t2.setGravity(Gravity.LEFT);
                    t3.setGravity(Gravity.CENTER);

                    t1.setWidth(50 * dip);
                    //t2.setWidth(50 * dip);
                    t3.setWidth(50 * dip);

                    t1.setPadding(20, 0, 0, 0);
                //  t2.setPadding(20, 0, 0, 0);
                    t3.setPadding(20, 0, 0, 0);

                    row.addView(t1);
                    row.setPadding(20, 0, 0, 20);
                //  row.addView(t2);
                    row.addView(b1);
                    row.addView(t3);

                    listd.addView(row, new TableLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT));
                    listd.addView(v);


            b1.setOnClickListener(new OnClickListener() {
                //String m_myData[];
                //int i=0;

                @Override
                public void onClick(View v) {
                    Log.e("IDS are:", ProjID[0]);

                    //Bundle bundel = new Bundle();
                 // for(int j=0;j<ProjID.length;j++){
                //      bundel.putStringArray("ID", ProjID);    

                //  }


                //  Intent intent =new Intent(List.this,peoplelist.class);
                    //bundel.putStringArray("IDs", ProjID);
                    //startActivity(intent);

                }
            });` 

【问题讨论】:

  • 行是否保存在 ListView 中?
  • 不,这些在表格布局中

标签: android arrays android-intent


【解决方案1】:

首先,您应该为此使用 ListView。它会让你的生活更轻松。 保持当前结构,你可以这样做:

b1.setTag(&lt;TheIdYouWantToPassLater&gt;);

在 OnClickListener 中:

 @Override
 public void onClick(View v) {

   String projId = v.getTag();
   Intent intent =new Intent(List.this,peoplelist.class);
   intent.putExtra("IDs", projId);
   startActivity(intent);
 }

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    相关资源
    最近更新 更多