我有一个由api返回的json填充的列表视图。当我解析json时,我创建了一个新的java对象,它被添加到数组列表中。然后使用数组列表构建列表视图。我可以当前点击列表视图中的一个项目,并获得一个说明项目被点击的祝酒词。
我希望能够检索单个对象,因此我将对象传递给新实例,并在新实例上加载所有对象详细信息以显示给用户。我怎样才能像这样传递一个对象?
我的代码:
private class ReadJSONResult extends AsyncTask
<String, Void, String> {
protected String doInBackground(String... urls) {
return readJSONFeed(urls[0]);
}
protected void onPostExecute(String result) {
try {
System.out.println(result);
Log.d("asycTask", "[ Gets into asyc task ]");
///get
Log.d("search", "[ check if we get search results ]");
Log.d("search", "[" + result + "]");
JSONObject json = new JSONObject(result);
//acces listview
lv = (ListView) findViewById(android.R.id.list);
//make array list for beer
List<BeerData> beerList = new ArrayList<BeerData>();
//get json items
for(int i = 0; i < json.getJSONArray("data").length(); i++) {
String beerId = GetBeerDataFromJSON(i,"id", json);
String beerName = GetBeerDataFromJSON(i,"name", json);
String beerDescription = GetBeerDataFromJSON(i,"description" , json);
String beerAbv = GetBeerDataFromJSON(i,"abv" , json);
String beerIbu = GetBeerDataFromJSON(i,"ibu" , json);
String beerIcon = GetBeerIconsFromJSON(i, "icon",json );
String beerMediumIcon = GetBeerIconsFromJSON(i, "medium",json );
String beerGlass = GetBeerGlassFromJSON(i, json );
String beerStyle = GetBeerStyleFromJSON(i,"name", json );
String beerStyleDescription = GetBeerStyleFromJSON(i,"description", json );
String beerBreweryId = GetBeerBreweryInfoFromJSON(i, "id", json );
String beerBreweryName = GetBeerBreweryInfoFromJSON(i, "name", json );
String beerBreweryDescription = GetBeerBreweryInfoFromJSON(i, "description", json );
String beerBreweryWebsite = GetBeerBreweryInfoFromJSON(i, "website", json );
String beerBreweryYear = GetBeerBreweryInfoFromJSON(i, "established", json );
//create beer object
BeerData thisBeer = new BeerData(beerName, beerId, beerDescription, beerAbv, beerIbu, beerIcon,
beerMediumIcon, beerGlass, beerStyle, beerStyleDescription, beerBreweryId, beerBreweryName,
beerBreweryDescription, beerBreweryYear, beerBreweryWebsite);
//add beer to list
beerList.add(thisBeer);
}
//update listview
ArrayAdapter<BeerData> adapter1 = new ArrayAdapter<BeerData>(MainActivity.this, R.layout.list_item, R.id.itemName, beerList);
lv.setAdapter(adapter1);
//set up clicks
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//on item click do this
Toast.makeText(MainActivity.this, "click worked", Toast.LENGTH_SHORT).show();
}
});
} catch (Exception e) {
Log.d("ReadBeerDataTask", e.getLocalizedMessage());
}
}
}
答案 0 :(得分:4)
让您的BeerData
类实现Parcelable接口。然后就做:
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
i.putExtra("myBeerObject", lv.getItemAtPosition(arg2));
startActivity(i);
}
});
从第二节课开始:
Bundle b = getIntent().getExtras();
BeerData beer = b.getParcelable("myBeerObject");
这是如何正确实现接口的example。
答案 1 :(得分:0)
应该允许您执行此操作的简单步骤:
跟踪BeerData
集合。在您的情况下,这将是beerList
。
让BeerData
实施Parceable
。
(在OnItemClick
中使用正确的参数名称。它应该是onItemClick(AdapterView<?> parent, View view, int position, long id)
“
在onItemClick
中onItemClick(AdapterView<?> parent, View view, int position, long id) {
var beerData = beerList[position];
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("BEER!", beerData);
startActivity(intent);
}
在NextActivities onCreate
中:
{
Bundle extras = getIntent().getExtras();
BeerData beerData = (BeerData) extras.getParcelable("BEER!);
}