0

In my app the user presses a button to gain points and they use that score to buy upgrades. In this list view the user buys the upgrades and I want to show the amount of upgrades they've bought.

final ArrayList<HashMap<String, String>>  authorList = new ArrayList<>();


    //Setup adapter
    customListViewAdapter = new CustomListViewAdapter(getApplicationContext(), authorList);
    listView.setAdapter(customListViewAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            firstoption = position;


        }
    });
    if (firstoption == 0) {
        nigg++;
        str = Integer.toString(nigg);
        bookPages[firstoption] = str;
    }
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {


                HashMap<String, String> data = new HashMap<>();
                data.put("title", bookTitles[i]);
                data.put("pages", bookPages[i]);
                data.put("author", authors[i]);


                authorList.add(data);
            }
            handler.postDelayed(this, 1000);
        }
    }, 1000);

This is the code I have right now, but for some reason it does not update. What am I doing wrong?

CustomListView class

package com.example.navjeevenmann.mytycoon;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.HashMap;


public class CustomListViewAdapter extends BaseAdapter {

private Context mContext;
private ArrayList<HashMap<String, String>> books;
private static LayoutInflater inflater = null;


public CustomListViewAdapter(Context context, ArrayList<HashMap<String, String>> data) {

    mContext = context;
    books = data;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}


@Override
public int getCount() {
    return books.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

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

    View view = convertView;

    if (convertView == null) {

        view = inflater.inflate(R.layout.program_list, null);

        TextView title = (TextView) view.findViewById(R.id.title);
        TextView author = (TextView) view.findViewById(R.id.textView);
        TextView pages = (TextView) view.findViewById(R.id.pages);
        ImageView image = (ImageView) view.findViewById(R.id.list_image);

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



        title.setText(mBook.get("title"));
        author.setText(mBook.get("author"));
        pages.setText(mBook.get("pages"));


    }


    return view;
}
}

5 Answers5

0

This is the issue:

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

This is always empty because you reinitialize mBook to empty every time getView is called, essentially resetting your data structure after every populate.

You already pass that data HashMap in you main class to your list as "books" list of HashMap.just add this after mBooks declaration

mBooks = books.get(position);

Also please read up on how listview/arrayadapter work here

It might also be issue that you are not populating your HashMap with data. Do some debugging here

Data.put("title", bookTitles[i]);
System.out.println(bookTitles[i]);

And let me know if you get any thing printed.

Bqin1
  • 467
  • 1
  • 9
  • 19
0

Can u pls change in getView() method of AdapterClass. End ur if condition before setting text on textview

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

    View view = convertView;

    if (convertView == null) {

        view = inflater.inflate(R.layout.program_list, null);

        TextView title = (TextView) view.findViewById(R.id.title);
        TextView author = (TextView) view.findViewById(R.id.textView);
        TextView pages = (TextView) view.findViewById(R.id.pages);
        ImageView image = (ImageView) view.findViewById(R.id.list_image);



}
        title.setText(mBook.get("title"));
        author.setText(mBook.get("author"));
        pages.setText(mBook.get("pages"));





    return view;
}
}

For how to send data to adapter just make changes in ur code like this...set adapter after setting values in arraylist

final ArrayList<HashMap<String, String>>  authorList = new ArrayList<>();


handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {


                HashMap<String, String> data = new HashMap<>();
                data.put("title", bookTitles[i]);
                data.put("pages", bookPages[i]);
                data.put("author", authors[i]);


                authorList.add(data);
            }
            handler.postDelayed(this, 1000);
        }
    }, 1000);

    //Setup adapter
    customListViewAdapter = new CustomListViewAdapter(getApplicationContext(), authorList);
    listView.setAdapter(customListViewAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            firstoption = position;


        }
    });
    if (firstoption == 0) {
        nigg++;
        str = Integer.toString(nigg);
        bookPages[firstoption] = str;
    }

Hope this will help u ...if not pls let me know

Lokesh Desai
  • 2,607
  • 16
  • 28
  • Hey man, are you sure this will work? By putting the text view setters outside of getView, it shouldn't even compile. Also it is not following how getView() is supposed to be used per android documentation. – Bqin1 Aug 13 '17 at 04:12
  • Not outside of getview() put outside of if condition and don't initialize hash map again in if condition...this should work – Lokesh Desai Aug 13 '17 at 04:13
  • @Bqin1 don't put outside getview() just put outside of if(convertview==null){} condition...and don't initialize ur hash map again in getView() – Lokesh Desai Aug 13 '17 at 04:23
0

do not use

View view = convertView;

because your convertView is null, do this

View view = inflater.inflate(R.layout.your_layout,parent,false);

and

HashMap<String, String> mBook = authorList.get(position);
text.setText(mBook.get("your_key");
Salman500
  • 1,213
  • 1
  • 17
  • 35
0

The Problem is you are adding data to authorList but not notify the adapter. So inside handler when add data just notify to the adapter so that adapter can update..

handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {


                    HashMap<String, String> data = new HashMap<>();
                    data.put("title", bookTitles[i]);
                    data.put("pages", bookPages[i]);
                    data.put("author", authors[i]);


                    authorList.add(data);
                    customListViewAdapter.notifyDataSetChanged();

                }
                handler.postDelayed(this, 1000);
            }
        }, 1000);

Hope it will work.

Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
0
  1. Change the getView code as below.
  2. Add ViewHolder for a better performance.

    public class CustomListViewAdapter extends BaseAdapter {
        private Context mContext;
        private ArrayList<HashMap<String, String>> books;
        private static LayoutInflater inflater = null;
        private ViewHolder mHolder;
    
       public CustomListViewAdapter(Context context, ArrayList<HashMap<String, String>> data) {
    
       mContext = context;
       books = data;
       inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
       }
    
    
      @Override
      public int getCount() {
        return books.size();
      }
    
      @Override
      public Object getItem(int position) {
         return position;
      }
    
      @Override
      public long getItemId(int position) {
        return position;
      }
    
      static class ViewHolder {
        TextView title;
        TextView author;
        TextView pages;
        ImageView image;
      }
    
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
    
        View view = convertView;
    
        if (convertView == null) {
    
            view = inflater.inflate(R.layout.program_list, null);
            mHolder = new ViewHolder();
            mHolder.title = (TextView) view.findViewById(R.id.title);
            mHolder.author = (TextView) view.findViewById(R.id.textView);
            mHolder.pages = (TextView) view.findViewById(R.id.pages);
            mHolder.image = (ImageView) view.findViewById(R.id.list_image);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        HashMap<String, String> mBook = books.get(position);
        title.setText(mBook.get("title"));
        author.setText(mBook.get("author"));
        pages.setText(mBook.get("pages"));
        return view;
     }
    }
    
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Harsh
  • 599
  • 3
  • 20