我有一个 ListView 并且我想要它,这样,当您单击列表中的一个项目时,它会将您带到另一个活动,但带有我单击的变量。
例如,如果我有 item1、item2、item3 我想要它,这样,当我单击 item1 时,它会将我带到另一个活动,并且在该其他活动中,json 提要中 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();
}
}
}