充分利用ListView中选择查看 [英] Getting the selected View from ListView

查看:298
本文介绍了充分利用ListView中选择查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是的CursorAdapter 来处理我的 ListActivity

I am using a CursorAdapter to handle my 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

and my adapter class that extends CursorAdapter and overrides 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 我的布局?
我试过:

How can I toggle the visibility of the TextView in my layout within onListItemClick? I've tried:

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

但它似乎进行选择以随机触发该列表中的项目,无论是一个我点击的。

but it seems to be choosing which list items to toggle at random, regardless of the one I click.

推荐答案

您必须保存你隐藏的行的id。你可以让你的活动字段来存储这些ID:

You'll have to store the id's of the rows you'll hide. You could make a field in your activity to store those ids:

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

,其中最关键的是该行和Boolean对象重新$ P $的长ID psents的状态的TextView (假就消失了,真的是可见)。在 onListItemClick()点击的行的ID添加到该字段:

where the key is the long id of the row and the boolean object represents the status of the TextView(false- it is gone, true it is visible). In the onListItemClick() add the ids of the clicked rows to that field:

    @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 (也添加到查询)。

这篇关于充分利用ListView中选择查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆