Java 从列表中检索数据

Java 从列表中检索数据,java,android,Java,Android,抱歉,如果标题有误导性,我无法用语言表达 这个列表运行良好,对于我正在做的事情看起来也不错。我正在计划做的是现在有一个细节页面,里面有大纲等。我想传递传递的电影ID 如果我点击一部电影,我如何在下一页设置该电影的图像、文本等,基本上从所选电影中获取我需要的所有数据 源代码可在此处找到- 屏幕截图(看起来很糟糕,但只是胡闹): 谢谢如果您向我们展示您在代码中所做的事情,我们可以为您提供更多帮助,但要将数据从一个活动传递到另一个活动,您可以使用intent示例: String value= ge

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

这个列表运行良好,对于我正在做的事情看起来也不错。我正在计划做的是现在有一个细节页面,里面有大纲等。我想传递传递的电影ID

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

源代码可在此处找到-

屏幕截图(看起来很糟糕,但只是胡闹):


谢谢

如果您向我们展示您在代码中所做的事情,我们可以为您提供更多帮助,但要将数据从一个活动传递到另一个活动,您可以使用
intent
示例:

String value= getIntent().getStringExtra("keyName");

Intent intent = new Intent(this, RatingDescriptionSearchActivity.class);
intent.putExtra("keyName", value);
startActivity(intent);

你应该这样做:

listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                YourObject item = arraylist.get(arg2);
                TextView textView = (TextView) arg1.findViewById(R.id.textView);


                Intent intent = new Intent(ThisActivity.this, SecondActivity.class);        
                intent.putExtra("textview_value", textView.getText().toString());
                startActivity(intent);

        }
    }
});
listView.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
YourObject item=arraylist.get(arg2);
TextView TextView=(TextView)arg1.findViewById(R.id.TextView);
意向意向=新意向(ThisActivity.this,SecondActivity.class);
intent.putExtra(“textview_值”,textview.getText().toString());
星触觉(意向);
}
}
});

您可以使用intent将数据从一个活动传递到另一个活动,就像Moudiz所说的那样,然后像这样检索接收到的数据

String value;

Intent intent = getIntent();
    value = intent.getStringExtra("keyName"); 

如果您已经粘贴了代码,那么就更容易理解您的实际问题。但在给定数据和json的情况下: 1.如果您在从Json检索数据时遇到问题,请点击以下链接:

  • 如果传递到下一个活动,则将使用意图:

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

    在我的例子中,我从一个数据库中检索股票,每只股票都有额外的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传递。您可以尝试本教程。并将解析后的数据保存到以ID为键的map对象中,解析JSON并保存到hashmap中。单击任何项目时,使用其实例使用唯一id检索该内容,并通过intent@x10sion我不确定这是否是你要找的。我想您必须一次解析所有数据并将其存储在本地。前进到下一节时,加载+1索引处的数据。若并没有,那个么每次用户想要移动到下一部电影时都会动态读取索引。通过intent可以通过Parcelable接口传递数据。请查看编辑问题中的源代码。通过intent传递数据的问题是,列表中没有我需要的所有数据,还有其他我需要的数据,如actors等。最好使用Parcelable。这使我们的编码更简单。请核对我的答案
    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);
        }
    
    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);
                }
            }
        }