第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Android:將數(shù)據(jù)從數(shù)據(jù)庫綁定到ListView中的CheckBox?

Android:將數(shù)據(jù)從數(shù)據(jù)庫綁定到ListView中的CheckBox?

ibeautiful 2019-10-15 15:28:42
我正在嘗試將數(shù)據(jù)從我綁定SQLiteDatabase到ListView。我目前正在使用SimpleCursorAdapter來填寫我的ListView。不幸的是,這似乎不適用于設置CheckBox的checked屬性。我現(xiàn)在就是這樣做的。適配器沒有將CheckBox的選中狀態(tài)更改為文本值,而是將其填寫到文本參數(shù)中,因此該值以文本形式顯示在CheckBox的右側。Java:setListAdapter( new SimpleCursorAdapter( this,      R.layout.mylist,      data,      new String[] { Datenbank.DB_STATE, Datenbank.DB_NAME },      new int[] { R.id.list_checkbox, R.id.list_text }    ) );mylist.xml:<?xml version="1.0" encoding="utf-8"?><LinearLayout android:id="@+id/LinearLayout01"android:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"><CheckBox android:text=""    android:id="@+id/list_checkbox"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:checked="false"    ></CheckBox><TextView android:text=""    android:id="@+id/list_text"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    ></TextView></LinearLayout>編輯:數(shù)據(jù)庫中的字段當然是布爾型的,我也嘗試過為已檢查的字段分配ID以填充值。
查看完整描述

3 回答

?
慕桂英4014372

TA貢獻1871條經(jīng)驗 獲得超13個贊

除了創(chuàng)建一個覆蓋newView / bindView或getView的自定義適配器之外,我不確定您將如何執(zhí)行此操作,具體取決于您覆蓋的內容(ResourceCursorAdapter是個不錯的選擇)。


好的,這是一個例子。我沒有測試是否可以編譯,因為我正在工作,但這絕對可以為您指明正確的方向:


public class MyActivity extends ListActivity {


    MyAdapter mListAdapter;


    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        Cursor myCur = null;


        myCur = do_stuff_here_to_obtain_a_cursor_of_query_results();


        mListAdapter = new MyAdapter(MyActivity.this, myCur);

        setListAdapter(mListAdapter);

    }



    private class MyAdapter extends ResourceCursorAdapter {


        public MyAdapter(Context context, Cursor cur) {

            super(context, R.layout.mylist, cur);

        }


        @Override

        public View newView(Context context, Cursor cur, ViewGroup parent) {

            LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            return li.inflate(R.layout.mylist, parent, false);

        }


        @Override

        public void bindView(View view, Context context, Cursor cur) {

            TextView tvListText = (TextView)view.findViewById(R.id.list_text);

            CheckBox cbListCheck = (CheckBox)view.findViewById(R.id.list_checkbox);


            tvListText.setText(cur.getString(cur.getColumnIndex(Datenbank.DB_NAME)));

            cbListCheck.setChecked((cur.getInt(cur.getColumnIndex(Datenbank.DB_STATE))==0? false:true))));

        }

    }

}


查看完整回答
反對 回復 2019-10-15
?
桃花長相依

TA貢獻1860條經(jīng)驗 獲得超8個贊

您可以設置一個自定義SimpleCursorAdapter.ViewBinder:


SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(/* ur stuff */);

cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {

        if(columnIndex == 1) {

            CheckBox cb = (CheckBox) view;

            cb.setChecked(cursor.getInt(1) > 0);

            return true;

        }

        return false;

    }

});

setViewValue在SimpleCursorAdapter構造函數(shù)中為您指定的每個列都調用該方法,并為您提供了一個操作某些(或全部)視圖的好地方。


查看完整回答
反對 回復 2019-10-15
?
阿晨1998

TA貢獻2037條經(jīng)驗 獲得超6個贊

您可以通過創(chuàng)建自定義CheckBox小部件來解決該問題,如下所示:


package com.example.CustomCheckBox;    

public class CustomCheckBox extends CheckBox {

     public CustomCheckBox(Context context, AttributeSet attrs, int defStyle) {

      super(context, attrs, defStyle);

     }

     public CustomCheckBox(Context context, AttributeSet attrs) {

      super(context, attrs);

     }

     public CustomCheckBox(Context context) {

      super(context);

     }

     protected void onTextChanged(CharSequence text, int start, int before, int after) {

      if (text.toString().compareTo("") != 0) {

       setChecked(text.toString().compareTo("1") == 0 ? true : false);

       setText("");

      }

     }

    }

當ListView將數(shù)據(jù)綁定到CheckBox時(即添加“ 0”或“ 1”),將調用onTextChanged函數(shù)。這將捕獲該更改并添加您的布爾處理。需要第一個“ if”語句,以免產(chǎn)生無限遞歸。


然后像這樣在布局文件中提供您的自定義類:


<com.example.CustomCheckBox

 android:id="@+id/rowCheckBox"

 android:layout_height="fill_parent"

 android:layout_width="wrap_content" />

那應該做!


查看完整回答
反對 回復 2019-10-15
  • 3 回答
  • 0 關注
  • 519 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號