2016-08-16 102 views
0

取消選擇在自定義列表視圖滾動單選按鈕 我已經定製列表視圖是添加 運行時間單選按鈕添加autimatically 但取消對滾動單選按鈕取消選擇在自定義列表視圖滾動

我的代碼如下適配器給出mainclass和活動文件

版式文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:background="@drawable/gradient11" 
       android:layout_width="match_parent" android:layout_height="wrap_content"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <RadioGroup 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <RadioButton 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="option one is selected now so you can" 
       android:textColor="#00ff00" 
       android:id="@+id/op1"/> 

      <RadioButton 
       android:checked="true" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="op2" 
       android:textColor="#00ff00" 
       android:id="@+id/op2"/> 

      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="op3" 
       android:textColor="#00ff00" 
       android:id="@+id/op3"/> 

      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="op4" 
       android:textColor="#00ff00" 
       android:id="@+id/op4"/> 

     </RadioGroup> 

    </LinearLayout> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/quest" 
     android:textColor="#e2000000"/> 

</LinearLayout> 

的適配器文件

package com.patel.ravin.com.domparsing; 

    import android.content.Context; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.BaseAdapter; 
    import android.widget.RadioButton; 
    import android.widget.TextView; 

    import java.util.ArrayList; 

    /** 
    * Created by lenovo on 08-08-2016. 
    */ 
    public class Adpt extends BaseAdapter 
    { 
     Context context; 
     ArrayList<MyBean> arrayList; 

     public Adpt(Context context,ArrayList<MyBean> arrayList) 
     { 
      this.context=context; 
      this.arrayList=arrayList; 
     } 
     @Override 
     public int getCount() { 
      return arrayList.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return null; 
     } 

     @Override 
     public long getItemId(int position) { 
      return 0; 
     } 

     @Override 
     public View getView(int i, View view, ViewGroup parent) { 

      LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = layoutInflater.inflate(R.layout.listview, null); 

      TextView txtFName = (TextView) view.findViewById(R.id.qid); 
      TextView txtLName = (TextView) view.findViewById(R.id.quest); 
      RadioButton op1=(RadioButton)view.findViewById(R.id.op1); 
      RadioButton op2=(RadioButton)view.findViewById(R.id.op2); 
      RadioButton op3=(RadioButton)view.findViewById(R.id.op3); 
      RadioButton op4=(RadioButton)view.findViewById(R.id.op4); 


      MyBean myBean = arrayList.get(i); 
      txtFName.setText("" + myBean.getQid()); 
      txtLName.setText(" Answer= " + myBean.getQname()); 
      op1.setText(myBean.getOp1()); 
      op2.setText(myBean.getOp2()); 
      op3.setText(myBean.getOp3()); 
      op4.setText(myBean.getOp4()); 

      return view; 
     } 
    } 

活動文件

package com.patel.ravin.com.domparsing; 

    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.widget.ArrayAdapter; 
    import android.widget.ListView; 
    import android.widget.Spinner; 
    import android.widget.TextView; 
    import android.widget.Toast; 

    import com.patel.ravin.com.domparsing.AsyncTask.AsyncTaskLoader; 
    import com.patel.ravin.com.domparsing.AsyncTask.OnAsyncResult; 

    import org.json.JSONArray; 
    import org.json.JSONException; 
    import org.json.JSONObject; 

    import java.util.ArrayList; 
    import java.util.HashMap; 

    public class MainActivity extends AppCompatActivity { 

     TextView textView; 
     ListView listView1; 
     Adpt adpt; 

     ArrayList<MyBean> arrayList=null; 

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

      // textView= (TextView) findViewById(R.id.tv1); 
      listView1=(ListView)findViewById(R.id.llv); 

    //  Adpt adpt=new Adpt(getApplicationContext(),arrayList); 


      //listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,arrayList)); 
      OnAsyncResult onAsyncResult=new OnAsyncResult() { 
       @Override 
       public void onAsyncResult(String result) { 

        Log.e("h", result.toString()); 

        try { 
         // textView.setText(""+result.toString()); 
        // String co=result.toString(); 

         JSONArray jsonArray=new JSONArray(result); 
         MyBean myBean; 
         arrayList = new ArrayList<>(); 

         for(int i=1;i<=jsonArray.length();i++) 
         { 
          JSONObject jsonObject=jsonArray.getJSONObject(i); 


          myBean = new MyBean(); 
          myBean.setQid(jsonObject.getString("que")); 
          myBean.setQname(jsonObject.getString("ans")); 

          myBean.setOp1(jsonObject.getString("a")); 
          myBean.setOp2(jsonObject.getString("b")); 
          myBean.setOp3(jsonObject.getString("c")); 
          myBean.setOp4(jsonObject.getString("d")); 

          arrayList.add(myBean); 
          listView1.setAdapter(new Adpt(getApplicationContext(),arrayList)); 
         } 
         //JSONObject object = new JSONObject(result); 

         //String contact = object.getString("que"); 



         // textView.setText(co); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 


       } 
      }; 

      AsyncTaskLoader asyncTaskLoader=new AsyncTaskLoader(MainActivity.this,onAsyncResult,null,"http://quiz/jsonapi.php"); 
      asyncTaskLoader.execute(); 


     } 


    } 

單選按鈕取消選擇在自定義列表視圖滾動 我已經定製列表視圖是添加 運行時間單選按鈕添加autimatically 但取消對滾動

+0

保持單選按鈕布爾值........... – sushildlh

回答

0

這是Android中使用Listviews和Radio按鈕的一個非常常見的問題。

首先,我建議你檢查這個帖子: Using radio button in custom listview in Android

現在,我要告訴你哪種解決方案適合我。在我的情況下,我使用GridView,但它也適用於ListView。

在適配器中的類,你必須有所選項目和可變他的指數,它可能是這樣的:

private int mSelectedPosition = -1; 
private RadioButton mSelectedRB; 

然後,定義一個函數來獲取這樣的信息:

public int getItemSelected(){ 
    return mSelectedPosition; 
} 

現在,爲了使它正常工作,您必須使用ViewHolder(在我的情況下,GridView中的每個項目都有一個圖像和一個RadioButton),因此您也可以在Adapter類中定義ViewHolder:

private class ViewHolder { 
    ImageView image; 
    RadioButton radio; 
} 

然後,在您的getView函數中,您必須使用ViewHolder。在代碼中,我寫下了需要遵循的評論。

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    View view = convertView; // assign the view 
    ViewHolder holder; // declare the ViewHolder 

    if(view == null){ 
     // cutom layout for each row (item) of the ListView 
     view = mLayoutInflater.inflate(R.layout.item_list_company, parent, false); 
     holder = new ViewHolder(); 

     // initialize the ViewHolder's field 
     holder.image = (ImageView) view.findViewById(R.id.c1); 
     holder.radio = (RadioButton)view.findViewById(R.id.cN1); 

     view.setTag(holder); // set the tag 
    }else{ // already initialized 
     holder = (ViewHolder)view.getTag(); // so we only set the tag 
    } 

    // on click triggered for each RadioButton in the ListView 
    holder.radio.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      if(position != mSelectedPosition && mSelectedRB != null){ 
       mSelectedRB.setChecked(false); // uncheck the last one 
      } 

      mSelectedPosition = position; // change the item selected index 
      mSelectedRB = (RadioButton)v; // assign the new item selected 
     } 
    }); 

    // just to control the right item checked 
    if(mSelectedPosition != position){ 
     holder.radio.setChecked(false); 
    }else{ 
     holder.radio.setChecked(true); 
     if(mSelectedRB != null && holder.radio != mSelectedRB){ 
      mSelectedRB = holder.radio; 
     } 
    } 

    return view; 
} 

最後,在自定義佈局(item_list_company.xml在我的情況)在ListView中的每個項目,我有以下代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:layout_height="match_parent"> 
    <ImageView android:id="@+id/c1" 
     android:layout_width="wrap_content" 
     android:layout_height="100dp" 
     android:layout_gravity="center" 
     android:scaleType="centerInside" /> 
    <RadioButton 
     android:id="@+id/cN1" 
     android:layout_width="wrap_content" 
     android:layout_height="50dp" 
     android:buttonTint="@color/colorPrimary" 
     android:textColor="@color/colorAccent" 
     android:focusable="false" 
     android:layout_gravity="left" 
     android:clickable="false" 
     android:focusableInTouchMode="false" 
     android:textSize="20dp"/> 
</LinearLayout> 

特別注意對於這個三個屬性RadioButton:

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

所以,所有這一切,你只需要在你的Activity中設置你的適配器和ListView並調用右鍵功能來檢索選定的項目:

ArrayList<MyBean> arrayList = new ArrayList<>(); 
ListView listView1 = (ListView)findViewById(R.id.llv); 
Adpt adpt = new Adpt(getApplicationContext(), arrayList); 

// add data to the ArrayList 

adpt.notifyDataSetChanged(); // notify for the new data in the ArrayList 

// retrieve the item selected 
int selected = adpt.getItemSelected(); 

希望它有幫助!