2012-01-29 7 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 어댑터에 오류가 발생합니다. 내 애플 리케이션 중 하나를 listview 양식을 포팅하고 "extends ListActivity"를 "ListFragment를 확장"으로 변경했습니다. 어댑터가 정의되지 않았다고합니다. 도움??? 코드를 작성합니다. http://dl.dropbox.com/u/33331786/Errors/listfragment.txt – borislemke

+0

다음 행에 CustomAdapter adapter = new 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() 내의보기를 사용하여 모든 작업을 수행하는 것이 좋습니다.

관련 문제