自定义ListView的项目单击事件在Android中不起作用

时间:2016-01-12 07:43:56

标签: android listview android-listview

我们正在开发一个项目,我们有一个自定义列表视图和一个baseadapter。我们正在尝试获取listview的项目点击事件。我在活动中的项目点击监听器中粘贴了一个吐司,但它没有显示。以下是我所做的代码。

question_list is the ListView Object.

question_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               // TextView example = (TextView) view.findViewById(R.id.single_question_name);
              //  String a=example.getText().toString();
                Toast.makeText(MainActivity.this,"jo", Toast.LENGTH_LONG).show();
            }
        });

listview的数据是动态添加的。我们在baseadapter中使用了两个动​​态布局来充气.baseadapter中的代码如下:

public class QuestionAdapter extends BaseAdapter {
    ArrayList<QuestionsDataModel>  question_list_array;
    List<OptionsDataModel> option_list;
    Context context;
    LayoutInflater inflater;
    SingleHolder sh;
    int k=0,l=0;
    View v_single=null,v_multiple=null,v_final=null;
    MultipleHolder mh;
    int number_of_options;
    int position_counter=0;
    public  static  final  int TYPE_HEADER=0;
    public  static  final  int TYPE_ROW=1;
    public QuestionAdapter(ArrayList<QuestionsDataModel> question_list_array, Context context) {
        this.question_list_array=question_list_array;
        this.context=context;
        inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        return question_list_array.size();
    }

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

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

   @Override
    public int getViewTypeCount()
    {
        return 2;

    }

    @Override
    public int getItemViewType(int position) {
        return (question_list_array.get(position).getQuestion_type().equals("single"))?TYPE_HEADER :TYPE_ROW;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        String question_type = question_list_array.get(position).getQuestion_type();
        option_list = question_list_array.get(position).option_list;
        number_of_options = option_list.size();

        if (question_type.equals("single"))
        {
            v_single=convertView;
            if (v_single == null)
            {
                v_single = inflater.inflate(R.layout.single_choice_layout, null);
                sh = new SingleHolder();
                sh.single_question_name = (TextView) v_single.findViewById(R.id.single_question_name);
                sh.single_radiogroup = (RadioGroup) v_single.findViewById(R.id.single_radiogroup);
                v_single.setTag(sh);
            }
            else
            {
                sh = (SingleHolder) v_single.getTag();
            }
            sh.single_radiogroup.clearCheck();
            sh.single_radiogroup.removeAllViews();
            sh.single_question_name.setText(question_list_array.get(position).getQuestion_name());
            for (int j = 0; j < number_of_options; j++)
            {
                RadioButton rb = new RadioButton(context);
                rb.setText(option_list.get(j).getOption_name());
                sh.single_radiogroup.addView(rb);
            }

           v_final=v_single;
        }



      else if(question_type.equals("multiple"))
        {

            v_multiple = convertView;
            if (v_multiple == null) {
                v_multiple = inflater.inflate(R.layout.multiple_choice_layout, null);
                mh = new MultipleHolder();
                mh.multiple_question_name = (TextView) v_multiple.findViewById(R.id.multiple_question_name);
                mh.ll_multiple = (LinearLayout) v_multiple.findViewById(R.id.ll_multiple);
                mh.ll_choice_child= (LinearLayout) v_multiple.findViewById(R.id.ll_choice_child);
                v_multiple.setTag(mh);

            } else {
                mh = (MultipleHolder) v_multiple.getTag();
            }
            mh.multiple_question_name.setText(question_list_array.get(position).getQuestion_name());
            mh.ll_choice_child.removeAllViews();
                for (int m = 0; m < number_of_options; m++)
                {
                    CheckBox c = new CheckBox(context);
                    c.setText(option_list.get(m).getOption_name());
                    mh.ll_choice_child.addView(c);
                }


            //return v;
            v_final=v_multiple;

        }
        return v_final;
    }



    private   class SingleHolder
    {
        TextView single_question_name;
        RadioGroup single_radiogroup;

    }
    private  class MultipleHolder
    {
        TextView multiple_question_name;
        LinearLayout ll_multiple;
        LinearLayout ll_choice_child;
    }
}

编辑: 我添加了单项xml文件:

single_choice_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="match_parent"
    android:orientation="vertical"
   >
   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/single_question_name"
      />
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/single_radiogroup"
        android:orientation="vertical"></RadioGroup>


</LinearLayout>

multiple_choice_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="match_parent"
    android:id="@+id/ll_multiple"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/multiple_question_name"
        android:text="second question"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ll_choice_child"
        android:orientation="vertical"></LinearLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

尝试在onCreate()方法中定义ClickListener。

答案 1 :(得分:0)

您必须在BaseAdaptor的Listview方法中返回getItem项的位置

 @Override
    public QuestionsDataModel getItem(int position) {
        return question_list_array.get(position);
    }

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