15

Tôi rất mới phát triển Android. Tôi đang cố gắng xây dựng một ứng dụng với các thẻ (được thêm vào thông qua các đoạn). Trong một trong những mảnh vỡ, tôi đang cố gắng hiển thị một danh sách. Danh sách này đã được phổ biến bằng cách sử dụng ListAdapter mà tôi đã mở rộng từ ArrayAdapter và tôi đã quá tải phương thức getView(). Đây là đoạn tôiPhương thức getView() của ArrayAdapter không được gọi là

public class tabcontentActivity extends Fragment { 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
     if (container == null) { 
      return null; 
     } 
     View v = (LinearLayout) inflater.inflate(R.layout.tablayout, container, 
      false); 
     ListView lv = (ListView) v.findViewById(R.id.listview1); 
     ListViewAdapter adapter = new ListViewAdapter(container.getContext(), 
      android.R.layout.simple_list_item_1, R.id.textview1); 
     adapter.notifyDataSetChanged(); 
     lv.setAdapter(adapter); 

     return v; 
    } 
} 

Và đây là cách tôi đã triển khai ListAdapter

public class ListViewAdapter extends ArrayAdapter { 
    Context context1; 

    public ListViewAdapter(Context context,int resource, int textViewResourceId) { 
     super(context,resource,textViewResourceId); 
     this.context1 = context; 
     System.out.println("here aswell!!!!!!"); 
     // TODO Auto-generated constructor stub 
    } 

    public View getView(int arg0, View convertView, ViewGroup arg2) { 
     // TODO Auto-generated method stub 
     System.out.println("@@@I AM H[email protected]@@"); 
     LayoutInflater inflater = LayoutInflater.from(context1); 
     convertView = inflater.inflate(R.layout.tablayout, null); 

     TextView wv = (TextView) convertView.findViewById(R.id.textview1); 
     String summary = "<html><body><h1>This is happening!!!</h1></body></html>"; 
     wv.setText(Html.fromHtml(summary)); 

     convertView.setTag(wv); 

     return convertView; 
    } 
} 

Các xml bố trí là

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@+id/listview1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 
    </ListView> 

    <TextView 
     android:id="@+id/textview1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:text="myreader" /> 

</LinearLayout> 

Xin hãy giúp tôi để tìm một cách nào để tôi có thể thực hiện công việc này.

Trả lời

48

Bạn chỉ cần quên để cung cấp các dữ liệu để các nhà xây dựng và sau đó thực hiện cuộc gọi đúng trong constructor:

public ListViewAdapter(Context context,int resource, int textViewResourceId, List<YourObjectType> items) { 
    super(context,resource,textViewResourceId, items); 
    this.context1 = context; 
    // TODO Auto-generated constructor stub 
} 
+2

Omg nhờ. Điều này đã giúp tôi ....! – jonassvensson

+0

'super (context, viewResourceId, items);' cái này cũng làm việc cho tôi –

1
adapter.notifyDataSetChanged(); 
    lv.setAdapter(adapter); 

hoán đổi vị trí

Layoutinflator=getLayoutInflator(); 
2

có vẻ như bạn đã không được xác định một nguồn dữ liệu cho bộ chuyển đổi của bạn. Nguồn dữ liệu là bất kỳ thứ gì cung cấp dữ liệu được hiển thị, ví dụ như một danh sách mảng hoặc một con trỏ. Nếu không có dữ liệu từ nguồn dữ liệu, getview() sẽ không được gọi.

16

Bạn hiện không cung cấp bất kỳ dữ liệu nào cho dữ liệu đó. Nếu bạn vẫn muốn xem những gì đang xảy ra trong danh sách, hãy ghi đè lên hàm getCount().

Tôi nghĩ rằng đó là thứ gì đó thuộc loại này.

public int getCount(){ 
    return 1; 
} 

Điều này sẽ vẽ một mục trong danh sách của bạn. Và cũng gọi getView của bạn(). Và khi bạn tìm hiểu cách cung cấp nguồn dữ liệu, hãy thay đổi getCount() thành kích thước của danh sách bạn. Kiểm tra một hướng dẫn ở đây

http://www.shubhayu.com/android/listview-with-arrayadapter-and-customized-items

0
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    if (container == null) { 
     View v = (LinearLayout) inflater.inflate(R.layout.tablayout, container, 
       false); 
     ListView lv = (ListView) v.findViewById(R.id.listview1); 
    ListViewAdapter adapter = new ListViewAdapter(container.getContext(), 
     android.R.layout.simple_list_item_1, R.id.textview1); 
    adapter.notifyDataSetChanged(); 
    lv.setAdapter(adapter); 
    } 
    else{ 
    View v = (LinearLayout) inflater.inflate(R.layout.tablayout, container, 
     false); 
     ListView lv = (ListView) v.findViewById(R.id.listview1); 
    ListViewAdapter adapter = new ListViewAdapter(container.getContext(), 
     android.R.layout.simple_list_item_1, R.id.textview1); 
    adapter.notifyDataSetChanged(); 
    lv.setAdapter(adapter); 
} 
return v; 
} 

và ghi đè phương pháp getCount

public int getCount(){ 
    return 100; 
    } 
6

Khi chúng tôi đang tạo ra một tùy chỉnh arrayadapter chúng ta phải sử dụng một trong hai

public ArrayAdapter (Context context, int textViewResourceId, T[] objects) 

public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects) 

public ArrayAdapter (Context context, int textViewResourceId, List<T> objects) 

public ArrayAdapter (Context context, int resource, int textViewResourceId, List<T> objects) 

nếu chúng ta không sử dụng ở trên đề cập đến adapter, chúng ta cần phải ghi đè lên phương thức getCount().

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