gpt4 book ai didi

java - 从列表中检索数据

转载 作者:搜寻专家 更新时间:2023-11-01 08:40:17 24 4
gpt4 key购买 nike

抱歉,如果标题有误导性,我无法用语言表达。

Rotten Tomatoes

列表运行良好,看起来适合我正在做的事情。我打算做的是现在有一个带有概要等的详细信息页面。我考虑过传递传递的电影的 ID。

如果我点击一部电影,我如何在下一页中设置该电影的图像、文本等。基本上从所选电影中获取我需要的所有数据?

源代码可以在这里找到 - Github

屏幕截图(看起来很糟糕,但它只是在乱搞):

enter image description here

谢谢

最佳答案

我在我的项目中遇到了同样的问题,但你看起来很相似,所以我会给你我的解决方案以帮助你。

在我的例子中,我从数据库中检索股票,每只股票都有额外的 15 个价格,我想在每次点击股票时显示这些价格,因此请查看以下答案。

代码:

我用 String[] 创建了一个对象来帮助我检索每只股票的所有这 15 个价格,然后通过 Intent 传递它。

public class StockList {

private String stockCurrentName;
private String stockCurrentPrice;
private String stockImage;
private String[] restPrices;

public StockList(String stockCurrentName, String stockCurrentPrice, String stockImage, String[] restPrices) {
this.stockCurrentName = stockCurrentName;
this.stockCurrentPrice = stockCurrentPrice;
this.stockImage = stockImage;
this.restPrices = restPrices;
}

public String getStockCurrentName() {
return stockCurrentName;
}

public void setStockCurrentName(String stockCurrentName) {
this.stockCurrentName = stockCurrentName;
}

public String getStockCurrentPrice() {
return stockCurrentPrice;
}

public void setStockCurrentPrice(String stockCurrentPrice) {
this.stockCurrentPrice = stockCurrentPrice;
}

public String getStockImage() {
return stockImage;
}

public void setStockImage(String stockImage) {
this.stockImage = stockImage;
}

public String[] getRestPrices() {
return restPrices;
}

public void setRestPrices(String[] restPrices) {
this.restPrices = restPrices;
}
}

然后是我检索数据的方式:

public class JsonReadTask extends AsyncTask<String, Void, String> {
public JsonReadTask() {
super();
}

@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ListLoaderActivity.this);
pDialog.setTitle(R.string.waiting);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setMessage(getString(R.string.get_stocks));
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
pDialog.setInverseBackgroundForced(true);
pDialog.show();
}

@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
} catch (Exception e) {
Intent intent1 = new Intent(ListLoaderActivity.this,
RefreshActivity.class);
startActivity(intent1);
ListLoaderActivity.this.finish();
}
return null;
}

private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
} catch (Exception e) {
Intent intent1 = new Intent(ListLoaderActivity.this,
RefreshActivity.class);
startActivity(intent1);
ListLoaderActivity.this.finish();
}
return answer;
}

@Override
protected void onPostExecute(String result) {
ListDrawer();
pDialog.dismiss();
}
}// end async task

public void accessWebService() {
JsonReadTask task = new JsonReadTask();
task.execute(new String[]{url});
}

public void ListDrawer() {
customList = new ArrayList<StockList>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("metoxes");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
//for each stock i get its prices.
//In your List for each movie you can get its synopsis and anything else you need.
name = jsonChildNode.optString("name");
price = jsonChildNode.optString("price");
price1 = jsonChildNode.optString("price1");
price2 = jsonChildNode.optString("price2");
price3 = jsonChildNode.optString("price3");
price4 = jsonChildNode.optString("price4");
price5 = jsonChildNode.optString("price5");
price6 = jsonChildNode.optString("price6");
price7 = jsonChildNode.optString("price7");
price8 = jsonChildNode.optString("price8");
price9 = jsonChildNode.optString("price9");
price10 = jsonChildNode.optString("price10");
price11 = jsonChildNode.optString("price11");
price12 = jsonChildNode.optString("price12");
price13 = jsonChildNode.optString("price13");
price14 = jsonChildNode.optString("price14");
price15 = jsonChildNode.optString("price15");

image = jsonChildNode.optString("image");

justPrices = new String[]{price1, price2,
price3, price4, price5, price6, price7, price8, price9,
price10, price11, price12, price13, price14, price15};
loipesTimes = new String[]{"1st Day Value " + price1, "2nd Day Value " + price2, "3rd Day Value " + price3, "4th Day Value " + price4, "5th Day Value " + price5,
"6th Day Value " + price6, "7th Day Value " + price7, "8th Day Value " + price8, "9th Day Value " + price9,
"10th Day Value " + price10, "11th Day Value " + price11, "12th Day Value " + price12, "13th Day Value " + price13, "14th Day Value " + price14, "15th Day Value " + price15};
customList.add(new StockList(name, price, image, justPrices));

}
} catch (Exception e) {
Intent intent1 = new Intent(ListLoaderActivity.this,
RefreshActivity.class);
startActivity(intent1);
ListLoaderActivity.this.finish();
}

ArrayAdapter adapter = new MyStocksAdapter(ListLoaderActivity.this, R.layout.list_item, customList);
adapter.notifyDataSetChanged();
startList.setAdapter(adapter);
}

然后通过Intent

传递
private void registerCallClickBack() {
startList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
tv1 = (TextView) viewClicked.findViewById(R.id.stock_name);
tv2 = (TextView) viewClicked.findViewById(R.id.stock_price);
Intent intent = new Intent(ListLoaderActivity.this, StockItem.class);
intent.putExtra("name", tv1.getText().toString());
intent.putExtra("price", tv2.getText().toString());
intent.putExtra("stockInfo", customList.get(position).getRestPrices());
intent.putExtra("stockImage", customList.get(position).getStockImage());
startActivity(intent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
}
}
}

我猜你可以像这样使用它,这将帮助你完成它!!!希望我能帮到你!!!

关于java - 从列表中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33408607/

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