2013-03-08 45 views
19

Tôi có tính năng hoạt động sẽ hiển thị danh sách tên và địa chỉ người có dữ liệu từ danh sách đối tượng. đây là phương pháp để điền vào listview cho đến nay ..Nhập danh sách xem danh sách từ danh sách đối tượng

private void fillData(ArrayList<Person> messages) { 
    //populating list should go here 
} 

Lớp người đó là cửa hàng tên và địa chỉ của một người.

public class Person { 
    String name; 
    String address; 
} 

Trong khi listitem tôi cho listview của tôi bao gồm hai TextView, như thế này:

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:minHeight="?android:attr/listPreferredItemHeight" 
android:mode="twoLine" 
android:clickable="false" 
android:paddingBottom="9dp" 
android:paddingTop="5dp" > 

<TextView 
    android:id="@+id/display_name" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10dp" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<TextView 
    android:id="@+id/display_number" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/display_name" 
    android:layout_below="@+id/display_name" 
    android:textAppearance="?android:attr/textAppearanceSmall" /> 

Tôi muốn mỗi mục trong listview của tôi để hiển thị cả tên và địa chỉ của từng người. Ai đó có thể giúp tôi không?

Cảm ơn trước, và xin lỗi vì tiếng Anh xấu của tôi ...

+0

Tìm kiếm trong danh sách tùy chỉnh googleview ... – Pragnani

+0

Bạn có thể sử dụng phương thức 'onCreate'. Trong trường hợp này, nó có thể sử dụng được trong chương trình. – user7275835

Trả lời

25

Trong hoạt động của bạn

AdapterPerson adbPerson; 
ArrayList<Person> myListItems = new ArrayList<Person>(); 

//then populate myListItems 

adbPerson= new AdapterPerson (youractivity.this, 0, myListItems); 
listview.setAdapter(adbPerson); 

Adaptor

public class AdapterPerson extends ArrayAdapter<Person> { 
    private Activity activity; 
    private ArrayList<Person> lPerson; 
    private static LayoutInflater inflater = null; 

    public AdapterPerson (Activity activity, int textViewResourceId,ArrayList<Person> _lPerson) { 
     super(activity, textViewResourceId, _lProducts); 
     try { 
      this.activity = activity; 
      this.lPerson = _lPerson; 

      inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     } catch (Exception e) { 

     } 
    } 

    public int getCount() { 
     return lPerson.size(); 
    } 

    public Product getItem(Product position) { 
     return position; 
    } 

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

    public static class ViewHolder { 
     public TextView display_name; 
     public TextView display_number;    

    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi = convertView; 
     final ViewHolder holder; 
     try { 
      if (convertView == null) { 
       vi = inflater.inflate(R.layout.yourlayout, null); 
       holder = new ViewHolder(); 

       holder.display_name = (TextView) vi.findViewById(R.id.display_name); 
       holder.display_number = (TextView) vi.findViewById(R.id.display_number); 


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



      holder.display_name.setText(lProducts.get(position).name); 
      holder.display_number.setText(lProducts.get(position).number); 


     } catch (Exception e) { 


     } 
     return vi; 
    } 
} 
+0

bằng cách này, tại sao tham số thứ 2 của AdapterPerson là 0? – djargonforce

2

Đây là giải pháp của tôi để các tùy chỉnh vấn đề bộ điều hợp danh sách. Lần đầu tiên một arrayadapter tùy chỉnh:

public class MemoListAdapter extends ArrayAdapter<MemoEntry> { 

private int layoutResourceId; 

private static final String LOG_TAG = "MemoListAdapter"; 

public MemoListAdapter(Context context, int textViewResourceId) { 
    super(context, textViewResourceId); 
    layoutResourceId = textViewResourceId; 
} 


@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    try { 
     MemoEntry item = getItem(position); 
     View v = null; 
     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      v = inflater.inflate(layoutResourceId, null); 

     } else { 
      v = convertView; 
     } 

     TextView header = (TextView) v.findViewById(R.id.list_memo_item_header); 
     TextView description = (TextView) v.findViewById(R.id.list_memo_item_text); 

     header.setText(item.getHeader()); 
     description.setText(item.getValue()); 

     return v; 
    } catch (Exception ex) { 
     Log.e(LOG_TAG, "error", ex); 
     return null; 
    } 
} 

}

và trong phương thức onCreate của hoạt động:

và sau đó tôi điền adapter sau khi một số lấy với một cuộc gọi như thế này:

ArrayList<MemoEntry> memoList = new ArrayList<MemoEntry>(); //here you should use a list with some content in it 
adapter.addAll(memoList); 

Vì vậy, để thích ứng với giải pháp của bạn, hãy tạo của riêng bạn customadapter, và thay vì đối tượng MemoEntry của tôi, sử dụng lớp Person của bạn. Trong phương thức getView, hãy thay đổi nó cho phù hợp với nhu cầu của bạn. Đó là về giống như những gì tôi đang làm như vậy không nên quá khó.

Hy vọng điều này sẽ giúp bạn một chút!

Các vấn đề liên quan