gpt4 book ai didi

android - CheckedTextViews 将在列表中随机出现选中状态,如果我点击列表中的更上一层

转载 作者:行者123 更新时间:2023-11-29 02:06:31 25 4
gpt4 key购买 nike

好的,所以这个网站上已经解决了很多问题,但是我不相信我的代码使用的确切问题。我正在用完全工作的 CheckedTextViews 填充 listView。但是,当我单击某个项目时,它会被选中,但当我上下滚动时,随机行也会被选中。我意识到这一定与 ListView 如何跟踪项目有关。我现在遇到了一些错误。我试图用行列表填充 HashMap ,这样我就可以跟踪哪些设置为 true,哪些设置为 false。但是我不确定在哪里实现 map 并尝试填充它。

这是我的 OnCreate

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewmenu);

//Get table name of menu clicked.
Bundle extras = getIntent().getExtras();
tableName = extras.getString("table");

// map each contact's name to a TextView in the ListView layout
String[] from = new String[] { "name" };
int[] to = new int[] { R.id.toppingCheckedTextView };

for(int i=0; i< from.length; i++){
map.put(i, false);
}

contactAdapter = new SimpleCursorAdapter(
ViewToppingListing.this, R.layout.toppings_list_item, null, from, to);
setListAdapter(contactAdapter); // set contactView's adapter
}

我试图将 map 放在 onCreate 中以填充它,但它提示空指针。

这是我尝试使用 OnListItemClick 方法的地方

 @Override
protected void onListItemClick(ListView arg0, View arg1, int arg2, long arg3){
final int index = arg2 - arg0.getFirstVisiblePosition();
View v = arg0.getChildAt(index);
CheckedTextView ctv = (CheckedTextView) v.findViewById(R.id.toppingCheckedTextView);

if((Boolean)map.get(index) == true){
ctv.setChecked(true);
ctv.setVisibility(View.VISIBLE);

} else{
ctv.setVisibility(View.GONE);
}

}

我已经阅读了很多这方面的资料,似乎很多解决方案都涉及使用 getView(),但我不知道这是否适用于我的情况。任何帮助将不胜感激!

最佳答案

首先你需要一个SimpleCursorAdapter吗?您使用 null 游标设置适配器:

contactAdapter = new SimpleCursorAdapter(
ViewToppingListing.this, R.layout.toppings_list_item, null, from, to); // the third parameter is the cursor and you set it to null!

您看到的行为是因为 ListView 正在回收 View ,是的,您必须实现自己的适配器并覆盖 bindView()。下面的代码基于对类似问题的另一个答案,也许你会想看看它(Getting the selected View from ListView)。这是一个例子:

public class TestCursorAdapter extends ListActivity {

MySimpleAdapter adapter;
private HashMap<Long, Boolean> positionHide = new HashMap<Long, Boolean>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] columns = new String[] { "_id", "name" };
MatrixCursor mc = new MatrixCursor(columns); // cursor for testing
for (int i = 1; i < 35; i++) {
long id = i;
mc.addRow(new Object[] { id, "Name" + i });
}
String[] from = new String[] { "name" };
int[] to = new int[] { R.id.checked_text };
adapter = new MySimpleAdapter(this,
R.layout.adapter_mysimpleadapter_row, mc, from, to);
setListAdapter(adapter);
}

private class MySimpleAdapter extends SimpleCursorAdapter {

public MySimpleAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
CheckedTextView ctv = (CheckedTextView) view
.findViewById(R.id.checked_text);
long pos = cursor.getLong(0); // the id from the cursor
if (positionHide.get(pos) == null) {
ctv.setChecked(false);
// we don't have this id in the hashmap so the value is by
// default false, the TextView is GONE
} else {
// we have the value in the Hashmap so see what it is and set
// the textview visibility from this value
Boolean tmp = positionHide.get(pos);
if (tmp.booleanValue()) {
ctv.setChecked(true);
} else {
ctv.setChecked(false);
}
}

}

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Boolean tmp = positionHide.get(id);
if (tmp == null) {
// if null we don't have this key in the hashmap so
// we add it with the value true
positionHide.put(id, true);
} else {
positionHide.put(id, !tmp.booleanValue());
// if the value exists in the map then inverse it's value
}
adapter.notifyDataSetChanged(); // notify the adapter that something has
// changed
}
}

关于android - CheckedTextViews 将在列表中随机出现选中状态,如果我点击列表中的更上一层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9759859/

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