我想将值与包含多个值的每个列表视图行分别分配给另一个活动。然后在另一个活动中分别在editext中获取每个值:
我的代码是:
try
{
JSONArray jArray = new JSONArray(result);
final ArrayList <String>arrayListTitle = new ArrayList<String>();
for(int i=0;i<jArray.length();i++)
{
StringBuilder s = new StringBuilder();
JSONArray innerJsonArray = jArray.getJSONArray(i);
JSONObject json_data = innerJsonArray.getJSONObject(0);
at1=json_data.getString("title");
at= json_data.getString("mdate");
at2=json_data.getString("mtime");
at3=json_data.getString("venue");
at4=json_data.getString("organiser");
arrayListTitle.add(at1);
arrayListTitle.add(at);
arrayListTitle.add(at2);
arrayListTitle.add(at3);
s.append("\nTitle:"+at1);
s.append("\ndate:"+at);
s.append("\nTime:"+at2);
s.append("\nVenue:"+at3);
s.append("\nOrganiser:"+at4);
s.append("\n");
r.add(s.toString());
}
l.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, r));
l.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng)
{
String selectedFromList =(String) (l.getItemAtPosition(myItemInt));
// Starting new intent
intent1 = new Intent(RequestmeetActivity.this, ConfirmmeetActivity.class);
intent1.putExtra("title", arrayListTitle.get(myItemInt));
intent1.putExtra("mdate", arrayListTitle.get(myItemInt));
intent1.putExtra("mtime", arrayListTitle.get(myItemInt));
intent1.putExtra("venue", arrayListTitle.get(myItemInt));
startActivity(intent1);
}
});
}
我的活动如下:
所以我想从此列表视图行中分别获取值,并将这些值用于其他活动。我怎样才能做到这一点。请帮助我,我是android的新手。
在另一项活动
Bundle extras = getIntent().getExtras();
if(extras != null){
mtitle = extras.getString("title");
stime = extras.getString("mtime");
sdate = extras.getString("mdate");
cvenue = extras.getString("venue");
}
title.setText(mtitle);
cdate=(EditText)findViewById(R.id.editText2);
cdate.setText(sdate);
venue=(EditText)findViewById(R.id.editText4);
venue.setText(cvenue);
ctime=(EditText)findViewById(R.id.editText3);
ctime.setText(stime);
最佳答案
定义全局ArrayList arrayListTitle = new ArrayList;如您在r中定义的。并在for循环中附加代码之前在其中添加值。标题arrayListTitle.add(at1);
ArrayList<String> arrayListTitle = new ArrayList<String>;
for(int i=0;i<jArray.length();i++) {
StringBuilder s = new StringBuilder();
JSONArray innerJsonArray = jArray.getJSONArray(i);
JSONObject json_data = innerJsonArray.getJSONObject(0);
at1=json_data.getString("title");
at= json_data.getString("mdate");
at2=json_data.getString("mtime");
at3=json_data.getString("venue");
at4=json_data.getString("organiser");
arrayListTitle.add(at1); // check this line I have added value of title in arraylist you can take it same as data and time etc.
s.append("\nTitle:"+at1);
s.append("\ndate:"+at);
s.append("\nTime:"+at2);
s.append("\nVenue:"+at3);
s.append("\nOrganiser:"+at4);
s.append("\n");
r.add(s.toString());
}
对于listview项目,请单击:
l.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter,
View myView, int myItemInt, long mylng) {
String selectedFromList =(String) (l.getItemAtPosition(myItemInt));
// Starting new intent
intent1 = new Intent(RequestmeetActivity.this,
ConfirmmeetActivity.class);
intent1.putExtra("title",arrayListTitle.get(myItemInt)); // check this line get value from the arraylistTitle for the particular position.
intent1.putExtra("mdate",selectedFromList);
intent1.putExtra("uid",uid);
startActivity(intent1);
}
在另一个活动中获得标题时:编写如下代码:
String title;
Bundle extras = getIntent().getExtras();
if(extras != null){
title = extras.getString("title"); // same for others
}
editTxt.setText(title);