2013-09-29 41 views
-1

我有一個列表,每列都有一個文本視圖和2個單選按鈕。我使用自定義數組適配器來填充列表視圖。我已經實現了對項目點擊收聽列表上,但它沒有返回anything.In其實它不執行對在all.Here點擊方法是代碼:單擊包含單選按鈕的列表項時無法獲得結果

適配器類:

public class MyAdapter extends ArrayAdapter<String> { 
Context context; 
public static ArrayList<String> values=new ArrayList<String>(); 
public static ArrayList<String> attendance=new ArrayList<String>(); 
RadioGroup radioGroup; 
    RadioButton present,absent; 
public MyAdapter(Context context,ArrayList values) { 
    super(context,org.example.attendance.R.layout.list_radio,values); 
    // TODO Auto-generated constructor stub 


    this.context=context; 
    this.values=values; 
} 

public View getView(int pos,View convertview,ViewGroup parent) 
    { 
     LayoutInflater inflator=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View rowView=inflator.inflate(org.example.attendance.R.layout.list_radio,parent,false); 
     radioGroup=(RadioGroup)rowView.findViewById(R.id.radioGroup1); 
     present=(RadioButton)rowView.findViewById(org.example.attendance.R.id.radio0); 
     absent=(RadioButton)rowView.findViewById(org.example.attendance.R.id.radio1); 
     TextView textview=(TextView)rowView.findViewById(org.example.attendance.R.id.textView1); 
    // textview.setText(values[pos]); 
     //String s=values[pos]; 

      textview.setText(values.get(pos)); 
     //int selectedId = radioGroup.getCheckedRadioButtonId(); 
     //if(selectedId==R.id.radio0) 
     return rowView; 


    } 
public boolean areAllItemsEnabled() 
    { 
     return true; 
    } 

    @Override 
    public boolean isEnabled(int arg0) 
    { 
     return true; 
    } 

} 

主等級:

public class Students_List extends Activity { 

Intent i1; 
String tablename; 
RadioGroup radioGroup; 
RadioButton present,absent; 
ArrayList<String> student_name = new ArrayList<String>() ; 
public static ArrayList<String> attendance=new ArrayList<String>(); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_students__list); 
    final ListView listview = (ListView) findViewById(R.id.listview); 
    i1=getIntent(); 
    radioGroup=(RadioGroup)findViewById(R.id.radioGroup1); 
    present=(RadioButton)findViewById(org.example.attendance.R.id.radio0); 
    absent=(RadioButton)findViewById(org.example.attendance.R.id.radio1); 
    tablename=i1.getStringExtra("class")+"_student"; 
    //Log.d("student","table"+tablename); 
    String temp=getServerData(KEY_12); 
    listview.setAdapter(new MyAdapter(this,student_name)); 
    listview.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int  position, 
       long arg3) { 
      // TODO Auto-generated method stub 
      Log.d("view","clicked"); 
      if(present.isChecked()) 
      { 
      attendance.add("present"); 
      } 
      if(absent.isChecked()) 
      { 
       attendance.add("absent"); 
      } 

} 

    }); 
} 

list_radio.xml:文件,它包含的意見,來填充每一行

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/textView2" 
    android:layout_marginTop="24dp" /> 

    <RadioGroup 
    android:id="@+id/radioGroup1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignTop="@+id/textView1" 
    android:layout_marginRight="35dp" 
    android:orientation="horizontal" > 
    <RadioButton 
    android:id="@+id/radio0" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    android:paddingRight="45dp" 
    android:checked="true" /> 

    <RadioButton 
    android:id="@+id/radio1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

    </RadioGroup> 


    </RelativeLayout> 

終於列表視圖XML文件:

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

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

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="150dp" 
     android:textSize="20sp" 
     android:textStyle="bold" 
     android:text="@string/present" 
     android:paddingRight="25dp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="20sp" 
     android:textStyle="bold" 
     android:text="@string/absent" /> 
</LinearLayout> 

    <ListView 
     android:id="@+id/listview" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:cacheColorHint="#f8f8ff" 
     android:divider="#000000" 
     android:fadeScrollbars="true" 
     android:choiceMode="singleChoice" 
     android:fastScrollEnabled="true" 
     android:footerDividersEnabled="true" 
     android:textFilterEnabled="true" 
     android:textStyle="normal" /> 


<Button 
    android:id="@+id/Button01" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/submit" 
    android:onClick="submit"/> 

</LinearLayout> 

我應該做的,這樣我可以得到國家廣電鈕釦??

回答

0

如果你的列表項目這個元素添加到佈局:

android:descendantFocusability="blocksDescendants" 

,也許從你的主要佈局中刪除嗎?

+0

它不工作。 – user2586942

0

這裏需要此代碼並將更改應用於您的需求。你需要有點自己的工作。

此代碼是一個微調列表。欲瞭解更多代碼,您可以訪問 Here

@Override 
public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) { 
    // On selecting a spinner item 
    String item = parent.getItemAtPosition(position).toString(); 

    // Showing selected spinner item 
    Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show(); 

} 

public void onNothingSelected(AdapterView&lt;?&gt; arg0) { 
    // TODO Auto-generated method stub 

} 
+0

它提供了一個錯誤,即void不是onItemSelected方法的有效返回類型 – user2586942

+0

您是否已經從我提供的url下載了整個代碼?它爲我工作。你能把我的全部代碼或項目發給我敏銳的參考資料嗎?如果是,那麼回覆我會給你我的電子郵件ID –

相關問題