Per Larsen
Per Larsen

Reputation: 23

How to use insert data from ArrayList<HashMap<String, String>> into a list view in Android Studio

I am working on a project that gets queries from a database on a remote server. The SQL side of it gets information from two separate tables, combines them and sends a JSON encoded string back me. I then parse the string and create an ArrayList with a HashMap of String, String to hold the data. All of this works just fine. There are 108 entries and all the related variables show this.

My problem arises when I try and put the final ArrayList HashMap into a listView. I have created a custom layout that I know works as I use to populate it from a set of arrays through the use of a dataprovider class and a custom adapter.

I followed a tutorial to create a custom adapter for the ArrayList HashMap (http://techlovejump.com/android-multicolum-listview/) . It appears to work except it will only pull 10-12 of the Items from the ArrayList HashMap, and then repeat. It does have exactly 108 items in the list. I notice that the first item in the list that is just off of the screen changes to different items as I scroll though the list.

I have debugged the code in the custom adapter class and everything is working fine the position starts at 0 and goes up. I have mostly seen it go to 10, once or twice 12. At this point on the next time through the getView method the position is returned to 0 and it starts over.

I am just learning all this and probably doing something wrong just don't know what. Here is the code I have to call the the custom adapter class and the class itself.

Custom Adapter this called from the method that parses my JSON string and builds the original ArrayList HashMap

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

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

/**
 * Created by jokerfwb on 11/12/15.
 */
public class UsersmyListAdapter extends BaseAdapter {

    private static final String TAG_NAME = "Name";
    private static final String AGE ="UserAge";
    private static final String CLASS = "Class";
    private static final String LEVEL = "Level";
    private static final String SKILL = "Skill";
    private static final String ACTIVITY = "Activity";
    private static final String TIME_STAMP = "TimeStamp";
    private static final String TAG_IN_GROUP = "InGroup";

    public ArrayList<HashMap<String, String>> myList;
    Activity activity;
    TextView name;
    TextView classType;
    TextView skill;
    TextView activityType;

    public UsersmyListAdapter(Activity activity, ArrayList<HashMap<String, String>> myList){
        super();
        this.activity = activity;
        this.myList = myList;
    }

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

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

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

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

        LayoutInflater inflater = activity.getLayoutInflater();

        if(convertView == null){
            convertView = inflater.inflate(R.layout.users_lfg_list_item,null);

            name = (TextView) convertView.findViewById(R.id.name);
            classType = (TextView) convertView.findViewById(R.id.classType);
            skill = (TextView) convertView.findViewById(R.id.skill);
            activityType = (TextView) convertView.findViewById(R.id.activityType);
        }

        HashMap<String, String> map = myList.get(position);
        name.setText(map.get(TAG_NAME));
        classType.setText(map.get(CLASS));
        skill.setText(map.get(SKILL));
        activityType.setText(map.get(ACTIVITY));

        return convertView;
    }

}

The call from my activity to the Custom Adapter this gets called at the end of the method that parses the JSON string pulled by my AsyncTask

UsersLFGListAdapter adapter = new UsersLFGListAdapter(this, UsersLFG);
listView.setAdapter(adapter);

Found a solution as well

The answer from Varzaru worked wonderfully. I also found another solution after much more googling. Just wanted to post it for anyone else that sees this. It is as follows

impleAdapter UserArrayList = new SimpleAdapter(UsersLFGListActivity.this, UsersLFG, R.layout.users_lfg_list_item, new String[] {TAG_DESTINY_USER_NAME, TAG_ACTIVE_CLASS, TAG_ACTIVE_CLASS_LIGHT, TAG_ACTIVITY_TYPE}, new int[]{R.id.name, R.id.Class, R.id.Light, R.id.activityType});
listView.setAdapter(UserArrayList);

Upvotes: 1

Views: 2252

Answers (1)

Iulian
Iulian

Reputation: 1156

Well without seeing your other class I can take a shot at this. First your Listview adapter is not good, try this one:

public class UsersmyListAdapter extends ArrayAdapter<HashMap<String, String>> {

    private static final String TAG_NAME = "Name";
    private static final String AGE ="UserAge";
    private static final String CLASS = "Class";
    private static final String LEVEL = "Level";
    private static final String SKILL = "Skill";
    private static final String ACTIVITY = "Activity";
    private static final String TIME_STAMP = "TimeStamp";
    private static final String TAG_IN_GROUP = "InGroup";

    public ArrayList<HashMap<String, String>> myList;
    Activity activity;

    public UsersmyListAdapter(Activity activity, ArrayList<HashMap<String, String>> myList){
        super(activity, R.layout.users_lfg_list_item, myList);
        this.activity = activity;
        this.myList = myList;
    }

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

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

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

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

        HashMap<String, String> map = myList.get(position);
        ItemViewHolder viewHolder;

        if(convertView == null){
            viewHolder = new ItemViewHolder();
            convertView = activity..getLayoutInflater().inflate(R.layout.users_lfg_list_item, null, true);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.classType = (TextView) convertView.findViewById(R.id.classType);
            viewHolder.skill = (TextView) convertView.findViewById(R.id.skill);
            viewHolder.activityType = (TextView) convertView.findViewById(R.id.activityType);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ItemViewHolder) convertView.getTag();
        }

        viewHolder.name.setText(map.get(TAG_NAME));
        viewHolder.classType.setText(map.get(CLASS));
        viewHolder.skill.setText(map.get(SKILL));
        viewHolder.activityType.setText(map.get(ACTIVITY));

        return convertView;
    }

    public class ItemViewHolder {
        TextView name;
        TextView classType;
        TextView skill;
        TextView activityType;
    }

}

Second you should never set the Listview adapter on your complete method of AsyncTask. You should set the adapter on create after you declare the listview and when the AsyncTask finishes then add the new items to the list and call:

adapter.notifyDataSetChanged();

Hope it helps with your issue!

Upvotes: 1

Related Questions