Question

Hi onListItemClick for listview is not working. Here i am fetching datas from SQLite using AsyncTask and displaying it in a list view. And i wants to do some actions when a list in a listview has clicked. But the click is not happening. I had tried a lot for this. Please help me. Here is my code

    package com.applexus.app.mobilesalesorder;

import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

import com.applexus.app.library.sql.SqlConnector;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class SoldToPartiesList extends ListActivity {
    private ArrayList<String> data = new ArrayList<String>();
    private ArrayList<String> idk = new ArrayList<String>();
    private ArrayList<String> name1 = new ArrayList<String>();
    private ArrayList<String> inco1 = new ArrayList<String>();
    private ArrayList<String> email = new ArrayList<String>();
    private ArrayList<String> tel = new ArrayList<String>();
    private ArrayList<String> vwerk = new ArrayList<String>();
    private SharedPreferences prefs;
    private String prefNamesalesorgid = "salesorgid";
    private String prefNamedistchnlid = "distchnlid";
    private String prefNamedivid = "divid";
    private String prefName = "mso";
    private TextView titlename;

    private static class ViewHolder {
        TextView tvlist;
        TextView tvlistsmall;
    }

    private class EfficientAdapter extends BaseAdapter {

        private Context context;
        LayoutInflater inflater;

        public EfficientAdapter(Context context) {
            // TODO Auto-generated constructor stub
            this.context = context;
            inflater = LayoutInflater.from(context);

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder holder;
            final int place = position;

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.listso, null);              
                holder = new ViewHolder();
                holder.tvlist = (TextView) convertView
                        .findViewById(R.id.textViewlist);
                holder.tvlistsmall = (TextView) convertView
                        .findViewById(R.id.textView1);          
                convertView.setTag(holder); 
//              
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tvlist.setText(idk.get(position));
            holder.tvlistsmall.setText(data.get(position));

            return convertView;
        }


    }

    Map<String, String> map = new TreeMap<String, String>();
    SqlConnector con;
    String salorg;
    String distch;
    String division;
    Context co = this;
    Boolean searchable=false;
    TextView tvmc;
    TextView tvmn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.materiallist);
        titlename = (TextView) findViewById(R.id.textViewtitle);
        titlename.setText(R.string.soldtoparties);
        tvmc=(TextView)findViewById(R.id.textViewmc);
        tvmn=(TextView)findViewById(R.id.textViewmn);
        prefs = getSharedPreferences(prefName, MODE_PRIVATE);
        salorg = (prefs.getString(prefNamesalesorgid, ""));
        distch = (prefs.getString(prefNamedistchnlid, ""));
        division=(prefs.getString(prefNamedivid, ""));
        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(new String[] { null });

//      ListView lv=(ListView)findViewById(android.R.id.list);
//      lv.setOnItemSelectedListener(new )
    }

    EditText es;
    LinearLayout ls;
    LinearLayout mc;
    LinearLayout mn;
    Boolean searchFlag = false;
    String search;

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        es = (EditText) findViewById(R.id.editTextSearch);
        ls = (LinearLayout) findViewById(R.id.linearLayoutsearch);

    }

    private class DownloadWebPageTasksearch extends
            AsyncTask<String, Void, String> {
        Cursor c;
        ProgressBar pb;

        @Override
        protected String doInBackground(String... urls) {
            con = new SqlConnector(co);
            try {
                if (searchFlag) {
                    c = con.select("select Kunnr,Name,Name1,Inco1,Vwerk,SmtpAddr,Telf1 from tb_soldtoparties where salesorg='"
                            + salorg + "' and channel='" + distch + "' and Name like '%"+search+"%' and division='"+division+"';");

                } else {
                    c = con.select("select Kunnr,Name,Name1,Inco1,Vwerk,SmtpAddr,Telf1 from tb_soldtoparties where salesorg='"
                            + salorg + "' and channel='" + distch + "' and Kunnr like '%"+search+"%' and division='"+division+"';");

                }
            } catch (Exception e) {

                e.printStackTrace();
            }

            int in = c.getCount();
            c.moveToFirst();
            for (int i = 0; i < in; i++) {

                idk.add(c.getString(0));
                data.add(c.getString(1));
                name1.add(c.getString(2));
                inco1.add(c.getString(3));
                vwerk.add(c.getString(4));
                email.add(c.getString(5));
                tel.add(c.getString(6));
                c.moveToNext();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {

            setListAdapter(new EfficientAdapter(SoldToPartiesList.this));
            pb = (ProgressBar) findViewById(R.id.progressBar1);
            pb.setVisibility(View.INVISIBLE);
            searchable=true;
            con.close();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            idk.clear();
            data.clear();
            name1.clear();
            inco1.clear();
            vwerk.clear();
            email.clear();
            tel.clear();
            setListAdapter(new EfficientAdapter(SoldToPartiesList.this));
            pb = (ProgressBar) findViewById(R.id.progressBar1);
            pb.setVisibility(View.VISIBLE);
            searchable=false;
        }
    }

    private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        Cursor c;
        ProgressBar pb;

        @Override
        protected String doInBackground(String... urls) {
            con = new SqlConnector(co);
            try {
                c = con.select("select Kunnr,Name,Name1,Inco1,Vwerk,SmtpAddr,Telf1 from tb_soldtoparties where salesorg='"
                        + salorg + "' and channel='" + distch + "' and division='"+division+"';");

            } catch (Exception e) {

                e.printStackTrace();
            }

            int in = c.getCount();
            c.moveToFirst();
            Log.d("size", "" + in + "");
            for (int i = 0; i < in; i++) {

                idk.add(c.getString(0));
                data.add(c.getString(1));
                name1.add(c.getString(2));
                inco1.add(c.getString(3));
                vwerk.add(c.getString(4));
                email.add(c.getString(5));
                tel.add(c.getString(6));
                c.moveToNext();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {

            setListAdapter(new EfficientAdapter(SoldToPartiesList.this));
            pb = (ProgressBar) findViewById(R.id.progressBar1);
            pb.setVisibility(View.INVISIBLE);
            searchable=true;
            con.close();


        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            idk.clear();
            data.clear();
            name1.clear();
            inco1.clear();
            vwerk.clear();
            email.clear();
            tel.clear();
            pb = (ProgressBar) findViewById(R.id.progressBar1);
            pb.setVisibility(View.VISIBLE);
            searchable=false;
        }
    }


//  class ClickOnList implements OnItemClickListener
//    {
//        @Override
//        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
//                long arg3) {
//            Log.d("ListView", "Position"+arg2);
//
//        }
//
//    }


//  public OnItemClickListener theListListener = new OnItemClickListener() {
//   
//  public void onItemClick(android.widget.AdapterView<?> parent, View v, int position, long id) { 
//      Log.d("position",position+"");
//  } };


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        int place=position;
        Log.d("position",position+"");

    }
}

And Layout code is materiallist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/bluebg"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bar1"
        android:gravity="center_vertical"
        android:minHeight="50dp"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:gravity="center_vertical|left" >

            <TextView
                android:id="@+id/textViewtitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:shadowColor="#000000"
                android:shadowDx="1"
                android:shadowDy="1"
                android:shadowRadius="1.5"
                android:text="@string/materials"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout3"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_margin="5dp"
            android:gravity="center" >

            <ProgressBar
                android:id="@+id/progressBar1"
                style="?android:attr/progressBarStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="visible" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/linearLayout3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:id="@+id/linearLayout4"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center" >

                <EditText
                    android:id="@+id/editTextSearch"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:hint="@string/search"
                    android:imeOptions="actionDone"
                    android:inputType="textUri" >

                </EditText>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearLayoutsearch"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:gravity="center" android:clickable="true">

                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:src="@drawable/search" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout6"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/linearLayout7"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/listbg2" >


            <LinearLayout
                android:id="@+id/linearLayoutmc"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:background="@drawable/lbg1"
                android:gravity="center" >


                <TextView
                    android:id="@+id/textViewmc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:text="Code"
                    android:textColor="@color/black" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearLayoutmn"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:background="@drawable/lbg2"
                android:gravity="center" >




                <TextView
                    android:id="@+id/textViewmn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:text="Name"
                    android:textColor="@color/black" />

            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout10"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:divider="@color/offwhite" >
        </ListView>
    </LinearLayout>

</LinearLayout>

And listso3.xml is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/lbg"
        android:orientation="vertical" 
        >

        <TextView
            android:id="@+id/textViewNamelist3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/black" 
            android:focusable="false"/>

        <TextView
            android:id="@+id/textViewKunn2list3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/black" 
            android:focusable="false"/>
    </LinearLayout>
Was it helpful?

Solution

Add below code to your TextView in the XML

android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"

and try again.

Another simple solution: add android:descendantFocusability="blocksDescendants" to the root viewgroup.

OTHER TIPS

You should add android:focusable="false" for ListView row items to make ListView Clikable. Because the views in the row of ListView gain the focus so ListView is not focusable. So, in your case you can add android:focusable="false" to the TextViews of your ListView row.

I had the same symptoms, and it drove me crazy for a while. Adding android:focusable="false" for the list items as suggested above fixed the problem for me.

But the real issue was that I had set android:textIsSelectable="true" for my list items (in response to a Warning generated by Eclipse); setting android:textIsSelectable="false" fixed the problem for me, and I did not need the android:focusable="false" option at all.

The workaround I found avoid the

AdapterView.OnItemClickListener mMessageClickedHandler=new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


        }
    };

on the ListView, but exploit the Adapter constructor that takes a Context as parameter:

myCustomAdapter=new MyCustomAdapter(ActivityName.this,...)

Passing ActivityName.this is possible to cast the Context in the adapter's class as ActivityName in a safe way and use its methods working like callbacks:

((ActivityName)context).activityMethod()

Given that the getView() method of the Adapter class has a position parameter, is possible to pass this value to the activityMethod(int position) in order to know which list item has been pressed into the Activity where the ListView is.

Another solution. if you have many child-control,and its to trouble to add android:focusable="false" to every child-control,you can add android:descendantFocusability="blocksDescendants" to its parent-control.try it.

I struggled with this for a while - none of the provided solutions worked for me. In the end I found that having a call to getListView() in my onViewCreated() method did the trick, although I have no idea why. This is for a Fragment rather than an Activity; not sure if this makes any difference.

public class NewsListFragment extends ListFragment {
    private ListView listView;
    ...
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        newsListView = getListView();
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // this now works as expected
    }
}

I was also struggling with this issue and none of the suggested solutions worked. My problem was that I did an override for onViewCreated but didn't call super.onViewCreated(view, savedInstanceState);. Hopefully this will help someone else so they don't spend hours trying to figure out what's going on.

I struggled as I by mistake put android:inputType="textCapWords" with the textbox after removing it it is fixed

Only the line below worked for me:

android:descendantFocusability="blocksDescendants"

The whole list_view_item.xml:

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

    <!--android:focusableInTouchMode="false"-->
    <!--android:focusable="false"-->
    <!--android:clickable="false"-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:descendantFocusability="blocksDescendants"
    android:layout_height="wrap_content">
        <CheckBox
            android:id="@+id/checkBox_list_view_checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/checkBox_list_view_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
</LinearLayout>

The above is to be used in getView like:

convertView=LayoutInflater.from(getContext())
    .inflate(R.layout.list_view_item, parent, false);

`

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top