如何使用ArrayList中的插入数据<hashmap <string,string =“”>&gt;进入Android Studio

时间:2015-11-13 21:19:29

标签: android listview arraylist hashmap

我正在开发一个从远程服务器上的数据库获取查询的项目。它的SQL端从两个单独的表中获取信息,将它们组合起来并将一个JSON编码的字符串发回给我。然后我解析字符串并创建一个带有HashMap String,String的ArrayList来保存数据。所有这一切都很好。共有108个条目,所有相关变量都显示了这一点。

当我尝试将最终的ArrayList HashMap放入listView时,我的问题出现了。我创建了一个自定义布局,我知道它可以用来通过使用dataprovider类和自定义适配器从一组数组中填充它。

我按照教程为ArrayList HashMap(http://techlovejump.com/android-multicolum-listview/)创建了一个自定义适配器。它似乎工作,除了它只从ArrayList HashMap中拉出10-12个项目,然后重复。它确实在列表中有108个项目。我注意到,当我滚动列表时,列表中刚出现在屏幕外的第一项会更改为不同的项目。

我调试了自定义适配器类中的代码,一切正常,位置从0开始上升。我大多看到它转到10,一次或两次12.在下一次通过getView方法时,位置返回到0并重新开始。

我只是在学习这一切,可能做错了什么,只是不知道是什么。这是我必须调用自定义适配器类和类本身的代码。

自定义适配器,它从解析我的JSON字符串并构建原始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;
    }

}

从我的活动到自定义适配器的调用将在解析由我的AsyncTask拉取的JSON字符串的方法结束时调用

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

也找到了解决方案

Varzaru的答案非常出色。我在google搜索后也发现了另一种解决方案。只是想发布给其他看到这个的人。如下

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);

1 个答案:

答案 0 :(得分:1)

没有看到你的其他课程,我可以为此拍摄。首先你的Listview适配器不好,试试这个:

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;
    }

}

其次,您不应该在AsyncTask的完整方法上设置Listview适配器。在声明列表视图后,应该在创建上设置适配器,当AsyncTask完成后,将新项目添加到列表中并调用:

adapter.notifyDataSetChanged();

希望它对您的问题有帮助!