Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8253281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:49:34+00:00 2026-06-08T00:49:34+00:00

i am trying to get the items in the listview using json from server,

  • 0

i am trying to get the items in the listview using json from server, for this i have made my custom adapter. but if there are 6 items on the server it displays the last item 6 times in the list view and i have given a checkbox infront of every item in the list to get the id of the checked item. here is my code:

btnclub.setOnClickListener(new OnClickListener() {

        ArrayList<Integer> checkedList = new ArrayList<Integer>();

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Dialog d = new Dialog(TennerTextActivity.this);
            d.setContentView(R.layout.slctevnt);

            ListView list = (ListView) d.findViewById(R.id.list_mulitple);
            HashMap<String, String> list_map = new HashMap<String, String>();

            // Initialize CATEGORY_LIST HERE
            try {
                client = new DefaultHttpClient();
                HttpGet get = new HttpGet(
                        "http://dhklashfgsdhgsdg");
                HttpResponse rp = client.execute(get);
                String result = EntityUtils.toString(rp.getEntity());
                System.out.println("----------------------- result: "
                        + result);

                result = "{\"root\": " + result + "}";
                JSONObject root = new JSONObject(result);
                JSONArray sessions = root.getJSONArray("root");

                for (int i = 0; i < sessions.length(); i++) {
                    HashMap<String, String> map2 = new HashMap<String, String>();
                    JSONObject e = sessions.getJSONObject(i);
                    list_map.put("category_name", e.getString("name"));
                    list_map.put("category_id", e.getString("id"));
                    list_category.add(list_map);

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

            list.setAdapter(new MyAdapter());
            d.show();
            Button btndone = (Button) d.findViewById(R.id.button_multiple);
            btndone.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    for(int i = 0 ; i < checkedList.size() ; i++) {
                        clubs = "," + String.valueOf(checkedList.get(i));
                    }
                    clubs = clubs.substring(1);
                    System.out.print(clubs);
                    Log.e("club", clubs);
                }
            });

and my adapter is:

class MyAdapter extends BaseAdapter {



            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return list_category.size();
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public View getView(final int position, View convertView,
                    ViewGroup parent) {

                if (convertView == null) {
                    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                    convertView = inflater.inflate(R.layout.dialoglist,
                            null);
                    TextView item = (TextView) convertView
                            .findViewById(R.id.dialogitem);
                    item.setText(list_category.get(position).get(
                            "category_name"));


                }
                CheckBox check = (CheckBox)convertView.findViewById(R.id.checkBox_list);
                check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        // TODO Auto-generated method stub
                        if(isChecked) {
                            int temp = Integer.parseInt(list_category.get(position).get("category_id"));
                            checkedList.add(temp);
                            Object[] tempArray = checkedList.toArray();
                            Arrays.sort(tempArray);
                            checkedList.clear();
                            for(int i = 0 ; i < tempArray.length ; i++) {
                                checkedList.add((Integer) tempArray[i]);
                            }
                        }
                    }
                });

                return convertView;
            }

        }

please tell where i am doing wrong …

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:49:37+00:00Added an answer on June 8, 2026 at 12:49 am

    problem is in below line

                       list_map.put("category_name", e.getString("name"));
                        list_map.put("category_id", e.getString("id"));
                        list_category.add(list_map);
    

    you are putting category_name and id into list_map and adding same into list_category. here you are adding list_map instance in list_category. and becuse of the key for map(which is category_name and id ) is same,

    it can store only one value corresponding to one key, hence it is storing last inserting value of these keys.

    when you are updating list_map with new data the instance itself getting change), That is why it is showing 6 times of last list_map data.

    Solution : what you can do here, initialize list_map in loop only with new keyword, so it will insert new instance every-time you insert into list_category. or you can use two d array. instead of hashmap.

    HashMap<String, String> list_map = new HashMap<String, String>();
    

    P.S: I pointed out one problem in your code, which is causing duplicacy of data, there can be other problem also as suggested by other SO members.

    Edit:
    replace your below code

    for(int i = 0 ; i < checkedList.size() ; i++) {
                            clubs = "," + String.valueOf(checkedList.get(i));
                        }
                        clubs = clubs.substring(1);
    

    with something like below

       StringBuilder sb = new StringBuilder();
    for(int i = 0 ; i < checkedList.size() ; i++) {
                           sb.add(String.valueOf(checkedList.get(i))+",");
                        }
                     sb.deleteCharAt(sb.lastIndexOf(","));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sure that I'm missing something simple to get this implemented, but have run
Im trying to display this listview, but I keep getting a: 07-17 21:14:22.233: ERROR/AndroidRuntime(349):
I'm trying to make a listview from an arraylist using the tutorial i found
This is for a jQuery Mobile app using version 1.0.1. I'm trying to get
I am trying to create a custom ListView using an ArrayAdapter . POJO Item
I am trying to create a ListView using the following code: setListAdapter(new ArrayAdapter<String>(this, R.layout.settings_items,
I have a custom listview and I am using a custom listadapter to display
I've been trying to get the text of items in listview another process. I
I am trying to get this code (from a blog) working. The download file
I am using an array list, a listView, and a custom simple adapter in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.