2011-10-05 2 views
1

listviewarraylist을 하나의 어댑터로 병합하려고합니다. getView() 방법의 위치 값을 사용해 보았지만 정확한 데이터를 listview에 표시하지 마십시오. arrayList 두 번째 데이터를 완료 한 후 데이터를 표시하십시오. arraylist이 표시되기 시작해야합니다.두 개의 다른 ArrayList를 하나의 접합 자에 추가합니다.

ArrayList<A> list1 = new ArrayList<A>; 
ArrayList<B> list2 = new ArrayList<B>; 

내가 목록 1 후 목록 1을 추가 할 필요가 adapter.first 하나에이 두 개의 목록을 추가 할 수는리스트 2에서 전체 데이터가 getView() 방법을 위치 변수를 사용하여 시도 display.i입니다 그러나 그것은 나에게 올바른 데이터를 표시하지 .

어떻게 이것을 수행할까요?

답변

2

이 어댑터를 본 일반적인 목록을 하나 개의 목록에 모두 목록을 추가하고 설정하여 달성 될 수있다.

ArrayList<A> list1 = new ArrayList<A>(); 
ArrayList<Object> object = new ArrayList<Object>(); 
list1.add(new A()); 
object.add(new A()); 
ArrayList<B> list2 = new ArrayList<B>(); 
list2.add(new B()); 
object.add(new B()); 
object.add(new B()); 
listadapter mAdapterSeeLatest ; 
mAdapterSeeLatest = new listadapter (getParent(),R.layout.help_row,object); 
mCheckInListView.setAdapter(mAdapterSeeLatest); 
private class listadapter extends ArrayAdapter { 

    ArrayList<Object> ListObject; 
    int srNumber = 1; 
    public listadapter (Context context, int textViewResourceId, 
       ArrayList<Object> objects) { 
     super(context, textViewResourceId, objects); 
     ListObject = objects; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if (v == null) 
     { 
      LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.row, null);  
     } 
     if(ListObject.get(position) instanceof A) 
     { 

     } 
     else if(ListObject.get(position) instanceof B) 
     { 
     } 
    } 

//now set this object ArrayList() into the List. 
+0

그것은 나를 위해 일했습니다. 솔루션 megha 주셔서 감사합니다 ... – praveenb

1

그냥 두 개를 하나의 ArrayList로 결합 할 수 없습니까?

List<String> newList = new ArrayList<String>(listOne); 
newList.addAll(listTwo); 


이 항목을 참조하십시오 : How do I join two lists in Java?

관련 문제