我正在尝试使用自定义列表视图列出所有已安装的应用程序。每行包含ImageView,TextView和RadioButton。问题是,当我单击任何单选按钮时,会选择多个单选按钮。
我的自定义适配器的代码和listview行的布局如下所示。
我的代码有什么问题?欢迎任何帮助。
ListViewAdapter.java
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
private Context context;
private ArrayList<ListItem> items;
private LayoutInflater inflater;
private String TAG = "TAG";
public ListViewAdapter(Context context, ArrayList<ListItem> items) {
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Declare Variables
View v = convertView;
AppNamesViewHolder viewHolder;
ListItem item = items.get(position);
if (convertView == null)
{
Log.d(TAG,"in convertview=null");
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item_layout, null);
viewHolder = new AppNamesViewHolder(v);
v.setTag(viewHolder);
}
else
{
viewHolder = (AppNamesViewHolder) v.getTag();
}
viewHolder.appName.setText(item.getName());
viewHolder.imgIcon.setImageDrawable(item.getIcon());
return v;
}
}
class AppNamesViewHolder
{
public TextView appName;
public ImageView imgIcon;
public RadioButton radioBt;
public AppNamesViewHolder(View base)
{
appName = (TextView) base.findViewById(R.id.tvAppName);
imgIcon = (ImageView) base.findViewById(R.id.ivIcon);
radioBt = (RadioButton)base.findViewById(R.id.radio);
}
}
list_item_layout.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="65dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/ivIcon"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:scaleType="fitXY" />
<TextView
android:id="@+id/tvAppName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_weight="0.85"
android:text="AppName"
android:textColor="@color/blue"
android:textSize="15dp" />
<RadioButton
android:id="@+id/radio"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:layout_gravity="center"
android:onClick="onRadioButtonClicked" />
</LinearLayout>