奇怪的检查/取消选中UI行为

时间:2011-05-04 07:10:00

标签: android listactivity simplecursoradapter checkedtextview

我面临着奇怪的UI行为问题。 当我设置* listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); * onListItemClick表现不正确时......如果我在这里遗漏了任何内容,请告诉我。

当我评论以下行

    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

此UI按预期正常工作,但是部分代码无法正常工作

    long[] idsList = getListView().getCheckedItemIds();
    Log.i(TAG,"number of rows selected"+idsList.length);

Listbox.java

package checkbox.list.com;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.provider.ContactsContract.CommonDataKinds.Phone;


public class listbox extends ListActivity {
private static final String TAG = "listbox";
Cursor mCursor = null;
ListView listView = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    listView = getListView();
    LoadList();
    //listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    int pos = l.getPositionForView(v);
    Log.i(TAG,"pos of view"+pos);
    CheckedTextView txtview = (CheckedTextView) v.findViewById(android.R.id.text1);
    txtview.toggle();
    long[] idsList = getListView().getCheckedItemIds();
    Log.i(TAG,"number of rows selected"+idsList.length);
}

private void LoadList() {
    mCursor = this.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
    startManagingCursor(mCursor);

    String [] from=new String []{Phone.DISPLAY_NAME,Phone.TYPE};
    int [] to=new int [] {android.R.id.text1, android.R.id.text2};
    MyCursorAdapter sca=new MyCursorAdapter(this,R.layout.fetchrow,mCursor,from,to);
    setListAdapter(sca);
}
}

MyCursorAdapter.java

package checkbox.list.com;
import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class MyCursorAdapter extends SimpleCursorAdapter {
    protected static final String TAG = "MyCursorAdapter";
    private Cursor mCursor;
    private String []strFromCols;
    private int []toView;

    public MyCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) {
        super(context, layout, c, from, to);
        mCursor = c;
        strFromCols = from;
        toView = to;
    }
    public View getView(int pos, View inView, ViewGroup parent) {
        View v =  super.getView(pos,inView,parent);
        mCursor.moveToPosition(pos);

        final CheckedTextView chkTxt = (CheckedTextView) v.findViewById(toView[0]);
        String name= mCursor.getString(mCursor.getColumnIndex(strFromCols[0]));
        chkTxt.setText(name);
//      chkTxt.setOnClickListener(new OnClickListener() {
//          
//          @Override
//          public void onClick(View v) {
//              chkTxt.toggle();
//          }
//      });
        TextView txtPhone = (TextView) v.findViewById(toView[1]);
        String phone = mCursor.getString(mCursor.getColumnIndex(strFromCols[1]));
        txtPhone.setText(phone);
        return (v);
    }
}

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="top">
<ListView
    android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:numColumns="1"
    android:stretchMode="columnWidth"
    android:gravity="top"
    android:layout_height="wrap_content" android:layout_gravity="bottom|top"/>

<Button
    android:text="delete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   />
</LinearLayout>

fetchrow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"


/>

    <TextView android:id="@android:id/text2"
        android:textSize="16sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="false"/>


</LinearLayout>

0 个答案:

没有答案