15

olarak adlandırılmıyor. Android geliştirmede çok yeniyim. Etiketler içeren bir uygulama oluşturmaya çalışıyorum (bunlar parçalara eklenmiştir). Parçalardan birinde bir liste görüntülemeye çalışıyorum. Bu liste ArrayAdapter'ten genişletilmiş ListAdapter kullanılarak dolduruldu ve ben aşırı miktarda getView() yöntemi var. Bu benim parçasıArrayAdapter'ın getView() yöntemi,

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; 
    } 
} 

olduğunu Ve bu 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 [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; 
    } 
} 

uygulayan nasıl düzen xml bir yol bulmak için bana yardım edin

<?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> 

olan elimden tarafından Bu işi yap.

cevap

48

Sadece kurucusuna veri sağlamak ve daha sonra yapıcı içerisinde doğru arama yapmak unuttum. Bu bana yardımcı oldu ....!
+2

OMG teşekkür: – jonassvensson

+0

'süper (içerik, viewResourceId, items);' bu da benim için çalıştı –

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

takas pozisyonları

Bağdaştırıcınız için bir veri kaynağı belirtmediniz gibi görünüyor
Layoutinflator=getLayoutInflator(); 
2

. Bir veri kaynağı, bir arraylist veya bir imleç için görüntülenecek veri sağlayan herhangi bir şeydir. Bir veri kaynağından veri yoksa, getview() hiç çağrılmayacaktır.

16

Şu anda ona herhangi bir veri sağlamıyorsunuz. Listede neler olup bittiğini hala görmek istiyorsanız, getCount() işlevini geçersiz kılın.

Bu tür bir şey olduğunu düşünüyorum.

public int getCount(){ 
    return 1; 
} 

Bu, listenizden bir öğe çizecektir. Ayrıca getView() öğesini de arayın. Ve veri kaynağınızı nasıl temin edeceğinizi öğrendiğinizde, getCount() öğesini listenizin büyüklüğüne göre değiştirin. Biz eğer biz ya

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) 

kullanıyor olmalısınız arrayadapter GetCount yöntemi bir özel oluştururken

public int getCount(){ 
    return 100; 
    } 
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; 
} 

göz atın ve geçersiz Yukarıda belirtilen adaptör kullanılmıyorsa, getCount() yöntemini geçersiz kılmamız gerekir.

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