2011-10-31 2 views
1

목록 행에 별도의 항목을 표시하려고합니다. 그래서 나는 4 textviews를 사용합니다. 하지만 나는 Activity 클래스를 확장하기 위해 arrayadapter를 사용한다. 이제 해당 텍스트 뷰에 따라 행 목록에 문자열 목록을 넣어야합니다. 이것을 달성하는 방법? 어떤 도움을 주셔서 감사 드리며 미리 감사드립니다 ...ArrayAdapter에서 두 개 이상의 textview를 사용하여 listview를 사용하는 방법은 무엇입니까?

답변

8

일반적으로 나는 ArrayAdapter을 연장 할 예정입니다. 일반적으로 어댑터에서 getView()와 생성자 중 하나의 함수 만 재정의해야합니다.

어댑터의 코드는 다음입니다 : 대신 ArrayAdapter와의

/** class to act as list adapter for rows List */ 
private static class FourTextListAdapter extends ArrayAdapter<MyDataClass> { 

    /** To cache views of item */ 
    private static class ViewHolder { 
     private TextView text1; 
     private TextView text2; 
     private TextView text3; 
     private TextView text4; 

     /** 
     * General constructor 
     */ 
     ViewHolder() { 
      // nothing to do here 
     } 
    } 

    /** Inflater for list items */ 
    private final LayoutInflater inflater; 

    /** 
    * General constructor 
    * 
    * @param context 
    * @param resource 
    * @param textViewResourceId 
    * @param objects 
    */ 
    public FourTextListAdapter(final Context context, 
      final int resource, 
      final int textViewResourceId, 
      final List<User> objects) { 
     super(context, resource, textViewResourceId, objects); 

     this.inflater = LayoutInflater.from(context); 
    } 

    @Override 
    public View getView(final int position, final View convertView, final ViewGroup parent) { 

     View itemView = convertView; 
     ViewHolder holder = null; 
     final MyDataClass item = getItem(position); 

     if(null == itemView) { 
      itemView = this.inflater.inflate(R.layout.four_texts_item, parent, false); 

      holder = new ViewHolder(); 

      holder.text1 = (TextView)itemView.findViewById(R.id.text1); 
      holder.text2 = (TextView)itemView.findViewById(R.id.text2); 
      holder.text3 = (TextView)itemView.findViewById(R.id.text3); 
      holder.text4 = (TextView)itemView.findViewById(R.id.text4); 

      itemView.setTag(holder); 
     } else { 
      holder = (ViewHolder)itemView.getTag(); 
     } 

     holder.text1.setText(item.getText1()); 
     holder.text2.setText(item.getText2()); 
     holder.text3.setText(item.getText3()); 
     holder.text4.setText(item.getText4()); 

     return itemView; 
    } 
} 
+1

+1이 사실에 동의하고 BaseAdapter도 확장 할 수 있습니다. –

+0

내 케이스에서는 BaseAdapter와 ArrayAdapter 중 하나를 선택해야합니다. http://stackoverflow.com/questions/10617093/listview-and-arrayadapter-issue-how-do-i-proceed – aProgrammer

4

을 SimpleAdapter를 사용하여보십시오. 위의 코드에서

ArrayList<HashMap<String, String>> sampleArrayList; 
SimpleAdapter sampleListAdapter; 

HashMap<String, String> sampleObjectMap; 

for (SampleObject sampleObj : sampleList) { 
    sampleObjectMap= new HashMap<String, String>(); 
     sampleObjectMap.put("value1", sampleObj.getValue1()); 
     sampleObjectMap.put("value2", sampleObj.getValue2()); 
    sampleObjectMap.put("value3", sampleObj.getValue3()); 
    sampleObjectMap.put("value4", sampleObj.getValue4()); 

     sampleArrayList.add(sampleObjectMap); 
} 

sampleListAdapter= new SimpleAdapter(
      context, sampleArrayList, 
      R.layout.custom_list_layout, new String[] { 
        "value1", "value2" , "value3", "value4"}, 
        new int[] { R.id.list_content_column1, 
          R.id.list_content_column2, 
          R.id.list_content_column3, 
          R.id.list_content_column4}); 

     sampleListView.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 

       HashMap<String, String> sampleObjectMapLocal = sampleArrayList 
            .get(position); 
       final String value1Obj = sampleObjectMapLocal          .get("value1"); 

       System.out.println("value1Obj : " + value1Obj); 

      } 
     }); 

리스트 뷰 컨텐츠 ArrayList를 sampleArrayList 등 모든 항목에서 항목 ("VALUE1", "값 2", "VALUE3을"키를 사용하여 액세스 할 수있다 "VALUE4을 사용하여 채워 ").

희망이 도움이 될 것입니다.

관련 문제