我有一个ListView和Iwant它,当你点击列表中的一个项目时,它会带你到另一个活动,但是带有我点击的变量。
例如,ifIi有item1,item2,item3我想要它,当我点击item1时它会转到另一个活动,而在其他活动中,将显示json feed中item1内的所有内容。
到目前为止,这是我的代码:
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class ChooseTeamActivity extends ListActivity {
public String FullData = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.chooseact);
final String FullData = getIntent().getStringExtra("FullData");
Log.v("lc", "chooseActivity:" + FullData);
try{
JSONObject obj = new JSONObject(FullData);
List<String> leagues = new ArrayList<String>();
JSONObject objData = obj.getJSONObject("data");
JSONArray jArray = objData.getJSONArray("structure");
for (int i=0; i < jArray.length(); i++)
{ JSONObject oneObject = jArray.getJSONObject(i);
JSONArray DivisionsArray = oneObject.getJSONArray("divisions");
for (int d=0; d < DivisionsArray.length(); d++){
JSONObject DivDict = DivisionsArray.getJSONObject(d);
leagues.add(DivDict.getString("name"));
}
}
setListAdapter ( new ArrayAdapter<String>(this, R.layout.single_item, leagues));
ListView list = getListView();
list.setTextFilterEnabled(true);
list.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(),1000).show();
Intent nextScreen = new Intent(getApplicationContext(), ChooseTeamActivity.class);
nextScreen.putExtra("FullData", FullData);
startActivity(nextScreen);
}
});
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
保持对DivisionArray的字段引用,然后覆盖onListItemClicked以获取所单击项目的索引,然后在DivisionArray中查找该项目:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// TODO: Add code here to look up the item in DivisionArray by index, then use
// that to launch an activity either with the index or the JSON structure as an extra
}
获得索引后,可以使用索引或JSON结构启动活动:
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("selectedItem", /* TODO: use the index to the item or the JSON structure itself here */);
startActivity(intent);
答案 1 :(得分:0)
覆盖click事件处理程序:
@Override
protected void onListItemClick (ListView l, View v, int position, long id) {
}
并在那里添加代码以使用参数激活您的活动(例如,您的json内容或获取它的方法)
Intent intent = new Intent(this, DetailedActivity.class);
//leagues.get(position);
//or just use the position:
foo.putExtra("itemIndex", position);
//foo.putExtra("fullData", FullData); //or just the part you want
startActivity(foo);
答案 2 :(得分:0)
您可以使用setOnItemClickListener()
方法并使用意图传递要在列表中显示的数据字符串
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Bundle b = new Bundle();
b.putString("itemData",data[poition]);
Intent intent = new Intent(Activity1.this,Activity2.class);
intent.putExtras(b);
startActivity(intent);
}
});
这里data []是一个字符串数组,其中包含要从Activity1传递给Activity2的数据。活动1是当前活动,您也可以修复Activity2,只需用不同的数据填充它。
答案 3 :(得分:0)
使用此代码。
yourListView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, final int position, long arg3)
{
Intent intent = new Intent(YourActivity.this, NotesActivity.class);
intent.putExtra("Date", "Item To Pass In Ur Activity");
startActivity(intent);
}
});
在您的活动上写下以下代码,以获取列表视图通过的项目
String selectedDate = getIntent().getStringExtra("Date");