2015-01-31 3 views
-1

안녕하세요! 간단한 쇼핑 목록 응용 프로그램 (사용자 지정 행이있는 ListView, 사용자 지정 어댑터). 그냥 유명한 listview 체크 박스 스크롤 문제를 다뤘다.사용자 지정 어댑터가있는 ListView. 데이터 저장 중

올바른 viewHolder 구현을 위해 내 코드를 확인하고 데이터를 저장하는 가장 쉬운 방법을 찾도록 도와주세요. (후에들의 OnDestroy())

어댑터 :

public class ShopAdapter extends BaseAdapter { 
private Context mainContex; 
private ArrayList<ShopItem> shopItems; 

static class ViewHolder { 
    CheckBox checkBox; 
    TextView textView; 
} 

public ShopAdapter(Context mainContex, ArrayList<ShopItem> shopItems) { 
    this.mainContex = mainContex; 
    this.shopItems = shopItems; 
} 

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

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

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

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    final ShopItem shopItem = shopItems.get(position); 
    final ViewHolder viewHolder; 
    if (convertView == null) { 
     viewHolder = new ViewHolder(); 

     LayoutInflater layoutInflater = LayoutInflater.from(mainContex); 
     convertView = layoutInflater.inflate(R.layout.shoplist_item, null); 

     viewHolder.textView = (TextView) convertView.findViewById(R.id.itemTextView); 
     viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.doneCheckBox); 

     convertView.setTag(viewHolder); 

    } else { 
     viewHolder = (ViewHolder) convertView.getTag(); 
    } 
    viewHolder.checkBox.setTag(shopItems.get(position)); 
    viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if (isChecked) { 
       shopItem.setDone(true); 
       viewHolder.textView.setTextColor(mainContex.getResources() 
         .getColor(R.color.done_text_color)); 
      } else { 
       shopItem.setDone(false); 
       viewHolder.textView.setTextColor(mainContex.getResources() 
         .getColor(R.color.secondary_text)); 
      } 
     } 
    }); 
    viewHolder.textView.setText(shopItem.getDescription()); 
    viewHolder.checkBox.setChecked(shopItem.isDone()); 
    return convertView; 
} 

}

항목 :

public class ShopItem { 

private String description; 
private boolean done; 

public String getDescription() { 
    return description; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 

public boolean isDone() { 
    return done; 
} 

public void setDone(boolean done) { 
    this.done = done; 
} 

} 당신이 사용해야리스트 뷰에 데이터 저장 거 경우

답변

관련 문제