我用radiobutton实现了一个Gridview,这是我的代码:
row_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginRight="10px" >
</ImageView>
<RadioButton
android:id="@+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5px"
android:textSize="15px" >
</TextView>
</LinearLayout>
我的MainActivity.java文件
public class Radio extends Activity {
ArrayList<ParserCategory>mList;
DatabaseConnectionAPI mApi;
ImageAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*** Get Images from SDCard ***/
// gridView1
mApi=new DatabaseConnectionAPI(getApplicationContext());
try {
mApi.createDataBase();
mApi.openDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mList=mApi.getCategoryData();
final GridView gView1 = (GridView)findViewById(R.id.gridView1);
mAdapter=new ImageAdapter(Radio.this, mList);
gView1.setAdapter(mAdapter);
// Get Item Checked
Button btnGetItem = (Button) findViewById(R.id.btnGetItem);
btnGetItem.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int count = gView1.getChildCount();
System.out.println("Count "+count);
for (int i = 0; i < count; i++) {
ViewGroup gridChild = (ViewGroup) gView1.getChildAt(i);
RadioButton rbtn = (RadioButton)gridChild.findViewById(R.id.radio);
if(rbtn.isChecked())
{
Log.d("Item "+String.valueOf(i), rbtn.getTag().toString());
System.out.println("ITEEMERM "+String.valueOf(i)+rbtn.getTag().toString());
Toast.makeText(Radio.this,rbtn.getTag().toString() ,2000).show();
}
}
}
});
}
public class ImageAdapter extends BaseAdapter
{
private Context context;
private ArrayList<ParserCategory> lis;
public ImageAdapter(Context c, ArrayList<ParserCategory> mList)
{
context = c;
lis = mList;
}
public int getCount() {
return lis.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.raddioshowimage, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.textView1);
String strPath = lis.get(position).toString();
// Get File Name
String fileName=lis.get(position).getCname();
textView.setText(lis.get(position).getCname());
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
RadioButton radiobtn = (RadioButton) convertView.findViewById(R.id.radio);
radiobtn.setTag(fileName);
return convertView;
}
}
}
当我运行上面的代码时,它会选择多个带有radiobutton的项目,但是当我点击一个按钮时我想要一个项目选择,所以我知道如何解决它?