2015-09-05 36 views
1

的Android列表視圖我有它實現列表視圖與列表中的每個行中的旋轉器和單選按鈕定製陣列適配器。選擇單選按鈕時,每個列表單選按鈕被選中。如何使用單選列表行進行選擇。與旋轉器和單選按鈕

如何使用單選按鈕,選擇只有一個列表。

對不起,英語不好。

這是代碼。

getView():

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

    if (convertView == null) 
    { 
     LayoutInflater inflator = myContext.getLayoutInflater(); 
     view = inflator.inflate(R.layout.single_list_row, null); 

     final ViewHolder viewHolder = new ViewHolder(); 
     viewHolder.data = new DataModel(myContext); 
     viewHolder.spin = (Spinner) view.findViewById(R.id.spinner1); 
     viewHolder.spin.setAdapter(viewHolder.data.getAdapter()); 
     viewHolder.radioBtn = (RadioButton) view.findViewById(R.id.choice); 
     viewHolder.radioBtn.setTag(position); 
     //viewHolder.radioBtn.setChecked(selectedPos == position); 

     viewHolder.spin.setOnItemSelectedListener(new OnItemSelectedListener() 
     { 
      @Override 
      public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
      { 
       viewHolder.data.setSelected(arg2); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> arg0) { 
      } 
     }); 

     viewHolder.radioBtn.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View view) 
      { 
       selectedPos = (Integer)view.getTag(); 
      } 
     }); 

     view.setTag(viewHolder); 
    } 
    else 
    { 
     view = convertView; 
    } 

    ViewHolder holder = (ViewHolder) view.getTag(); 
    holder.spin.setSelection(getItem(position).getSelected()); 
    holder.radioBtn.setChecked(selectedPos == position); 
    return view; 
} 

main_activity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <EditText android:id="@+id/inputSearch" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:hint="Search list.." 
     android:inputType="textVisiblePassword"/> 

    <ListView 
     android:id="@+id/listview1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:cacheColorHint="#f8f8ff" 
     android:divider="#000000" 
     android:fadeScrollbars="true" 
     android:choiceMode="singleChoice" 
     android:descendantFocusability="beforeDescendants" 
     android:fastScrollEnabled="true" 
     android:footerDividersEnabled="true" 
     android:textFilterEnabled="true" 
     android:textStyle="normal" /> 
</LinearLayout> 

single_row_list.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout android:id="@+id/relativeLayout1" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

<LinearLayout 
    android:layout_centerVertical="true" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:orientation="vertical" 
    android:gravity="center_vertical"> 

    <Spinner 
     android:id="@+id/spinner1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:entries="@array/options" 
     android:prompt="@string/spinner_prompt" /> 

    <RadioButton 
     android:layout_height="wrap_content" 
     android:id="@+id/choice" 
     android:layout_width="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_alignParentRight="true" 
     android:gravity="center_vertical" 
     android:checked="false"/> 
    </LinearLayout> 
    </RelativeLayout> 

MainActivity.java:

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    lv = (ListView) this.findViewById(R.id.listview1); 
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

    DataModel data = new DataModel(this); 
    DataModel data1 = new DataModel(this); 
    DataModel data2 = new DataModel(this); 
    DataModel data3 = new DataModel(this); 
    DataModel data4 = new DataModel(this); 

    d = new ListAdapter(this, R.layout.single_list_row, new DataModel[] { data, data1, data2, data3, data4 }); 
    lv.setAdapter(d); 
} 

的DataModel類

public class DataModel { 

    private int selected; 
    private ArrayAdapter<CharSequence> adapter; 

    public DataModel(Context parent){ 
      adapter = ArrayAdapter.createFromResource(parent, R.array.options, android.R.layout.simple_spinner_item); 
      sadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    } 

    public ArrayAdapter<CharSequence> getAdapter() { return adapter; } 

    public int getSelected() { return selected; } 

    public void setSelected(int selected) { this.selected = selected; } 
} 

完整的適配器類別:

public class ListAdapter extends ArrayAdapter<DataModel> 
{ 
private Activity myContext; 
int selectedPos = 0; 
int flag = 0; 
int pos = 0; 

public ListAdapter(Activity context, int textViewResourceId, DataModel[] objects) 
{ 
    super(context, textViewResourceId, objects); 
    myContext = context; 
} 

static class ViewHolder 
{ 
    protected DataModel data; 
    protected Spinner spin; 
    protected RadioButton radioBtn; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    View view = convertView; 
    final ViewHolder viewHolder; 

    if (view == null) 
    { 
     LayoutInflater inflator = myContext.getLayoutInflater(); 
     view = inflator.inflate(R.layout.single_list_row, null); 

     viewHolder = new ViewHolder(); 
     viewHolder.data = new DataModel(myContext); 

     viewHolder.spin = (Spinner) view.findViewById(R.id.spinner1); 
     viewHolder.spin.setAdapter(viewHolder.data.getAdapter()); 
     viewHolder.radioBtn = (RadioButton) view.findViewById(R.id.choice); 
     viewHolder.radioBtn.setTag(position); 

     if(selectedPos == position) 
     { 
      viewHolder.radioBtn.setChecked(true); 
     } 
     else 
     { 
      viewHolder.radioBtn.setChecked(false); 
     } 

     view.setTag(viewHolder); 
    } 
    else 
    { 
     viewHolder = (ViewHolder) view.getTag(); 
    } 

    //viewHolder.data = new DataModel(myContext); 
    //viewHolder.spin.setAdapter(viewHolder.data.getAdapter()); 

    viewHolder.spin.setOnItemSelectedListener(new OnItemSelectedListener() 
    { 
     @Override 
     public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
     { 
      viewHolder.data.setSelected(arg2); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> arg0) { 
     } 
    }); 

    viewHolder.radioBtn.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View view) 
     { 
      selectedPos = (Integer)view.getTag(); 
     } 
    }); 

    viewHolder.spin.setSelection(getItem(position).getSelected()); 
    viewHolder.radioBtn.setChecked(selectedPos == position); 
    view.setTag(viewHolder); 
    return view; 
} 
} 
+0

你能跟着我的答案,看看那是怎麼回事嗎? –

回答

0

我得到了解決方案。

@Override 
public View getView(final int position, View convertView, ViewGroup parent) 
{ 
    View view = convertView; 
    final ViewHolder viewHolder; 

    if (view == null) 
    { 
     LayoutInflater inflator = myContext.getLayoutInflater(); 
     view = inflator.inflate(R.layout.single_list_row, null); 

     viewHolder = new ViewHolder(); 
     viewHolder.data = new DataModel(myContext); 

     viewHolder.selectedText = (TextView) view.findViewById(R.id.singleitem); 
     viewHolder.spin = (Spinner) view.findViewById(R.id.spinner1); 
     viewHolder.spin.setAdapter(viewHolder.data.getAdapter()); 
     viewHolder.radioBtn = (RadioButton) view.findViewById(R.id.choice); 

     view.setTag(viewHolder); 
    } 
    else 
    { 
     viewHolder = (ViewHolder) view.getTag(); 
    } 

    viewHolder.spin.setOnItemSelectedListener(new OnItemSelectedListener() 
    { 
     @Override 
     public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
     { 
      viewHolder.data.setSelected(arg2); 
      viewHolder.selectedText.setText(viewHolder.data.getText()); 
      //Log.v("", String.valueOf(viewHolder.spin.getSelectedItemPosition())); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> arg0) 
     { 
     } 
    }); 

    if(flag != 1) 
    { 
     if (selectedPos == position) 
     { 
      viewHolder.radioBtn.setChecked(true); 
     } 
     else 
     { 
      viewHolder.radioBtn.setChecked(false); 
     } 
    } 
    else 
    { 
     viewHolder.radioBtn.setChecked(true); 
    } 

    viewHolder.radioBtn.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View view) 
     { 
      if(flag != 1) 
      { 
       if (((RadioButton) view).isChecked()) 
       { 
        selectedPos = position; 
       } 
       else 
       { 
        selectedPos = -1; 
       } 
      } 
      else 
      { 
       ((RadioButton) view).setChecked(true); 
      } 
      notifyDataSetChanged(); 
     } 
    }); 

    //viewHolder.spin.setSelection(getItem(position).getSelected()); 
    viewHolder.selectedText.setText(viewHolder.data.getText()); 
    view.setTag(viewHolder); 
    return view; 
} 
0

首先,

RadioButtons被認爲是有源元件,

您可以添加,

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout android:id="@+id/relativeLayout1" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:descendantFocusability="blocksDescendants" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

,並添加,

  android:clickable="false" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 

你的單選按鈕元素,否則你不會得到這些點擊的事件。

我看到你的代碼中的一些問題,嘗試了這一點

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    View view = convertView; 
    ViewHolder viewHolder; 

    if (view == null) 
    { 
     LayoutInflater inflator = myContext.getLayoutInflater(); 
     view = inflator.inflate(R.layout.single_list_row, null); 

     viewHolder = new ViewHolder(); 

     viewHolder.spin = (Spinner) view.findViewById(R.id.spinner1); 
     viewHolder.radioBtn = (RadioButton) view.findViewById(R.id.choice); 

     view.setTag(viewHolder); 

    }else{ 
     viewHolder = (ViewHolder) view.getTag(); 
    } 


     viewHolder.data = new DataModel(myContext); 
     viewHolder.spin.setAdapter(viewHolder.data.getAdapter()); 
     viewHolder.radioBtn.setTag(position); 

     viewHolder.spin.setOnItemSelectedListener(new OnItemSelectedListener() 
     { 
      @Override 
      public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
      { 
       viewHolder.data.setSelected(arg2); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> arg0) { 
      } 
     }); 

     viewHolder.radioBtn.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View view) 
      { 
       selectedPos = (Integer)view.getTag(); 
      } 
     }); 


    viewHolder.spin.setSelection(getItem(position).getSelected()); 
    viewHolder.radioBtn.setChecked(selectedPos == position); 
    view.setTag(viewHolder); 
    return view; 
} 

的問題是在此代碼,

viewHolder.radioBtn.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View view) 
      { 
       selectedPos = (Integer)view.getTag(); 
      } 
     }); 

(Integer)view.getTag()是給予的位置,因此,所有的時間if語句是真的。

我建議你保持選定的狀態形成的DataModel一個布爾值,並用它來保持選定的單選按鈕。

請張貼完整的適配器類,看看你在做什麼在那裏。

+0

我在viewHolder上收到錯誤。 – Kruise

+0

@Kruise對不起,您現在可以檢查 –

+0

viewHolder.spin.setAdapter(viewHolder.data。getAdapter()); – Kruise