QOnlistitemClick不适用于ListView?

Hi OnlistitemClick for ListView不工作。在这里,我使用asynctask从sqlite获取数据并将其显示在列表视图中。我想在ListView中的列表单击时执行一些操作。但点击没有发生。我为此做了很多尝试。请帮帮我。这是我的代码

    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+"");

    }
}

布局代码是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>

listso3.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="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>
2012-03-19 05:13:35  Jin
  • 在onListElemClick()中,将URE语句放在supr.onListElemClick()前面,如果它有效,则将dsee放在它前面。 – Shubhayu Mar 19 '12 at 5:00
  • 嗨,我把代码放在super之前,但没有工作 – Jin Mar 19 '12 at 5:17

A回答

  • 1

    将下面的代码添加到xml中的TextView

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

    再试一次。

    另一个简单的解决方案:将android:descendantFocusability="blocksDescendants"添加到根视图组。

    2014-07-01 03:36:39  dreamtale
    • 为什么会这样?我是说文本视图是不是在传递事件? – Murtaza Khursheed Hussain Dec 11 '13 at 7:46
    • 2
      @murtazazahussain textview会消耗touch事件,因此onitemclick将没有机会启动。 – dreamtale Dec 11 '13 at 8:10
  • 2

    您应该为listview行项添加android:focusable="false",以使listview可裁剪。因为listview行中的视图获得焦点,所以listview不可聚焦。因此,在您的示例中,您可以将android:focusable="false"添加到listview行的textviews中。

    2012-03-19 04:53:02  Lalit Poptani
    • 嗨,我添加了android:focusable=";false";用于文本视图,但现在也不起作用。 – Jin Mar 19 '12 at 5:05
    • 1
      android:focusable="false"显示更新xml – Lalit Poptani Mar 19 '12 at 5:07
    • 嗨,我已经更新了XML请检查。 – Jin Mar 19 '12 at 5:16
    • 尝试添加DreamTale所选择的内容。 – Lalit Poptani Mar 19 '12 at 5:22
    • 大家好,现在开始工作了……谢谢你的回答。:-) – Jin Mar 19 '12 at 5:34
  • 3我有同样的症状,这让我发疯了一段时间。为上面建议的列表项添加android:focusable="false"为我解决了这个问题。

    但是真正的问题是我已经为我的列表项目设置了{C.O.E.}(响应Eclipse生成的警告);设置android:textIsSelectable="true"为我解决了问题,并且我根本不需要{C.O.E.}选项。< /P>android:textIsSelectable="false"
    2013-06-05 16:41:04  likeitlikeit sguidos
  • 4

    我找到的解决方法避免了

    AdapterView.OnItemClickListener mMessageClickedHandler=new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
    
            }
        };
    
    <在{ }上,但利用以参数为参数的{ } }的适配器构造函数:< > >ListView以安全的方式在适配器类中以{ } }的形式传递{ } },并使用像回调那样工作的方法:Context

    假定适配器类的

    myCustomAdapter=new MyCustomAdapter(ActivityName.this,...)
    
    方法具有ActivityName.this参数,则可以将该值传递到Context,以便知道哪个列表项已被压缩到ActivityName是.

    的活动中。
    ((ActivityName)context).activityMethod()
    
    2018-02-27 21:11:48  Mattia Personaggio Uno Ducci
  • 5

    另一个解决方案。如果您有许多子控件,并且很难将android:focusable="false"添加到每个子控件,则可以将android:descendantFocusability="blocksDescendants"添加到其父控件。请尝试。

    2013-09-03 08:04:21  pan7an
  • 6

    我挣扎了一段时间-没有一个提供的解决方案对我有效。最后,我发现在我的getListView()方法中调用onViewCreated()确实起到了作用,尽管我不知道为什么。这是一个片段而不是一个活动;不确定这是否有任何区别。

    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
        }
    }
    
    2016-03-14 02:14:18  gregdev
  • 7

    我也在努力解决这个问题,但没有一个建议的解决方案奏效。我的问题是我对onViewCreated做了一个重写,但没有调用super.onViewCreated(view, savedInstanceState);。希望这能帮助其他人,这样他们就不会花上几个小时去想到底发生了什么。

    2016-03-25 08:23:38  primehalo
  • 8

    我努力挣扎,因为我错误地将android:inputtype=textcapwords与文本框放在一起,在删除它之后,它是固定的

    2016-08-14 06:03:48  user2746732
  • 9

    只有下面这一行对我有效:

    android:descendantFocusability="blocksDescendants"
    

    整个列表查看项。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>
    

    以上内容将在getview中使用,如:

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

    `

    2016-09-18 11:08:46  TastyCatFood