gpt4 book ai didi

java - Android BaseAdapter 仅显示一次正确信息

转载 作者:行者123 更新时间:2023-12-01 12:40:11 25 4
gpt4 key购买 nike

我正在尝试组合一个适配器来显示列表中出现的次数。选项卡宿主中有两个 ListView,每个 ListView 都位于自己的选项卡中。只要您不单击显示事件的选项卡,它就会很好地跟上数据输入。第一次查看 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();
}
}

最佳答案

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

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

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

关于java - Android BaseAdapter 仅显示一次正确信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25185737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com