2012-01-29 9 views
6

以前は、レイアウトビューをListViewのカスタムビュー階層として拡張しても問題はありませんでした。しかし、私はどのようにlistFragmentのために同じことをするかについての手掛かりを持っていません。 ImageViewと2つのtextviewsを持つitem_listレイアウトがあるとします。私はこれを私のListFragmentで使うために膨らませたい。しかし、どのように?ListFragmentsにカスタム表示を設定しますか?

public class Tab1Fragment extends ListFragment { 
private LayoutInflater mInflater; 
private Vector<RowData> data; 
RowData rd; 

static final String[] title = new String[] { 
    "Classic Plank", "Side Plank", "Reversed Plank", "Swissball Plank", "One Arm One Leg", //5 
    }; 

static final String[] description = new String[] { 
    "Variation of Sushis from fresh vegetables and seafoods! Good for family occassions!", 
    "Oyakodon is a Japanese Rice Bowl dish with Chicken, Eggs and various sorts of healthy and delicious Veggetables!", 
    "Japanese assorted Pancake that is made from many different ingredients. The taste is so delicious!", 
    "Japanese Dumplings made of Rice Flour. This is one of the healthiest sweets in Japan!", 
    "Japanese assorted stews. Made from many different kind of veggetables. Popular in Winter!", //5 
    }; 

private Integer[] imgid = { 
R.drawable.thumb_classic, R.drawable.thumb_side, R.drawable.thumb_reverse,  R.drawable.thumb_swissball, R.drawable.thumb_reachout, //5 
}; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.easylist); 
mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
data = new Vector<RowData>(); 
for(int i=0;i<title.length;i++){ 
    try { 
     rd = new RowData(i,title[i],description[i]); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
      } 
    data.add(rd); 
    } 
CustomAdapter adapter = new CustomAdapter(this, R.layout.list_item_main, android.R.id.list, data); 
    setListAdapter(adapter); 
    getListView().setTextFilterEnabled(true); 
} 


private void setContentView(int main) { 
// TODO Auto-generated method stub 

} 


private LayoutInflater getSystemService(String layoutInflaterService) { 
// TODO Auto-generated method stub 
return null; 
} 


public void onListItemClick(ListView listView, View view, int position, long id) { 
Bundle bundle = new Bundle(); 
bundle.putInt("Layout", position); 

Intent newIntent = new Intent(this.getApplicationContext(), ContentViewer.class); 
newIntent.putExtras(bundle); 
startActivityForResult(newIntent, 0); 
} 
    private Context getApplicationContext() { 
// TODO Auto-generated method stub 
return null; 
} 
private class RowData { 
    protected int mId; 
    protected String mTitle; 
    protected String mDescription; 
    RowData(int id, String title, String description){ 
    mId=id; 
    mTitle = title; 
    mDescription = description; 
} 
    @Override 
    public String toString() { 
      return mId+" "+mTitle+" "+mDescription; 
    } 
} 
    private class CustomAdapter extends ArrayAdapter<RowData> { 

    public CustomAdapter(Context context, int resource, int textViewResourceId, List<RowData> objects) {    

super(context, resource, textViewResourceId, objects); 
} 
@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

    ViewHolder holder = null; 
    TextView title = null; 
    TextView description = null; 
    ImageView thumbnail = null; 
    RowData rowData = getItem(position); 
    if(null == convertView){ 
     convertView = mInflater.inflate(R.layout.list_item_main, null); 
     holder = new ViewHolder(convertView); 
     convertView.setTag(holder); 
} 
     holder = (ViewHolder) convertView.getTag(); 
     title = holder.gettitle(); 
     title.setText(rowData.mTitle); 

     description = holder.getdescription(); 
     description.setText(rowData.mDescription); 

     thumbnail = holder.getImage(); 
     thumbnail.setImageResource(imgid[rowData.mId]); 
     return convertView; 
} 
     private class ViewHolder { 
     private View mRow; 
     private TextView title = null; 
     private TextView description = null; 
     private ImageView thumbnail = null; 

     public ViewHolder(View row) { 
     mRow = row; 
} 
    public TextView gettitle() { 
     if(null == title){ 
      title = (TextView) mRow.findViewById(R.id.title); 
      } 
     return title; 
    }  

    public TextView getdescription() { 
     if(null == description){ 
       description = (TextView) mRow.findViewById(R.id.description); 
       } 
     return description; 
    } 

    public ImageView getImage() { 
     if(null == thumbnail){ 
       thumbnail = (ImageView) mRow.findViewById(R.id.thumbnail); 
            } 
      return thumbnail; 
    } 
}    
} 
} 
+0

これはまったく同じです.ListViewのようにアダプタを使用し、独自のレイアウトを拡張します。 –

+0

@DavidCauntアダプターでエラーが発生しました。私は、アプリケーションのリストビューフォームを移植し、 "extends ListActivity"を "ListFragmentを拡張"に変更しました。アダプタは未定義です。助けて??? コードを掘り下げるhttp://dl.dropbox.com/u/33331786/Errors/listfragment.txt – borislemke

+0

この行に: CustomAdapter adapter =新しいCustomAdapter(this、R.layout.list_item_main、android.R.id.list 、データ)。 \t setListAdapter(adapter); \t getListView()。setTextFilterEnabled(true); \t} – borislemke

答えて

1

これは、私はそれを持っていたとき、この問題を解決するものであるならば、私は確かに覚えていることはできませんが、私はあなたが正しく(here見て)フラグメントのライフサイクルに従っているとは思いません。

アクティビティについて話すことのコンテキストでは、setAdapter()のようなコールを行う前に、まずListViewを作成する必要があります。 onCreateView()より前に呼び出されたonCreate()に実装しています。

したがって、あなたが作成する前にあなたのListViewのアダプターを設定しようとしているようです。

習慣として、フラグメントライフサイクルのonCreateView()部分内のビューですべての作業を行うのが常にベストです。

関連する問題