从列表

时间:2015-10-29 07:52:13

标签: java android

如果标题有误导性,我很抱歉,我无法用文字表达。

Rotten Tomatoes

列表运行良好,看起来很好我正在做什么。我正在计划做的是现在有一个详细信息页面的概要等。我考虑传递传递的电影的ID。

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

源代码可在此处找到 - Github

屏幕截图(看起来很可怕,但它只是乱七八糟):

enter image description here

由于

5 个答案:

答案 0 :(得分:2)

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

在我的情况下,我是从数据库中检索股票,每个股票都有额外的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);
            }
        }
    }

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

答案 1 :(得分:1)

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

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

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

答案 2 :(得分:0)

你应该这样做:

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

        }
    }
});

答案 3 :(得分:0)

你可以使用意图将一个活动的数据传递到另一个活动,这完全是Moudiz所说的,然后像这样检索收到的数据

String value;

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

答案 4 :(得分:0)

如果您已粘贴代码,则可以更轻松地了解您的实际问题。但是给出了数据和json: 1.如果您在从Json检索数据时遇到问题,请点击以下链接: http://www.androidhive.info/2012/01/android-json-parsing-tutorial

  1. 如果传递给下一个Activity,则可以使用Intent:
  2. 完成
    String value= getIntent().getStringExtra("Key");
         Intent intent = new Intent(this, DataClassNameHere.class);
        intent.putExtra("key", value);
        startActivity(intent);