2013-07-16 59 views
0

首先,我很抱歉我的壞英語,也許,我的愚蠢問題(我是新手)。 我想實現一個格式化爲標題的選項面板。 所以在這裏我的代碼:Android:自定義列表適配器圖像,文本,子文本和複選框

listview_item_row.xml

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dp"> 

    <ImageView 
     android:id="@+id/imgIcon" 
     android:contentDescription="option image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/txtText" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_toRightOf="@id/imgIcon" 
     android:paddingLeft="15dp" 
     android:textSize="20sp" /> 

    <TextView 
     android:id="@+id/txtSubText" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_toRightOf="@id/imgIcon" 
     android:layout_below="@id/txtText" 
     android:paddingLeft="15dp" 
     android:textSize="14sp" /> 

    <CheckBox 
     android:id="@+id/checkBox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/txtText" /> 

</RelativeLayout> 

Row.java

public class Row { 
    public int icon; 
    public String text; 
    public String subtext; 
    public Row(){ 
     super(); 
    } 

    public Row(int icon, String text ,String subtext) { 
     super(); 
     this.icon = icon; 
     this.text = text; 
     this.subtext = subtext; 
    } 
} 

RowAdapter.java

public class RowAdapter extends ArrayAdapter<Row>{ 

    Context context; 
    int layoutResourceId;  
    Row data[] = null; 

    public RowAdapter(Context context, int layoutResourceId, Row[] data) { 
     super(context, layoutResourceId, data); 
     this.layoutResourceId = layoutResourceId; 
     this.context = context; 
     this.data = data; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View myRow = convertView; 
     RowHolder holder = null; 

     if(myRow == null) 
     { 
      LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
      myRow = inflater.inflate(layoutResourceId, parent, false); 

      holder = new RowHolder(); 
      holder.imgIcon = (ImageView)myRow.findViewById(R.id.imgIcon); 
      holder.txtText = (TextView)myRow.findViewById(R.id.txtText); 
      holder.txtSubText = (TextView)myRow.findViewById(R.id.txtSubText); 
      holder.checkBox = (CheckBox)myRow.findViewById(R.id.checkBox); 

      myRow.setTag(holder); 
     } 
     else 
     { 
      holder = (RowHolder)myRow.getTag(); 
     } 

     Row row = data[position]; 
     holder.txtText.setText(row.text); 
     holder.imgIcon.setImageResource(row.icon); 
     holder.txtSubText.setText(row.subtext); 

     return myRow; 
    } 

    static class RowHolder 
    { 
     ImageView imgIcon; 
     TextView txtText; 
     TextView txtSubText; 
     CheckBox checkBox; 
    } 
} 

現在我沒有顯示任何複選框,當我開始活動,即使我已經膨脹它。我怎樣才能解決這個問題?

此外,如果我可以利用這個問題,當我必須實現複選框的onClickListener時,我怎麼理解哪個複選框被點擊? 我會做這樣的事情:

... 
Row row = data[position]; 
holder.txtText.setText(row.text); 
holder.imgIcon.setImageResource(row.icon); 
holder.txtSubText.setText(row.subtext); 
holder.check..setOnCheckedChangeListener(new OnCheckedChangeListener() 
{ 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
    { 
     if (isChecked) 
     { 
      // my code here 
     } 

    } 
}); 
... 

我認爲「位置」是關鍵,但我不知道我該如何使用它。

回答

0

這是問題的開始!

我用它和它的作品

public class ImageAdapter extends BaseAdapter { 
    private LayoutInflater mInflater; 

    public ImageAdapter() { 
     //Largeur des colonnes 
     LargeurCol = (int) ((largeurEcran/4)-7); 
     System.out.println(LargeurCol); 
     mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public int getCount() { 
     return count; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 
      holder = new ViewHolder(); 
      convertView = mInflater.inflate(R.layout.galleryitem, null); 
      holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage); 
      holder.imageview.setLayoutParams(new RelativeLayout.LayoutParams(LargeurCol, LargeurCol)); 
      holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox); 
      holder.checkbox.setButtonDrawable(R.drawable.checkbox); 
      convertView.setTag(holder); 
     } 
     else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     holder.checkbox.setId(position); 
     holder.imageview.setId(position); 
     holder.checkbox.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       CheckBox cb = (CheckBox) v; 
       int id = cb.getId(); 
       if (thumbnailsselection[id]){ 
        cb.setChecked(false); 
        thumbnailsselection[id] = false; 
       } else { 
        cb.setChecked(true); 
        thumbnailsselection[id] = true; 
       } 
      } 
     }); 
     holder.imageview.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       int id = v.getId(); 
       Intent intent = new Intent(); 
       intent.setAction(Intent.ACTION_VIEW); 
       intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*"); 
       startActivity(intent); 
      } 
     }); 
     holder.imageview.setImageBitmap(thumbnails[position]); 
     holder.checkbox.setChecked(thumbnailsselection[position]); 
     holder.id = position; 
     return convertView; 
    } 
} 

class ViewHolder { 
    ImageView imageview; 
    CheckBox checkbox; 
    int id; 
} 

的setLayoutParams是不是對行大小適應屏幕的義務。

+0

仍然沒有工作,我試圖重新編寫所有我的代碼實現「BaseAdapter」,因爲你在代碼中建議,但我只是一個只有標題行可見的白色屏幕。我也使用getSystemService(..)代替getLayoutInflater()來修改我的代碼(這是我在2個代碼之間看到的唯一區別),但結果相同:所有工作正常,但checkBox不會出現。 –

+0

解決,謝謝! –

相關問題