gpt4 book ai didi

java - 在 ListView 单击时将 java 对象传递给新的 android 实例

转载 作者:行者123 更新时间:2023-11-29 03:32:52 24 4
gpt4 key购买 nike

我有一个 ListView ,由从 api 返回的 json 填充。当我解析 json 时,我创建了一个新的 java 对象,该对象被添加到数组列表中。然后使用数组列表来构建 ListView 。我现在可以点击 ListView 中的一个项目,然后 toast 说一个项目被点击了。

我希望能够检索单个对象,因此我可以将对象传递给新实例并将所有对象详细信息加载到新实例上以显示给用户。我怎样才能像这样传递一个对象?

我的代码:

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());
}
}
}

最佳答案

让您的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如何正确实现接口(interface)。

关于java - 在 ListView 单击时将 java 对象传递给新的 android 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17167085/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com