如何使用ArrayList中从LT和插入数据,HashMap的<字符串,字符串>>进入Android Studio中的列表视图 [英] How to use insert data from ArrayList<HashMap<String, String>> into a list view in Android Studio

查看:146
本文介绍了如何使用ArrayList中从LT和插入数据,HashMap的<字符串,字符串>>进入Android Studio中的列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作是在远程服务器上获取查询从数据库中的一个项目。它的SQL一边从两个单独的表获取信息,将它们组合并发送一个JSON EN codeD字符串支持我。然后,我解析字符串和字符串,字符串的一个HashMap来保存数据创建一个ArrayList。所有这一切都工作得很好。有108项和所有相关的变量显示此。

当我尝试把最后的ArrayList的HashMap成一个ListView我的问题就出现了。我创建了一个自定义布局,我知道作品我通过使用DataProvider类和自定义适配器的使用来填充它从一组阵列。

我也跟着教程创建ArrayList中的HashMap( http://techlovejump.com自定义适配器/ Android的multicolum-列表视图/ )。它似乎工作除了它只会拉离ArrayList中的HashMap的项目10-12,然​​后重复。它在列表正好108项。我注意到,我虽然滚动列表列表中的第一项,这只是在屏幕的改变为​​不同的项目。

我已经调试中的自定义适配器类code和一切工作正常位置从0开始直到。我主要看到它转到如图10所示,一次或两次12.在这一点上,下一次就通过getView方法的位置被返回到0,并重新开始。

我刚学了这一切,很可能做错事只是不知道是什么。这里是code我要调用自定义适配器类和类本身。

自定义适配器这从我的解析JSON字符串,并建立原始的ArrayList HashMap中的方法调用

进口android.app.Activity;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.TextView;进口的java.util.ArrayList;
进口的java.util.HashMap;/ **
 *创建者jokerfwb于15年11月12日。
 * /
公共类UsersmyListAdapter延伸BaseAdapter {    私有静态最后弦乐TAG_NAME =姓名;
    私有静态最后弦乐年龄=UserAge;
    私有静态最后弦乐CLASS =类;
    私有静态最后弦乐LEVEL =级别;
    私有静态最后弦乐技巧=技能;
    私有静态最后弦乐活性=活动;
    私有静态最后弦乐TIME_STAMP =时间戳;
    私有静态最后弦乐TAG_IN_GROUP =内群体;    公众的ArrayList<&HashMap的LT;字符串,字符串>> myList中;
    活动活动;
    TextView的名称;
    TextView的classType所;
    TextView的技能;
    TextView的activityType;    公共UsersmyListAdapter(活动活动,ArrayList的<&HashMap的LT;字符串,字符串>> myList中){
        超();
        this.activity =活动;
        this.myList = myList中;
    }    @覆盖
    公众诠释的getCount(){
        返回myList.size();
    }    @覆盖
    公共对象的getItem(INT位置){
        返回myList.get(位置);
    }    @覆盖
    众长getItemId(INT位置){
        返回0;
    }    公共查看getView(INT位置,查看convertView,父母的ViewGroup){        LayoutInflater吹气= activity.getLayoutInflater();        如果(convertView == NULL){
            convertView = inflater.inflate(R.layout.users_lfg_list_item,NULL);            名称=(TextView中)convertView.findViewById(R.id.name);
            classType所=(TextView中)convertView.findViewById(R.id.classType);
            技能=(TextView中)convertView.findViewById(R.id.skill);
            activityType =(TextView中)convertView.findViewById(R.id.activityType);
        }        HashMap的<字符串,字符串>地图= myList.get(位置);
        name.setText(map.get(TAG_NAME));
        classType.setText(map.get(CLASS));
        skill.setText(map.get(技能));
        activityType.setText(map.get(活动));        返回convertView;
    }}

从我的活动自定义适配器该被调用时,它分析由我的AsyncTask拉JSON字符串的方法的结束通话

UsersLFGListAdapter适配器=新UsersLFGListAdapter(这一点,UsersLFG);
listView.setAdapter(适配器);

发现了一个解决方案,以及

这Varzaru答案奇妙的工作。我还发现了另一种解决方案后,更使然。只是想将它张贴了其他任何人看到这一点。这是如下:

impleAdapter UserArrayList =新SimpleAdapter(UsersLFGListActivity.this,UsersLFG,R.layout.users_lfg_list_item,新的String [] {TAG_DESTINY_USER_NAME, TAG_ACTIVE_CLASS,TAG_ACTIVE_CLASS_LIGHT,TAG_ACTIVITY_TYPE},新的INT [] {R.id.name,R.id.C​​lass,R.id.Light,R.id.activityType});
listView.setAdapter(UserArrayList);


解决方案

好吧,没有看到你的其他类,我可以在此拍摄。首先你的列表视图适配器不好,试试这个:

 公共类UsersmyListAdapter扩展ArrayAdapter<&HashMap的LT;字符串,字符串>> {    私有静态最后弦乐TAG_NAME =姓名;
    私有静态最后弦乐年龄=UserAge;
    私有静态最后弦乐CLASS =类;
    私有静态最后弦乐LEVEL =级别;
    私有静态最后弦乐技巧=技能;
    私有静态最后弦乐活性=活动;
    私有静态最后弦乐TIME_STAMP =时间戳;
    私有静态最后弦乐TAG_IN_GROUP =内群体;    公众的ArrayList<&HashMap的LT;字符串,字符串>> myList中;
    活动活动;    公共UsersmyListAdapter(活动活动,ArrayList的<&HashMap的LT;字符串,字符串>> myList中){
        超(活动,R.layout.users_lfg_list_item,myList中);
        this.activity =活动;
        this.myList = myList中;
    }    @覆盖
    公众诠释的getCount(){
        返回myList.size();
    }    @覆盖
    公共对象的getItem(INT位置){
        返回myList.get(位置);
    }    @覆盖
    众长getItemId(INT位置){
        返回0;
    }    公共查看getView(INT位置,查看convertView,父母的ViewGroup){        HashMap的<字符串,字符串>地图= myList.get(位置);
        ItemViewHolder viewHolder;        如果(convertView == NULL){
            viewHolder =新ItemViewHolder();
            。convertView = activity..getLayoutInflater()膨胀(R.layout.users_lfg_list_item,空,真);
            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);
        }其他{
            viewHolder =(ItemViewHolder)convertView.getTag();
        }        viewHolder.name.setText(map.get(TAG_NAME));
        viewHolder.classType.setText(map.get(CLASS));
        viewHolder.skill.setText(map.get(技能));
        viewHolder.activityType.setText(map.get(活动));        返回convertView;
    }    公共类ItemViewHolder {
        TextView的名称;
        TextView的classType所;
        TextView的技能;
        TextView的activityType;
    }}

二你不应该设置你的完整的AsyncTask的方法在ListView适配器。你应该设置适配器的上创建的后声明的列表视图,当AsyncTask的完然后添加新项目到列表中,并呼吁:

  adapter.notifyDataSetChanged();

希望它可以帮助您解决问题!

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

解决方案

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!

这篇关于如何使用ArrayList中从LT和插入数据,HashMap的&LT;字符串,字符串&GT;&GT;进入Android Studio中的列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆