简体   繁体   中英

onListItemClick not called on ListActivity

My activity extends the ListActivvity and I added the onListItemClick () inside it but listitem click is not getting called.

When I inflate my view and returning that view inside the getView () method listitem click is not getting called but when I did not inflate view and directly call the super method it works fine.

CustomArrayAdapter.java

package com.mobileinsight.presenter.form;

import android.content.Context;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.mobileinsight.R;
import com.mobileinsight.common.SimpleMap;
import com.mobileinsight.common.Util;

public class CustomArrayAdapter extends ArrayAdapter<SimpleMap> {

    private Product[] objects;
    private Context _this;

    public CustomArrayAdapter(Context context, int resource, Product[] objects) {
        super(context, resource, objects);
        this.objects = objects;
        this._this = context;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) _this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

             rowView = inflater.inflate(R.layout.list_row, parent, false);
        } else {
            rowView = convertView;
        }

        TextView textView = (TextView) rowView.findViewById(R.id.text1);
        TextView detailview = (TextView) rowView.findViewById(R.id.text2);

              textView.setText("Text 1"));
//      textView.setClickable(true);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        detailview.setText(objects[position].getDetails());

        return rowView; //  not working
           // return super.getview(); // Working

    }

}

list_row.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="horizontal" >

     <TextView
         android:id="@+id/text1"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_alignParentLeft="true"
         android:layout_gravity="center_vertical|left"
         android:layout_margin="6dip"
         android:layout_weight="2"
         android:ellipsize="end"
         android:gravity="center_vertical|left"
         android:singleLine="false"
         android:textAppearance="?android:attr/textAppearanceLarge"
         />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_gravity="center_vertical|right"
        android:layout_margin="6dip"
        android:layout_weight="0"
        android:ellipsize="none"
        android:gravity="center_vertical|right"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Try this code, it may help..

public class CustomArrayAdapter extends ArrayAdapter<SimpleMap>
{
    LayoutInflater inflater;
    public CustomArrayAdapter(Context context) 
    {
        inflater = LayoutInflater.from(context);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;

        if (convertView == null) {
            rowView = inflater.inflate(R.layout.list_row, parent, false);
        } else {
            rowView = convertView;
        }

        TextView textView = (TextView) rowView.findViewById(R.id.text1);
        TextView detailview = (TextView) rowView.findViewById(R.id.text2);

        return rowView; 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM