androidjsonlistviewdynamiconselect

Android JSON input for setOnItemSelect


I am populating a ListView using remote JSON data in the following format:

{"nodes":[{"node":{"title":"Article#1","id":"4"}},{"node":{"title":"Article#2","id":"3"}}]}

My ListView is constructed with the following code:

ArrayList<String> articles = new ArrayList<String>();
    try{
                    for(int i=0; i < data.length(); i++){
                        JSONObject dataObj = (JSONObject)data.get(i);
                        JSONObject record = dataObj.getJSONObject("node");
                        title = (record.getString("title"));
                        nid = (record.getString("nid"));

                        Log.i("FOUND", "title: " + title);
                        Log.i("FOUND", "nid: " + nid);

                        articles.add(title);
                    }
                }catch(JSONException j){
                    Log.e("CHECK", "Attempting to read data returned from JSONReader: " + j.toString());
                }
    ListView articlesList = (ListView)findViewById(R.id.articlesList);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(ArticlesActivity.this, R.layout.article_item, R.id.articleItem, articles);
    articlesList.setAdapter(adapter);

The entire process works and successfully lists my Article titles. But, I am trying to follow tutorials which will help me enable onSelectListeners on each list item. The ID element associated to each article title is all I need to remotely fetch the article content.

Is it possible to setup my ArrayList to contain both title and id data and use it to setup my dynamic OnSelectListener enabled ListView?


Solution

  • The proper way to do it is to create a class that will hold the article name and id and whatever info you need. In your custom adapter you setTag() method when you create the view. Then in your onClickListener use getTag() method. Below I'll give you some code snipsetss hope they will help you.

    public class Article{
    private String name;
    private String id;
    
    public Article(String name, String id) {
        this.name =name;
        this.id = id;
    
    }
    
        public String getName() {
            return name;
        }
    
        public String getID() {
            return id;}
    }
    

    In you custom adapter class use setTag method when you create the view

           public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.group_list, null);
    TextView title = (TextView) v.findViewById(R.id.group_title);
                    ...//rest of my code 
    
                    Article article = getItem(position);
                    title.setText(article.getName());
                    title.setTag(article);
                    v.setTag(article);
                return v;
            }
    

    In your click listener

    list.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
    
                    Article article= (Article ) view.getTag();
                    String articleID= article.getID();
    }
    }