15

私は非常に新しいアンドロイド開発です。私は、タグを使ってアプリケーションをビルドしようとしています(フラグメントから追加されています)。断片の一つで、私はリストを表示しようとしています。このリストは、ArrayAdapterから拡張したListAdapterを使用して作成され、getView()メソッドをオーバーロードしています。 私ができることで方法を見つけるために私を助けてくださいこれは私のフラグメントArrayAdapterのgetView()メソッドが呼び出されていません

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

であり、これは私が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; 
    } 
} 

を実装したレイアウトXMLは

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

ある方法ですこの作品を作る。

答えて

48

は、あなただけのコンストラクタにデータを提供し、その後、コンストラクタ内の正しい呼び出しを行うのを忘れていました。これは私を助けた....!
+2

すっごい感謝: – jonassvensson

+0

'super(context、viewResourceId、items);'これも私のために働いた –

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

スワップポジション

Layoutinflator=getLayoutInflator(); 
2

あなたのアダプタのデータソースを指定していないように見えます。データソースは、arraylistまたはカーソルのために、表示されるデータを提供するものです。データソースからのデータがない場合、getview()はまったく呼び出されません。

16

あなたは現在、それにいかなるデータも提供していません。リスト内で何が起こっているかを見たい場合は、getCount()関数をオーバーライドします。

私はそれがこの種のものだと思います。

public int getCount(){ 
    return 1; 
} 

これはあなたのリストに1つの項目を表示します。また、getView()を呼び出します。そして、あなたはあなたのデータソースを提供する方法を見つけたら、getCount()をリストのサイズに変更してください。私たちがしている場合、我々はどちらか

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) 

を使用している必要がありarrayadapter GETCOUNT方法我々は、カスタムを作成している

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

をチェックアウトし、上書き上記のアダプタを使用しないで、getCount()メソッドをオーバーライドする必要があります。

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