我在行XML
文件中的自定义适配器类上使用了以下XML
,并添加了我的自定义适配器类。当我点击Spinner
之后,点击ListView
项目点击即可。如何解决这个问题?
我的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/exam_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/qeuestion_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="qwq"
android:textColor="@color/Black"
android:textSize="18sp" />
<ImageView
android:id="@+id/img_select_question"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:src="@drawable/select_question" />
<Spinner
android:id="@+id/marks_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/img_select_question"
android:focusable="false"
android:focusableInTouchMode="false"
android:padding="10dp"
android:prompt="@string/tv_chapter" />
</RelativeLayout>
将设定值中的微调器作为自定义类。 所以我不是没有更好或没有。 同样有助于解决点击列表视图问题。
我的适配器Java代码:
public class Question_list_adapter extends BaseAdapter{
private LayoutInflater l_Inflater;
Activity obj_a;
List<Questions_Details> question_list;
private static class ViewHolder {
public DataHolder data;
public TextView qeuestion_name;
public ImageView img_select_question;
public Spinner marks_spinner;
}
public Question_list_adapter(Activity activity,List<Questions_Details> list_question_Details) {
this.obj_a=activity;
this.question_list =list_question_Details;
l_Inflater = LayoutInflater.from(activity);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return question_list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return question_list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View vi = convertView;
if(vi==null)
vi = l_Inflater.inflate(R.layout.question_list_row, null);
holder = new ViewHolder();
holder.qeuestion_name = (TextView)vi.findViewById(R.id.qeuestion_name);
holder.img_select_question=(ImageView)vi.findViewById(R.id.img_select_question);
holder.qeuestion_name.setText(question_list.get(position).question);
holder.marks_spinner= (Spinner)vi.findViewById(R.id.marks_spinner);
holder.data = new DataHolder(obj_a);
holder.marks_spinner.setAdapter(holder.data.getAdapter());
if(Global_Application.selected_question_list.contains(question_list.get(position).question_Id))
holder.img_select_question.setVisibility(View.VISIBLE);
else
holder.img_select_question.setVisibility(View.INVISIBLE);
return vi;
}
}
spinner in set custom class
public class DataHolder {
private int selected;
private ArrayAdapter<CharSequence> adapter;
public DataHolder(Context parent) {
adapter = ArrayAdapter.createFromResource(parent, R.array.marks, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
public ArrayAdapter<CharSequence> getAdapter() {
return adapter;
}
public String getText() {
return (String) adapter.getItem(selected);
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
}
答案 0 :(得分:0)
Try to replace your adapter code with this
public class Question_list_adapter extends BaseAdapter {
private LayoutInflater l_Inflater;
Activity activity;
List<Questions_Details> question_list;
private class ViewHolder {
public TextView qeuestion_name;
public ImageView img_select_question;
public Spinner marks_spinner;
}
public Question_list_adapter(Activity activity,List<Questions_Details> list_question_Details) {
this.activity=activity;
this.question_list =list_question_Details;
}
@Override
public int getCount() {
return question_list.size();
}
@Override
public Object getItem(int position) {
return question_list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(activity).inflate(R.layout.question_list_row, null);
holder.qeuestion_name = (TextView)convertView.findViewById(R.id.qeuestion_name);
holder.img_select_question=(ImageView)convertView.findViewById(R.id.img_select_question);
holder.marks_spinner= (Spinner)convertView.findViewById(R.id.marks_spinner);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.qeuestion_name.setText(question_list.get(position).question);
holder.marks_spinner.setAdapter(new DataHolder(activity));
if(Global_Application.selected_question_list.contains(question_list.get(position).question_Id))
holder.img_select_question.setVisibility(View.VISIBLE);
else
holder.img_select_question.setVisibility(View.INVISIBLE);
return convertView;
}
}