2016-09-26 2 views
2

나는 안드로이드에 할일 목록 앱을 만들고 있습니다. 프로그래밍 방식으로 설정할 수없는 ListView에 체크 상자가있는 것을 제외하면 모든 것이 잘 작동합니다. 내 작업을 저장하는 SQLite 데이터베이스를 사용하고 있으며 완료되었는지 표시되었는지 여부를 확인합니다. 확인란을 선택하면 데이터베이스에 "1"이 성공적으로 저장됩니다. 데이터베이스에서 내 작업 텍스트를 읽고 표시하는 updateUI 메서드가 있습니다. 나는 새 "updateUICheckBox"메서드를 사용하여 체크 상자의 UI 업데이트를 처리했습니다. 지금 당장 나는 모든 체크 박스를 체크 표시로 설정하려하지만 setChecked (true)를 호출하면 nullpointerexception이 발생한다.안드로이드에서 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를 선택 또는 선택 취소합니다.

ListView를 채우기 위해 BaseAdapter를 확장하여 새 클래스를 만듭니다. 당신이

+0

BaseAdapter를 확장하는 내부 클래스를 추가했습니다 (위 편집 참조). 나는 getView 메소드를 채웠다. 어떻게 내 updateUICheckBox 메서드에서 데이터베이스 값에 따라 확인란을 설정하는 데 활용할 수 있습니까? –

+0

구현을 확인하십시오. http://stackoverflow.com/questions/5457699/cursor-adapter-and-sqlite-example –

1

이 목록에서 플래그를 추가하려고 관리하는 1 개 이상의 UI 객체와 사용자 정의 레이아웃을 한 경우 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); 
     } 

이 플래그를 사용하여 어댑터 내부의 확인란을 설정하십시오.

관련 문제