2016-09-26 75 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); 
     } 

使用此标志可将您的复选框设置在适配器内。