Java Android BaseAdapter只显示一次正确的信息
我正在尝试组合一个适配器,它将显示列表中出现的次数。选项卡主机中有两个ListView,每个都位于自己的选项卡中。只要不单击显示事件的选项卡,它就会很好地跟上数据输入。第一次查看listview时,将显示所有组,每个组的出现次数都正确;但是,从那时起,它就不想正确更新。有时它根本不会更新,请重新插入现有组,但仍保留旧组 例如,如果我在第一次查看groups listview时输入10、20和30,我会看到:Java Android BaseAdapter只显示一次正确的信息,java,android,arraylist,hashmap,baseadapter,Java,Android,Arraylist,Hashmap,Baseadapter,我正在尝试组合一个适配器,它将显示列表中出现的次数。选项卡主机中有两个ListView,每个都位于自己的选项卡中。只要不单击显示事件的选项卡,它就会很好地跟上数据输入。第一次查看listview时,将显示所有组,每个组的出现次数都正确;但是,从那时起,它就不想正确更新。有时它根本不会更新,请重新插入现有组,但仍保留旧组 例如,如果我在第一次查看groups listview时输入10、20和30,我会看到: 10 -- 1 20 -- 1 30 -- 1 …但如果我再加上10,它仍然会显示相同
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));
}
}
}
导入android.content.Context;
导入android.net.Uri;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.TextView;
导入java.net.URI;
导入java.util.ArrayList;
导入java.util.LinkedHashMap;
公共类HashAdapter扩展了BaseAdapter{
私有LinkedHashMap mData=新LinkedHashMap();
private final ArrayList keyArray=新ArrayList();
私有最终上下文_上下文;
公共哈希适配器(上下文){
_上下文=上下文;
}
@凌驾
public int getCount(){
返回keyArray.size();
}
@凌驾
公共字符串getItem(int位置){
返回mData.get(keyArray.get(position));
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
最终视图列表视图;
最终LayoutFlater充气器=(LayoutFlater)\u context.getSystemService(context.LAYOUT\u充气器\u SERVICE);
如果(convertView==null){//FIXME
listView=充气机。充气(R.layout.list_项,空);
TextView TextView=(TextView)listView.findViewById(R.id.listItem);
String tile=keyArray.get(位置)+“--”+getItem(位置);
textView.setText(平铺);
Log.i(“getView文本为”,平铺);
}否则{
listView=convertView;
}
返回列表视图;
}
public void addNewValue(字符串键){
if(mData.containsKey(键)){
int retrieve=Integer.parseInt(mData.get(key));
put(key,String.valueOf(++retrieve));
}else{//创建值为1的新密钥的哈希映射
mData.put(键“1”);
添加(0,键);
}
this.notifyDataSetChanged();
Log.i(“数组列表”,keyArray.toString());
}
公共void modifyExistingValue(字符串oldValue、字符串newValue){
如果(!oldValue.equals(newValue)){
int newInt=Integer.parseInt(mData.get(oldValue));
如果(newInt>1){
mData.put(oldValue,String.valueOf(--newInt));
字符串secondInt=mData.get(newValue);
if(secondInt!=null){
int newint=Integer.parseInt(secondInt);
mData.put(newValue,String.valueOf(++newint));
}否则{
mData.put(新值,“1”);
}
}否则{
mData.remove(oldValue);
keyArray.remove(oldValue);
mData.put(新值,“1”);
Log.i(“旧值”,旧值+“从HashMap中消失”);
}
添加(0,newValue);
}
this.notifyDataSetChanged();
Log.i(“数组列表”,keyArray.toString());
}
public void removeOccurrence(字符串oldValue){
字符串oldOccurrencess=mData.get(oldValue);
如果(!oldOccurrencess.equals(“1”){//剩余的空间足够使其仍然存在
int newInt=Integer.parseInt(oldOccurrencess);
mData.put(oldValue,String.valueOf(--newInt));
}否则{//剩余的空间不足…请将其删除
mData.remove(oldValue);
keyArray.remove(oldValue);
Log.i(“旧值”,旧值+“从HashMap中消失”);
}
this.notifyDataSetChanged();
Log.i(“数组列表”,keyArray.toString());
}
公共空间清除(){
mData.clear();
keyArray.clear();
this.notifyDataSetChanged();
Log.i(“数组列表”,keyArray.toString());
}
公共值(字符串键){
Log.i(“AdapterDebug值”,key+“有”+mData.get(key));
}
public void debugAll(){
int size=keyArray.size();
对于(int i=0;i
编辑:
我想有些人可能会发现完整的工作版本很有用,所以我将把它放在这里。
它将跟踪重复出现的值。以它为基础,按原样使用,我不在乎
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();
}
}
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.TextView;
导入java.util.ArrayList;
导入java.uti
@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;
}