안드로이드 개발 질문/답변
(글 수 45,052)
리스트 뷰를 simpleadapter로 이용해서 구현하였는데... 체크박스를 선택하고 버튼을 클릭하면
그 리스트뷰 줄에있는 데이타가 텍스트뷰에 출력되게 만든 소스입니다.
아 래 링크 클릭하시면 구현한 이미지가 나오고 문제 이미지가 나오는데요.(여기에 그림올리니 다 짤려서 ㅠ)
http://blog.naver.com/liesdo/memo/10085844477
설명하면, 눈에보이는 리스트뷰에있는 데이타는 인식하는데...
스크롤뷰로 아래 내려서 보면, 인식하지 못하는거에요..
도움 부탁드려요
//====================== 소스 부분 =============================
public class CheckList extends ListActivity {
public ArrayList<RowItem> arrayItems = new ArrayList<RowItem>();
private MyArrayAdapter notes;
private Button select;
private CheckBox checkbox;
private EditText et;
private ListView lv;
private TextView tv;
public void onListItemClick(ListView l, View v, int position, long id) {
tv = (TextView)findViewById(R.id.ttt);
tv.setText(Integer.toString(position));
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.premain);
select = (Button)findViewById(R.id.selectcancelbutton);
checkbox =(CheckBox)findViewById(R.id.CheckBox);
String[] paper_name = getResources().getStringArray(R.array.paper_name);
String[] paper_explain = getResources().getStringArray(R.array.paper_explain);
String[] paper_time = getResources().getStringArray(R.array.paper_time);
for (int i = 0; i < paper_name.length; i++) {
arrayItems.add(new RowItem(paper_name[i],paper_explain[i],paper_time[i]));
}
select.setOnClickListener(new OnClickListener(){ // 이부분에서 문제가 있는 듯싶거나 getView()인가 ....
public void onClick(View v) {
int j = getListView().getChildCount();
for(int i =0; i < j; i++){
checkbox = (CheckBox)getListView().getChildAt(i).findViewById(R.id.CheckBox);
tv = (TextView)findViewById(R.id.ttt);
if(checkbox.isChecked()){
String s =arrayItems.get(i).toString();
tv.setText(s);
}
}
}
});
ListfillData(); //ListView 에 데이타 집어 넣기
notes.notifyDataSetChanged();
}
private void ListfillData() {
// TODO Auto-generated method stub
String[] from =
new String[]{RowItem.ROW_TEXT_1, RowItem.ROW_TEXT_2, RowItem.ROW_TEXT_3,};//여기에 있는데이타를
int[] to = new int[] {R.id.text1,R.id.text2, R.id.text3};//여기에 집어넣기
notes = new MyArrayAdapter(this,
arrayItems, R.layout.rows, from, to); //listView에 들어갈 데이타들
setListAdapter(notes); //리스트와 데이터를 연결한 것을 화면에 보여줌.
}
class MyArrayAdapter extends SimpleAdapter{
// 사용자 정의 Adapter을 이용하여 리스트의 바탕 배경화면을 설정해주고 있다.
private Context context;
public MyArrayAdapter(Context context,
List<? extends Map<String, ?>> data, int resource,
String[] from, int[] to) {
super(context, data, resource, from, to);
this.context = context;
data = arrayItems;
// TODO Auto-generated constructor stub
}
public View getView(int position, View convertView, ViewGroup parent){
View view = super.getView(position, convertView, parent);
if(view == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.rows, null);
}
if(position%2 == 0){
view.setBackgroundResource(R.drawable.list_backgroundfirst);
}
else{
view.setBackgroundResource(R.drawable.list_backgroundsecond);
}
return view;
}
}
}
2010.05.08 20:59:17
(추천:
1 / 0)
문제의 핵심은요 listview 에 getChildAt(k) 을 호출하면, 현재 눈에 보이는 뷰 들 중에 위에서 부터 k번째
자식뷰를 얻어옵니다. 그래서 스크롤을 한 경우 문제가 생깁니다. list 에보면 getFirstVisiblePosition()
인가 하는 함수가 있는데, 이게 현재 스크롤 상태에서 getChildAt(0) 했을 때 얻어지는 뷰가, 전체 데이터 중에 몇번째
아이템을 표현하는 뷰인지를 넘겨줍니다. 요걸 잘 이용하시면 위 문제 해결되실겁니다.



