Android BaseAdapter只显示一次正确的信息

时间:2014-08-07 14:57:20

标签: java android arraylist hashmap baseadapter

我试图将一个适配器组合在一起,该适配器将显示列表中出现的次数。选项卡主机中有两个ListView,每个ListView都在其自己的选项卡中。只要您没有单击显示事件的选项卡,它就会跟上数据输入。第一次查看列表视图时,所有组都会显示每个组的正确出现次数;然而,从那时起,它不想正确更新。有时它根本不会更新,重新插入现有的组,但仍然保留旧组。

例如,如果我第一次看到群组列表视图时放入10,20和30,我会看到:

10 -- 1
20 -- 1
30 -- 1

...但是如果我要放入另外10个,它仍然会显示相同的内容而没有更新。 当我把45这样的东西放进去的时候我得到了:

10 -- 1
20 -- 1
30 -- 1
10 -- 2

我可以继续添加其他数字,它会继续添加10 -- 1或有时甚至更多的20或30。如果我在日志窗口中使用debugAll(),它将显示它尝试我输入的所有数字,并且它将返回正确的出现次数,只是显示器不会跟上并显示更多已经存在的东西。

我已尝试输入几个日志语句,它们显示适配器中的信息是正确的,只是显示错误。

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.net.URI;
import java.util.ArrayList;
import java.util.LinkedHashMap;

public class HashAdapter extends BaseAdapter {
    private LinkedHashMap<String, String> mData = new LinkedHashMap<String, String>();
    private final ArrayList keyArray = new ArrayList();
    private final Context _context;

    public HashAdapter (Context context) {
        _context = context;
    }

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

    @Override
    public String getItem(int position) {
        return mData.get(keyArray.get(position));
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final View listView;
        final LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) { // FIXME
            listView = inflater.inflate(R.layout.list_item, null);
            TextView textView = (TextView) listView.findViewById(R.id.listItem);

            String tile = keyArray.get(position) + " -- " + getItem(position);
            textView.setText(tile);
            Log.i("getView Text Is", tile);
        } else {
            listView = convertView;
        }

        return listView;
    }

    public void addNewValue (String key) {
        if (mData.containsKey(key)) {
            int retrieve = Integer.parseInt(mData.get(key));
            mData.put(key, String.valueOf(++retrieve));
        } else { // HASH MAP CREATING A NEW KEY WITH VALUE OF 1
            mData.put(key, "1");
            keyArray.add(0, key);
        }
        this.notifyDataSetChanged();
        Log.i("Array List ", keyArray.toString());
    }

    public void modifyExistingValue (String oldValue, String newValue) {
        if (!oldValue.equals(newValue)) {
            int newInt = Integer.parseInt(mData.get(oldValue));
            if (newInt > 1) {
                mData.put(oldValue, String.valueOf(--newInt));
                String secondInt = mData.get(newValue);
                if (secondInt != null) {
                    int newNewInt = Integer.parseInt(secondInt);
                    mData.put(newValue, String.valueOf(++newNewInt));
                } else {
                    mData.put(newValue, "1");
                }
            } else {
                mData.remove(oldValue);
                keyArray.remove(oldValue);
                mData.put(newValue, "1");
                Log.i("Old Value ", oldValue + " Is Gone From HashMap");
            }
            keyArray.add(0, newValue);
        }
        this.notifyDataSetChanged();
        Log.i("Array List ", keyArray.toString());
    }

    public void removeOccurrence  (String oldValue) {
        String oldOccurrences = mData.get(oldValue);

        if (!oldOccurrences.equals("1")) { // IF THERE ARE ENOUGH REMAINING TO STILL EXIST
            int newInt = Integer.parseInt(oldOccurrences);
            mData.put(oldValue, String.valueOf(--newInt));
        } else { // NOT ENOUGH LEFT...REMOVE IT
            mData.remove(oldValue);
            keyArray.remove(oldValue);
            Log.i("Old Value", oldValue + " Is Gone From HashMap");
        }
        this.notifyDataSetChanged();
        Log.i("Array List ", keyArray.toString());
    }

    public void clear () {
        mData.clear();
        keyArray.clear();
        this.notifyDataSetChanged();
        Log.i("Array List ", keyArray.toString());
    }

    public void debugValue (String key) {
        Log.i("AdapterDebug Value", key + " has " + mData.get(key));
    }

    public void debugAll () {
        int size = keyArray.size();
        for (int i = 0; i < size; i++) {
            String retrieve = (String) keyArray.get(i);
            Log.i("AdapterDebug All", "Attempted " + retrieve + " and retrieved " + mData.get(retrieve));
        }
    }
}

修改

我认为有些人可能会发现完全正常工作的版本很有用,所以我会把它放在这里。 它将跟踪重新出现的值。建立它,按原样使用,我不在乎。

import android.content.Context;
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.LinkedHashMap;

/*
*       ARRAY ADAPTER FOR KEEPING TRACK OF REOCCURRING VALUES AND DISPLAYING ON A LISTVIEW
*       CREATED BY : COREY WILLIAMS
*       8/13/2014
*
*       BeerWare (http://en.wikipedia.org/wiki/Beerware)
* */

class HashAdapter extends BaseAdapter {
    private LinkedHashMap<String, String> mData = new LinkedHashMap<String, String>();
    private ArrayList keyArray = new ArrayList();
    private final Context _context;

    public HashAdapter (Context context) {
        _context = context;
    }

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

    @Override
    public String getItem(int position) {
        return mData.get(keyArray.get(position));
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final View listView;
        final LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            listView = inflater.inflate(R.layout.list_item, null);
            TextView textView = (TextView) listView.findViewById(R.id.listItem);

            String tile = keyArray.get(position) + " -- " + getItem(position);
            textView.setText(tile);
        } else {
            listView = convertView;
            TextView textView = (TextView) listView.findViewById(R.id.listItem);

            String tile = keyArray.get(position) + " -- " + getItem(position);
            textView.setText(tile);
        }

        return listView;
    }

    public void addNewValue (String key) {
        if (mData.containsKey(key)) {
            int retrieve = Integer.parseInt(mData.get(key));
            mData.put(key, String.valueOf(++retrieve));
        } else { // HASH MAP CREATING A NEW KEY WITH VALUE OF 1
            mData.put(key, "1");
            keyArray.add(0, key);
        }
        this.notifyDataSetChanged();
    }

    public void modifyExistingValue (String oldValue, String newValue) {
        if (!oldValue.equals(newValue)) {
            int oldAmmount = Integer.parseInt(mData.get(oldValue)); // GET HOW MANY ARE LEFT OF WHAT YOU SELECTED
            boolean alreadyHasNewValue = mData.containsKey(newValue);
            if (oldAmmount > 1) { // IF THERE IS SOME LEFT
                mData.put(oldValue, String.valueOf(--oldAmmount));
                if (alreadyHasNewValue) {
                    String secondInt = mData.get(newValue);
                    int newNewInt = Integer.parseInt(secondInt);
                    mData.put(newValue, String.valueOf(++newNewInt));
                } else {
                    mData.put(newValue, "1");
                }
            } else {
                mData.remove(oldValue);
                keyArray.remove(oldValue);
                if (alreadyHasNewValue) {
                    String secondInt = mData.get(newValue);
                    int newNewInt = Integer.parseInt(secondInt);
                    mData.put(newValue, String.valueOf(++newNewInt));
                } else {
                    mData.put(newValue, "1");
                }
            }
            if (!keyArray.contains(newValue)) {
                keyArray.add(0, newValue);
            }

        }
        this.notifyDataSetChanged();
    }

    public void removeOccurrence  (String oldValue) {
        String oldOccurrences = mData.get(oldValue);

        if (!oldOccurrences.equals("1")) { // IF THERE ARE ENOUGH REMAINING TO STILL EXIST
            int newInt = Integer.parseInt(oldOccurrences);
            mData.put(oldValue, String.valueOf(--newInt));
        } else { // NOT ENOUGH LEFT...REMOVE IT
            mData.remove(oldValue);
            keyArray.remove(oldValue);
        }
        this.notifyDataSetChanged();
    }

    public void clear () {
        mData.clear();
        keyArray.clear();
        this.notifyDataSetChanged();
    }

    public ArrayList getKeys () {
        return keyArray;
    }

    public void restoreKeys (ArrayList<String> restore) {
        keyArray = restore;
        this.notifyDataSetChanged();
    }

    public LinkedHashMap<String, String> getValues () {
        return mData;
    }

    public void restoreValues (LinkedHashMap<String, String> restore) {
        mData = restore;
        this.notifyDataSetChanged();
    }
}

1 个答案:

答案 0 :(得分:1)

在“getView”方法中,即使“convertView”不为null,也必须更新内容占位符(代码中的TextView)。 这是因为ListView的视图回收系统。

所以更新的代码可能是这样的:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final View listView;
    final LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        listView = inflater.inflate(R.layout.list_item, null);
    } else {
        listView = convertView;
    }

    TextView textView = (TextView) listView.findViewById(R.id.listItem);

    String tile = keyArray.get(position) + " -- " + getItem(position);
    textView.setText(tile);
    Log.i("getView Text Is", tile);

    return listView;
}