2016-09-26 77 views
2

我在android中做了一個待辦事項列表應用程序。除了我有一個ListView中的複選框,我無法以編程方式進行設置,一切都運行良好。我正在使用存儲我的任務的SQLite數據庫,以及它們是否被標記爲完整。當我選中一個複選框時,它會在我的數據庫中成功保存「1」。我有一個updateUI方法,它從我的數據庫中讀取我的任務的文本並顯示它。我做了一個新的「updateUICheckBox」方法來處理更新複選框的UI。現在,我只是試圖將所有複選框設置爲選中狀態,但當我調用setChecked(true)時,我得到一個nullpointerexception。在Android中訪問ListView CheckBox

// not working 
private void updateUICheckBox() { 
    CheckBox c = (CheckBox) findViewById(R.id.list_complete_check_box); 
    c.setChecked(true); // null pointer exception 
} 
+0

http://stackoverflow.com/a/10896140/726863 –

回答

1

因爲它是您設置CheckBox的ListView項目,所以您必須在ListView自身的適配器中進行設置。並操縱數據模型來檢查或取消選中CheckBox。

通過擴展BaseAdapter來創建新類,以填充ListView。如果您已經自定義佈局有超過1個UI對象來管理

+0

我添加了一個擴展BaseAdapter的內部類(見上面的編輯)。我填寫了getView方法。我如何在updateUICheckBox方法中根據數據庫值設置複選框? –

+0

檢查此實現http://stackoverflow.com/questions/5457699/cursor-adapter-and-sqlite-example –

1

嘗試在你的列表中添加一個標誌,不要使用ArrayAdapter

public class Country { 
    public String name; 
    public String code; 
    public String abbreviation; 
    public boolean selected = false; //--> example this flag 
    public int image; 
} 

適配器

public class CountryCodeAdapter extends BaseAdapter { 
    private Context context; 
    private List<Country> countryList; 


    public CountryCodeAdapter(Context context, List<Country> countryList) { 
     this.context = context; 
     this.countryList = countryList; 
    } 

    @Override 
    public int getCount() { 
     return countryList.size(); 
    } 

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

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder viewHolder; 
     if (convertView == null) { 
      convertView = LayoutInflater.from(context).inflate(R.layout.adapter_country_code, parent, false); 
      viewHolder = new ViewHolder(convertView); 
      convertView.setTag(viewHolder); 
     } else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 

     Country country = countryList.get(position); 
     viewHolder.tv_item_country_name.setText(country.getName()); 

     String plus = context.getResources().getString(R.string.plus); 
     viewHolder.tv_item_country_code.setText(plus.concat(country.getCode())); 
     int image = country.getImage(); 
     if (image != -1) { 
      viewHolder.iv_item_country.setImageResource(image); 
     } 

     return convertView; 
    } 

    public void updateList(List<Country> countryList) { 
     this.countryList = countryList; 
     notifyDataSetChanged(); 
    } 

    static class ViewHolder { 
     @Bind(R.id.iv_item_country) 
     ImageView iv_item_country; 
     @Bind(R.id.tv_item_country_name) 
     TextView tv_item_country_name; 
     @Bind(R.id.tv_item_country_code) 
     TextView tv_item_country_code; 

     public ViewHolder(View view) { 
      ButterKnife.bind(this, view); 
     } 
    } 
} 

當你想修改標誌,請在適配器外部執行此操作

CountryCodeAdapter adapter = new CountryCodeAdapter(activity, countryList); 
//eg. you may change the 1st element in countryList selected flag into true and //call updateList 
     if (adapter != null) { 
      adapter.updateList(countryList); 
     } 

使用此標誌可將您的複選框設置在適配器內。