-3

I have some data in json format like this.

{
  "data": [
    {
      "name": "Online Earning",
      "id": "172970482777411"
    },
    {
      "name": "U Wanna Be My Friend",
      "id": "193618510697929"
    },
    {
      "name": "Photos That Will Take Your Breath Away",
      "id": "102241993216852"
    },
    {
      "name": "Dashing Mee",
      "id": "218061968291332"
    },
    {
      "name": "I want to be a good Muslim",
      "id": "144132975732372"
    },
    {
      "name": "Aik Dam Jhakaas",
      "id": "988150887884868"
    }
  ]
}

i want to create android listview which only show list of names from my given data. When i click the listview item it should give me name + id and move to next activity.
Please someone write the sample code for this problem.

Muhammad Asim
  • 379
  • 1
  • 4
  • 14
  • If you can extract the data from JSON then there should not be any difficulty. And do once try on writing codes on own, if still getting error then post the code with error or whatever problem you are facing – Swr7der Nov 19 '16 at 10:03
  • You can use recyclerView for your purpose. – Swr7der Nov 19 '16 at 10:05
  • i am a beginner in android programing i don't understand how to do this plz i want a short sample code. – Muhammad Asim Nov 19 '16 at 10:07
  • it will be helpful for many beginners if someone will answer it. – Muhammad Asim Nov 19 '16 at 10:12
  • First look for JSON parsing for Android and then display the parsed data as you want on the screen. No one here will share you code directly. You can solve the problem that reads the documents in this section http://stackoverflow.com/documentation/android/topics – emrekose26 Nov 19 '16 at 10:12
  • It will be really long code dude! I can direct you, else everything depends on you. Just try to learn new things and check the link given by @emrekose26 – Swr7der Nov 19 '16 at 10:14

3 Answers3

1

Firstly get your model class

public class Data
{
    private String id;

    private String name;

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }

    public String getName ()
    {
        return name;
    }

    public void setName (String name)
    {
        this.name = name;
    }
}

public class ListData
{
    private Data[] data;

    public Data[] getData ()
    {
        return data;
    }

    public void setData (Data[] data)
    {
        this.data = data;
    }
}

Now you can use gson to convert json to Java objects

ListData myObject = gson.fromJson(jsonData, ListData.class);

Now further you can create simple adapter class for listview. Here is the url : Custom Adapter for List View

Community
  • 1
  • 1
SAM
  • 418
  • 3
  • 12
1

You have to follow this sequence:

  1. Make a class according to the need of what you want to show when user clicks on item. Suppose you want to show a text and an image when user clicks on item (and item expands), then add text and image in the class using proper constructor, getter and setter method.

  2. Make an adapter that extends BaseExpandableListAdapter. The benefit of this adapter is, it has methods like getGroupCount,getChildCount (in your case child count would be 2; name and id), isExpanded(for the group view, which would be called when user would click on any item) etc.

  3. Make ExpandableListView in your class and make an adapter object of adapter created in 2, for setting the ExpandableListView adapter.

This Tutorial of ExpandableListView would help you.

Swr7der
  • 849
  • 9
  • 28
0

I finally found an easy way to solve this problem.

  1. Create an object class.
  2. create List for that object class
  3. Create a listview
  4. Add your objects to that arraylist
  5. Create arrayAdapter
  6. SetAdapter listview

That's it.

Here is my code.

public class PagesList extends Activity {
        ListView listView;
        public List<Person> persons;

        class Person {
            String name;
            String age;


            Person(String name, String age) {
                this.name = name;
                this.age = age;

            }

            @Override
            public String toString() {
                return this.name;
            }
        }

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


             listView = (ListView) findViewById(R.id.PagesList);
            persons = new ArrayList<>();

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView parent, final View view, int position, long id) {
                    Toast toast = Toast.makeText(getApplicationContext(), persons.get(position).age.toString(), Toast.LENGTH_LONG);
                    toast.show();
                            }


            });


            /* make the API call */
            new GraphRequest(
                    AccessToken.getCurrentAccessToken(),
                    "/me/accounts",
                    null,
                    HttpMethod.GET,
                    new GraphRequest.Callback() {
                        public void onCompleted(GraphResponse response) {
                /* handle the result */

                            try {
                                JSONArray data = response.getJSONObject().optJSONArray("data");
                                for (int i = 0; i < data.length(); i++) {
                                    JSONObject oneAlbum = data.getJSONObject(i);

                                    //get your values    

                                    persons.add(new Person(oneAlbum.getString("name"), oneAlbum.getString("id")));

                                }

                                ShowPageList();

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }


                        }
                    }
            ).executeAsync();


        }

        public void ShowPageList(){
            ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, persons);
            listView.setAdapter(adapter);

        }

    }
Muhammad Asim
  • 379
  • 1
  • 4
  • 14