simplecursoradapter随机更新视图

时间:2013-07-15 21:40:48

标签: android listview simplecursoradapter

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

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

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

来源:

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>

Contacts.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);
    }

}

1 个答案:

答案 0 :(得分: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;
}