我正在使用 CursorAdapter
来处理我的 ListActivity
:
public class Mclass extends ListActivity{
...
TheAdapter mAdapter;
public static HashMap<Long, Boolean> shown = new HashMap<Long, Boolean>();
...
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Boolean tmp = shown.get(id);
if (tmp == null) { // if null we don't have this key in the hashmap so we added with the value true
shown.put(id, true);
} else {
shown.put(id, !tmp.booleanValue()); // if the value exists in the map then inverse it's value
}
mAdapter.notifyDataSetChanged();
}
}
和我扩展 CursorAdapter
并覆盖 bindView
的适配器类:
@Override
public void bindView(View view, Context context, Cursor cursor) {
String listText= cursor
.getString(cursor
.getColumnIndexOrThrow(DataHandler.MY_TEXT));
long id = cursor.getLong(cursor.getColumnIndexOrThrow(DataHandler.ROW_ID));
if (Mclass.shown.get(id) != null) {
TextView m_text = (TextView) view
.findViewById(R.id.my_text);
if (m_text != null) {
m_text.setVisibility(Mclass.shown.get(id)? View.VISIBLE:
View.GONE);
if (m_text.isShown())
m_text.setText("STRING");
}
}
我的列表项布局在 xml 文件 list_item.xml
中定义:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#DDDDDD"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/my_image"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:paddingRight="1dip"
android:paddingTop="1dip"
</LinearLayout>
/*
*I want to toggle this text view's visibility
*/
<TextView
android:id="@+id/my_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"//VISIBILITY defined here
android:paddingBottom="2dip"
android:paddingLeft="4dip"
android:paddingRight="4dip"
android:paddingTop="5dip" />
</LinearLayout>
如何在 onListItemClick
中切换布局中 TextView
的可见性?
我试过:
TextView mTextView = (TextView) v.findViewById(R.id.my_text);
mTextView.setVisibility(mTextView.isShown()? View.GONE: View.VISIBLE);
mAdapter.notifyDataSetChanged();
但它似乎是在随机选择要切换的列表项,而不管我点击的是哪个。
最佳答案
您必须存储要隐藏的行的 ID。您可以在 Activity 中创建一个字段来存储这些 ID:
HashMap<Long, Boolean> positionHide = new HashMap<Long, Boolean>();
其中键是行的长 id, bool 对象表示 TextView
的状态(false - 它消失了,true 它可见)。在 onListItemClick()
中,将点击行的 ID 添加到该字段:
@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 added with the value true
status.put(id, true);
} else {
status.put(id, !tmp.booleanValue()); // if the value exists in the map then inverse it's value
}
mAdapter.notifyDataSetChanged();
}
同时修改bindView()
方法:
@Override
public void bindView(View view, Context context, Cursor cursor) {
String listText = cursor.getString(cursor
.getColumnIndexOrThrow(DataHandler.MY_TEXT));
long pos = cursor.getLong(cursor.getColumnIndex(DataHandler.ID)); // DataHandler.ID will point to the column _id
TextView m_text = (TextView) view.findViewById(R.id.my_text);
if(status.get(pos) == null) {
//id is not yet in the hashmap so the value is
//by default false, the TextView is invisible
m_text.setVisibility(View.GONE);
} else {
// we have the value in the
// Hashmap so see what it is and set the
// textview visibility from this value
if (tmp.booleanValue()) {
m_text.setVisibility(View.VISIBLE);
} else {
m_text.setVisibility(View.GONE);
}
}
if (m_text != null)
m_text.setText(listText);
}
为此,您需要在数据库中包含 _id INTEGER PRIMARY KEY AUTOINCREMENT
列(并添加到查询中)。
关于android - 从 ListView 获取选中的 View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9546386/