2

我有一个适配器,使用CheckedTextView并从SimpleCursorAdapter扩展。我试图加载CheckTextView的默认值,但我没有设法做到这一点。我不知道这是否是CheckedTextView的行为,或者我做错了什么。CheckedTextView从SimpleCursorAdapter加载默认值

我试过了什么?

我试图覆盖getView()方法并得到CheckedTextView视图和使用setChecked(boolean),失败,奇怪它不起作用。然后我试着用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; 
      } 
     }); 
+0

你可以发布你的观点粘结剂的代码示例? “ –

回答

4

如果查看从getView返回是CheckedTextView(或实现可检查任何其他视图),则列表视图将设置相关的行检查的基础上,它认为选择(从调用哪些行setItemChecked)。您需要告诉ListView哪些行被选中,而不是试图自己设置它,或者将视图封装在未实现可检查的视图中,然后您可以在CheckedTextView中对自己进行检查。

+0

”在视图中包装你的视图,不实现可检查“我试过这个,但是当我试图检查它们时,它不允许我这样做...... – Necronet

0

它实际上有助于从@superfell的想法,但因为我的粘结剂取决于上的光标,我不得不重新查询的点击甚至从ListView控件,这样

 lv.setOnItemClickListener(new OnItemClickListener() { 

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

       CheckedTextView checkBox = (CheckedTextView) view 
         .findViewById(R.id.checkTextProgram); 
       CursorWrapper cursor = (CursorWrapper) adapterlist 
         .getItemAtPosition(pos); 

       if (checkBox.isChecked()) { 
        String programId = cursor.getString(cursor 
          .getColumnIndex(Program._ID)); 
        getActivity().getContentResolver().delete(
          ProgramSurvey.buildUri(_ID), 
          ProgramSurvey.PROGRAM_ID + "=?", 
          new String[] { programId }); 
       } else { 
        ContentValues values = new ContentValues(); 
        values.put(ProgramSurvey.PROGRAM_ID, cursor 
          .getString(cursor.getColumnIndex(Program._ID))); 
        values.put(ProgramSurvey.SURVEY_ID, _ID); 
        getActivity().getContentResolver().insert(
          ProgramSurvey.CONTENT_URI, values); 
       } 

       getLoaderManager().restartLoader(0, null, ProgramDialogFragment.this); 

      } 

     }); 

所以我用装载机经理根据需求重新调整光标。

0

我只是试图膨胀布局(android.R.layout.simple_list_item_multiple_choice)...它给出了简单修改复选框图像和textview颜色和填充。它与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(); 

    } 
} 
2

你可以试试这个:

listview.setItemChecked(position,true);