如何将unique_id的值传递给另一个要在ListView中使用的活动 [英] How to pass value of unique_id to another activity to be used in ListView

查看:64
本文介绍了如何将unique_id的值传递给另一个要在ListView中使用的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,在其中我努力将来自json的解析数据的唯一id的值传递给DetailActivity.java.我想使用DetailActivity.javaMainactivity.javaMainactivity.java的unique_id值作为URL的参数.我是新手,所以请帮帮我. 我把孔Mainactivity.javaDetailActivty.java

I have following code in which I am struggling to pass value of unique id from the parsed data from json to the DetailActivity.java . I want to use that value of unique_id from Mainactivity.javain DetailActivity.java as a parameter to the URL .I am a newbie so help me out on this. I am putting the hole Mainactivity.java and DetailActivty.java

public class MainActivity extends AppCompatActivity {

    private TextView text1;
    private ListView lisView;
    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create default options which will be used for every
        //  displayImage(...) call if no options will be passed to this method
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
        .cacheInMemory(true)
        .cacheOnDisk(true)
        .build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())

        .defaultDisplayImageOptions(defaultOptions).build();
        ImageLoader.getInstance().init(config); // Do it on Application start


        lisView = (ListView) findViewById(R.id.lvnews);
        new JSONTask().execute("http://cricapi.com/api/cricket");
        Button button = (Button)findViewById(R.id.btn);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new JSONTask().execute("http://cricapi.com/api/cricket");
            }
        });

    }


    public class JSONTask extends AsyncTask<String, String, List<NewsModel>> {



        @Override
        protected List<NewsModel> doInBackground(String... params) {
            HttpURLConnection connection = null;
            BufferedReader reader = null;
            try {
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();

                InputStream stream = connection.getInputStream();

                reader = new BufferedReader(new InputStreamReader(stream));
                StringBuffer buffer = new StringBuffer();
                String line = "";
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }
                String finalJson = buffer.toString();

                JSONObject parentObject = new JSONObject(finalJson);
                JSONArray parentArray = parentObject.getJSONArray("data");

                List<NewsModel> newsModelList = new ArrayList<>();

                for (int i = 0; i < parentArray.length(); i++) {
                    JSONObject finalObject = parentArray.getJSONObject(i);
                    NewsModel newsModel = new NewsModel();
                    newsModel.setHeadline(finalObject.getString("unique_id"));
                    newsModel.setAuthor(finalObject.getString("description")); //          newsModel.setImage(finalObject.getString("Image"));
                    newsModel.setDescription(finalObject.getString("title"));

                    newsModelList.add(newsModel);
                }
                return newsModelList;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


            return null;
        }

        @Override
        protected void onPostExecute(final List<NewsModel> result) {
            super.onPostExecute(result);
            if(result != null) {
                final NewsAdapter newsAdapter = new NewsAdapter(getApplicationContext(), R.layout.row, result);
                lisView.setAdapter(newsAdapter);
                lisView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(MainActivity.this, DetailActivity.class);
                        startActivity(intent);

                    }
                });
            }
        }
    }

    public class NewsAdapter extends ArrayAdapter{

        public List<NewsModel> newsModelList;
        private int resource;
        private LayoutInflater inflater;
        public NewsAdapter(Context context, int resource, List<NewsModel> objects) {
            super(context, resource, objects);
            newsModelList = objects;
            this.resource = resource;
            inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if(convertView == null){
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.row, null);
                holder.headline = (TextView)convertView.findViewById(R.id.headlineview);
                holder.author = (TextView)convertView.findViewById(R.id.authorview);
                //holder.image = (ImageView)convertView.findViewById(R.id.imageView);
                holder.description = (TextView)convertView.findViewById(R.id.descriptionview);

                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
                //holder.headline.setText(newsModelList.get(position).getHeadline());
                holder.author.setText(newsModelList.get(position).getAuthor());
                holder.description.setText(newsModelList.get(position).getDescription());



            return convertView;
        }

        private class ViewHolder { //            private ImageView image;
            private TextView headline;
            private TextView author;
            private TextView description;
        }
    }


}

现在进入DetailActivity,我想在其中使用unique_id作为URL的参数

Now to the DetailActivity in which I would like to use unique_id as a parameter to URL

public class DetailActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        // here I want to access that unique_id and pass it as id to url
        //Example  http://cricapi.com/api/cricketScore?unique_id=946765
    }
}

预先感谢

推荐答案

您可以直接使用(或)捆绑包,也可以通过意图本身将其发送.以下是Main Activty类的代码

You can use either bundle (or) directly you can send it through the intent itself. Below is the code for Main Activty class

    lisView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Intent intent = new Intent(MainActivity.this, DetailActivity.class);
                                //Passing the values to another activty using a unique key..
                                intent.putExtra("unique_id", newsModelList.get(position).getHeadline());
                                startActivity(intent);

                            }
                    });

详细信息活动现在借助相同的唯一键获取值.

Now in details activity get the values with the help of the same unique key.

Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            String text = bundle.getString("unique_id");
        }

希望这会有所帮助.

这篇关于如何将unique_id的值传递给另一个要在ListView中使用的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆