2

CheckedTextViewを使用し、から拡張するアダプターがありSimpleCursorAdapterます。のデフォルト値を読み込もうとしましCheckTextViewたが、うまくいきませんでした。CheckedTextViewこれがの動作なのか、何か間違ったことをしているのかはわかりません。

私が試したことは何ですか?

getView()メソッドをオーバーライドしてCheckedTextViewビューを取得しようとしましたがsetChecked(boolean)、FAILED、奇妙に機能しませんでした。

次に、を使用しようとしましたViewBinderが、それも機能しません。

この問題に対する洞察はありますか?

adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
            public boolean setViewValue(View view, Cursor cursor,
                    int columnIndex) {
                CheckedTextView cb = (CheckedTextView) view;
                int checked=cursor.getInt(cursor.getColumnIndex(ProgramSurvey.CHECKED));
                cb.setChecked(checked==1);

                return false;
            }
        });
4

4 に答える 4

4

getView から返されたビューが CheckedTextView (または Checkable を実装するその他のビュー) である場合、listView は、(setItemChecked への呼び出しから) 選択されていると考えられる行に基づいて、関連する行にチェックを設定します。自分で設定しようとするのではなく、どの行がチェックされているかを ListView に伝える必要があります。あるいは、checkable を実装していないビューでビューをラップすると、CheckedTextView で自分でチェックを設定できるようになります。

于 2011-11-18T02:30:35.287 に答える
2

これを試すことができます:

listview.setItemChecked(position,true);
于 2012-08-13T07:58:34.453 に答える
0

レイアウトを膨らませようとしました(android.R.layout.simple_list_item_multiple_choice)...単純に変更されたチェックボックスの画像とテキストビューの色とパディングを提供します。checkedTextviewに似ています。

アダプタ クラス:

public class CustomListAdapter extends BaseAdapter {

    private String[] stringArray;
    private Context mContext;
    private LayoutInflater inflator;
    int checkbox;
    /**
     * 
     * @param context
     * @param stringArray
     */
    public CustomListAdapter(Context  context, String[] stringArray) 
    {
        this.mContext=context;
        this.stringArray=stringArray;
        this.inflator= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount()
    {
        return stringArray.length;
    }

    @Override
    public Object getItem(int position)
    {
        return position;
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {

        final MainListHolder mHolder;
        View v = convertView;
        if (convertView == null)
        {
            mHolder = new MainListHolder();
            v = inflator.inflate(android.R.layout.simple_list_item_multiple_choice, null);
            mHolder.txt=(CheckedTextView) v.findViewById(android.R.id.text1);
            v.setTag(mHolder);
        } 
        else
        {
            mHolder = (MainListHolder) v.getTag();
        }
        mHolder.txt.setText(stringArray[position]);
        mHolder.txt.setTextSize(12);
        mHolder.txt.setTextColor(Color.YELLOW);

    /**
     * When checkbox image is set By setImageFromResourceCheckBox(int id) method...Otherwise it takes default
     */
        if(checkbox!=0)
            mHolder.txt.setCheckMarkDrawable(R.drawable.android_button);

        mHolder.txt.setPadding(5, 5, 5, 5);
        return v;
    }
    class MainListHolder 
    {
        private CheckedTextView txt;

    }
    /***
     * Setting Image for Checkbox
     * @param id
     * 
     */
    public void setImageFromResourceCheckBox(int id)
    {
        this.checkbox=id;
    }


}

主な活動:

public class CheckedListActivity extends ListActivity implements OnItemClickListener  {

    //final String[] str={"Android","Black Berry","Iphone","Ipad"};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        CustomListAdapter c=new CustomListAdapter(this,GENRES);
        c.setImageFromResourceCheckBox(R.drawable.android_button);
        setListAdapter(c);

        final ListView listView = getListView();
        listView.setItemsCanFocus(false);
        //listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);// For single mOde

        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        //used for default selection .....
        listView.setItemChecked(2, true);
        listView.setOnItemClickListener(this);
    }


    private static final String[] GENRES = new String[] 
                                                {
        "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
        "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
    };
    @Override
    public void onItemClick(AdapterView<?> adapter, View arg1, int arg2, long arg3)
    {

    SparseBooleanArray sp=getListView().getCheckedItemPositions();

    String str="";
    for(int i=0;i<sp.size();i++)
    {
        str+=GENRES[sp.keyAt(i)]+",";
    }
    Toast.makeText(this, ""+str, Toast.LENGTH_SHORT).show();

    }
}
于 2011-11-18T05:42:38.920 に答える