1

我正在使用 aCursorAdapter来处理我的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>

如何TextView在我的布局中切换 的可见性onListItemClick?我试过了:

TextView mTextView = (TextView) v.findViewById(R.id.my_text);
mTextView.setVisibility(mTextView.isShown()? View.GONE: View.VISIBLE);
mAdapter.notifyDataSetChanged();

但它似乎是在随机选择要切换的列表项,无论我点击哪个。

4

3 回答 3

3

您必须存储要隐藏的行的 ID。您可以在活动中创建一个字段来存储这些 ID:

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

其中键是行的长 id,布尔对象表示状态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(并添加到查询中)。

于 2012-03-03T13:35:56.933 回答
1

好吧,您不应该将ListView其用于任何活动视图,请尝试使用 TableLayout 或 LinearLayout。查看此博客文章了解更多详细信息,但它适用于复选框,但适用于任何活动视图。

于 2012-03-03T14:06:35.943 回答
1

您的问题是视图在列表视图中被重用。这意味着即使您有一个包含 100 个项目的列表,也只有 10 个左右的视图实例(取决于屏幕上可以容纳多少)。如果您可以看到方法 bindView 将视图作为参数重用。

您所做的是查看显示例如列表中的第 23 项的视图,并隐藏文本框。当第 23 个项目滚动到屏幕外时,它的视图被重用,但只有值被第 33 个项目的数据替换,您为要重用的视图设置的可见性和其他参数保持不变。

您应该做的是向所有列表项添加一个状态布尔值,并在适配器的 bindView 中根据项目当前想要的状态切换文本框的可见性。

另一件重要的事情是,当您单击项目时,不会调用 bindView 方法。因此,您还应该在 onClick 方法中切换可见性,以使更改立即生效。如果你错过了这一点,视图的外观只会在项目被滚动出去并再次滚动进入后才会改变——这就是 bindView 检查可见性布尔值的时候。

将“数据”中的更改通知 DataSetObservers 会更优雅,并让 ListView 为所有可见项目调用 bindView,但它也更消耗资源。

HashMap<Long, Boolean> positionHide = new HashMap<Long, Boolean>();
    @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
            positionHide.put(id, true);
            tmp = true;
        } else {
                        tmp = !tmp.booleanValue();
            positionHide.put(id, !tmp.booleanValue()); // if the value exists in the map then inverse it's value
        }
        // You should also hide the text view, because bindView will not be called until the list item is scrolled out and in again
                TextView m_text = (TextView) view.findViewById(R.id.my_text);
        if (m_text != null) {
              if(tmp) {
                            // We should hide the text view
                            m_text.setVisibility(View.GONE);
                  } else {
                        // We should display the text view
                            m_text.setVisibility(View.VISIBLE);
                  }
        } 
    }


@Override
public void bindView(View view, Context context, Cursor cursor) {
    String listText = cursor.getString(cursor
            .getColumnIndexOrThrow(DataHandler.MY_TEXT));
            TextView m_text = (TextView) view.findViewById(R.id.my_text);
    if (m_text != null) {
          m_text.setText(listText);
          long pos = cursor.getLong(cursor.getColumnIndex(DataHandler.ID)); // DataHandler.ID will point to the column _id
          if((positionHide.get(pos) != null)&&(positionHide.get(pos))) {
                        // We should hide the text view
                        m_text.setVisibility(View.GONE);
              } else {
                    // We should display the text view
                        m_text.setVisibility(View.VISIBLE);
              }
            }
}

为此,您需要在数据库中拥有该列_id INTEGER PRIMARY KEY AUTOINCREMENT(并添加到查询中)。

于 2012-03-03T13:38:04.190 回答