setOnItemClickListener无法正常工作

时间:2016-11-22 06:41:07

标签: java android xml listview onclicklistener

我的Listview应用从自定义适配器ListAdapter.class获取itemview的数据和背景颜色。我还需要在textview listview以下setOnItemClickListener设置当前选中的列表项值MAinActivity中的public class MainActivity extends Activity { private static ListAdapterclass adapter; ListView lv; TextView tv2; private final String android_versions[]={ "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean", "KitKat", "Lollipop", "Marshmallow" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } private void initViews() { lv = (ListView) findViewById(R.id.listView1); tv2 = (TextView) findViewById(R.id.selected); adapter = new ListAdapterclass(getApplicationContext(), android_versions); lv.setAdapter(adapter); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(getApplicationContext(), "hiiiiiiiii", Toast.LENGTH_SHORT).show(); System.out.println("********************** INSIDE ONITEMCLICKLISTNER IN MAIN ACTIVITY ******************"); String ver_name = (lv.getItemAtPosition(i)).toString(); tv2 = (TextView) findViewById(R.id.selected); tv2.setText(ver_name); } }); } } 未执行.pls帮助。

这是我的列表视图应用:

Layout image

MainActivity.java

public class ListAdapterclass extends ArrayAdapter implements AdapterView.OnItemClickListener{

    private String android_versionNames[];
    Context mContext;
    public int row_index=-1;

    @Override
    public void onItemClick(AdapterView<?> adapterView, View v, int i, long l) {
        int position=(Integer)v.getTag();
        String ver_name=getItem(position).toString();
    }

    private static class ViewHolder{
        TextView tv;
       LinearLayout LL;
        TextView tv2;
    }



    public ListAdapterclass(Context context,String android_versionnames[]) {
        super(context, R.layout.list_item,android_versionnames);
        this.android_versionNames=android_versionnames;
        this.mContext=context;

        System.out.println(" ????????????????????????  Inside dataadapter,Android names :  ?????????????????????????????\n  ");
        for(int i=0;i<android_versionnames.length;i++){
            System.out.println("\n"+android_versionnames[i]);
        }

    }
    private int lastPosition=-1;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        String ver_name=getItem(position).toString();

        final ViewHolder viewHolder;
        final View result;

        if(convertView==null){
            viewHolder=new ViewHolder();
            LayoutInflater inflater=LayoutInflater.from(getContext());
            convertView=inflater.inflate(R.layout.list_item,parent,false);
            viewHolder.tv=(TextView)convertView.findViewById(R.id.label);
            viewHolder.LL=(LinearLayout) convertView.findViewById(R.id.linearLayout_1);
            viewHolder.tv2=(TextView)convertView.findViewById(R.id.selected);
            result=convertView;
            convertView.setTag(viewHolder);
        }else{
            viewHolder=(ViewHolder) convertView.getTag();
            result=convertView;
        }
        lastPosition=position;

        viewHolder.tv.setText(ver_name);

        viewHolder.LL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                row_index=position;
                notifyDataSetChanged();
            }
        });
        if(row_index==position){
            viewHolder.LL.setBackgroundColor(Color.parseColor("#409de1"));
            viewHolder.tv.setTextColor(Color.parseColor("#ffffff"));
        }
        else
        {
            viewHolder.LL.setBackgroundColor(Color.parseColor("#ffffff"));
            viewHolder.tv.setTextColor(Color.parseColor("#000000"));

        }
        return convertView;
    }
}

ListAdapter.class

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
android:orientation="vertical"
    tools:context="com.cybraum.test.listviewcolorchange.MainActivity"
    >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
android:clickable="true"
    android:layout_weight="1"
    >
 <ListView
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/listView1"
     >
 </ListView>


</LinearLayout>

 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="0dp"
     android:layout_weight=".2"
     android:id="@+id/linearLayout_2"
     android:orientation="horizontal"

     >
  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Selected : "
      android:textStyle="bold"
      android:layout_gravity="center"/>
  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textStyle="bold"
      android:text="text"
      android:id="@+id/selected"
      android:layout_gravity="center"/>

 </LinearLayout>

</LinearLayout>

ActivityMain.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:id="@+id/linearLayout_1"
    android:padding="10dp">
<TextView
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold"
    android:textColor="#000000"
    android:gravity="center">
</TextView>
</LinearLayout>

listitem.xml

char[] arr1 = { 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'e', 'e', 'g', 'f', 'h' };

Arrays.sort(arr1);
for (int i = 0; i < arr1.length; i++) {
    char each = arr1[i];
    System.out.print(each);
    if (i + 1 < arr1.length && arr1[i + 1] != each) {
        System.out.println();
    }
}

问题是什么?

6 个答案:

答案 0 :(得分:1)

如果您从适配器中获取点击事件,那么如果您需要适配器点击事件和列表视图项目,则listview itemclick无效,请参阅链接, How to make imageView clickable from OnItemClickListener?

  @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    long viewId = view.getId();

    if (viewId == R.id.button1) {
        Toast.makeText(this, "Button 1 clicked", Toast.LENGTH_SHORT).show();
    } else if (viewId == R.id.button2) {
        Toast.makeText(this, "Button 2 clicked", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "ListView clicked" + id, Toast.LENGTH_SHORT).show();
    }
}

在适配器中:

viewHolder.Btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0); // Let the event be handled in onItemClick()
}

答案 1 :(得分:0)

使用:

 lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getApplicationContext(), "hiiiiiiiii", Toast.LENGTH_SHORT).show();
                System.out.println("********************** INSIDE  ONITEMCLICKLISTNER IN MAIN ACTIVITY ******************");
                String ver_name = (lv.getItemAtPosition(i)).toString();


                tv2 = (TextView) findViewById(R.id.selected);
                tv2.setText(ver_name);
            }
        });

从适配器删除

实现AdapterView.OnItemClickListener

答案 2 :(得分:0)

您需要从setOnClickListner()移除适配器 尝试使用

更改您的方法
listView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
   Object o = prestListView.getItemAtPosition(position);
   prestationEco str=(prestationEco)o;//As you are using Default String Adapter
   Toast.makeText(getBaseContext(),str.getTitle(),Toast.LENGTH_SHORT).show();
    }
});

答案 3 :(得分:0)

从适配器中删除viewHolder.LL.setOnClickListener侦听器 在适配器中添加一个更新row_index的方法:

public void changeIndex(int rowIndex){
this.row_index = rowIndex;
notifyDataSetChanged();
}

从onItemClickListener事件调用此方法:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
adapter.changeIndex(i);//This will give you the same result of viewHolder.LL.setOnClickListener as you are doing
                //Do whatever you are doing previously
            }
        }); 

答案 4 :(得分:0)

更改listview xml

    <ListView

         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:clickable="true"   
         android:focusable="true">

 </ListView>

从适配器类中删除AdapterView.OnItemClickListener

public class ListAdapterclass extends ArrayAdapter {

}

答案 5 :(得分:0)

更改listview xml

<ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"></ListView>

从适配器类中删除AdapterView.OnItemClickListener

public class ListAdapterclass extends ArrayAdapter {

}