0

我在活动中有一个 ListView。ListView 的数据是使用 SimpleCursorAdapter 从联系人中填充的。ListView 行的布局是两个 TextView,分别代表人名和数字,以及一个 ImageView,其可见性设置为不可见。

我有适配器的自定义 ViewBinder,它检查 ImageView 是否应该可见。问题是图像是随机可见的。我想问题出在 SimleCursorAdapter 的实现与 newView 和 bindView 一起使用的 ViewHolder 模式中。

我可以在不编写自定义光标适配器的情况下解决问题吗?

以下来源:

list_row.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/contact_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="10dp"
        android:drawableLeft="@drawable/directory_pushed"
        android:drawablePadding="10dp"
        android:text="contact name" />

    <TextView
        android:id="@+id/contact_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/contact_name"
        android:text="093797888" />

    <ImageView
        android:id="@+id/selected"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/icon_phone"
        android:visibility="gone" />

</RelativeLayout>

联系人.java

public class Contacts extends Activity implements
        AdapterView.OnItemClickListener, ViewBinder {

    private ListView mContactList;
    private SimpleCursorAdapter mAdapter;
    private List<String> contacts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contacts);

        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String[] projection = new String[] { Phone._ID, Phone.DISPLAY_NAME,
                Phone.NUMBER };
        String selection = Phone.TYPE + "=?";
        String[] selectionArgs = new String[] { String
                .valueOf(Phone.TYPE_MOBILE) };
        String sortOrder = Phone.DISPLAY_NAME + " ASC";

        contacts = LoginInfo.getContacts(this);

        Cursor managedCursor = getContentResolver().query(uri, projection,
                selection, selectionArgs, sortOrder);

        mContactList = (ListView) findViewById(R.id.contacts_list);

        mAdapter = new SimpleCursorAdapter(this, R.layout.contacts_row,
                managedCursor, new String[] { Phone.DISPLAY_NAME, Phone.NUMBER,
                        Phone.NUMBER }, new int[] { R.id.contact_name,
                        R.id.contact_number, R.id.selected }, 0);

        mAdapter.setViewBinder(this);

        mContactList.setOnItemClickListener(this);

        mContactList.setAdapter(mAdapter);
    }

    @Override
    protected void onPause() {
        super.onPause();

        LoginInfo info = LoginInfo.getLoginInfo(this);
        info.contacts = null;
        info.contacts = contacts;

        LoginInfo.updateCache(this, info);
    }

    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        if (view.getId() == R.id.selected) {
            String number = cursor.getString(columnIndex);

            if (isSelected(number)) {
                view.setVisibility(View.VISIBLE);
            }

            return true;
        }

        return false;
    }

    private boolean isSelected(String number) {
        int index = contacts.indexOf(number);
        return !(index == -1);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        Cursor c = (Cursor) mAdapter.getItem(position);

        String number = c.getString(c.getColumnIndex(Phone.NUMBER));

        contacts.add(number);
    }

}
4

1 回答 1

1

在我看来,创建自定义适配器更简单,但如果你想保持这种方式,试试这个

@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
    if (view.getId() == R.id.selected) {
        String number = cursor.getString(columnIndex);

        if (isSelected(number)) {
            view.setVisibility(View.VISIBLE);
        }
        else {
            view.setVisibility(View.GONE);
        }

        return true;
    }

    return false;
}
于 2013-07-15T22:51:49.127 回答