启用ListView选择 - Android

时间:2012-02-08 10:47:06

标签: android android-listview

我遇到了奇怪的问题,我无法点击我的列表视图...我已经按照我之前的方式实现了它,但事情是它没有用。

listTags.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                listTags.setSelection(position);
                Toast.makeText(getParent(), "hello", Toast.LENGTH_LONG).show();
            }
        });

我正在扩展Activity类

并听取我如何宣布listview

listTags = (ListView) viewToLoad.findViewById(R.id.listPack);

听到我在xml中做了什么

 <ListView
        android:id="@+id/listPack"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="5dip"
        android:layout_marginLeft="5dip"
        android:layout_marginRight="5dip"
        android:layout_weight="1" >
    </ListView>

这和其他地方一样正常,我无法弄清楚是不是错了,请帮帮我。

谢谢

听取适配器的代码

adapter = new KeywordAdapter(getApplicationContext(), id,
                getLNApplication().getKeyworddetail());
listTags.setAdapter(adapter);

我的KeywordAdapter类

public class KeywordAdapter extends BaseAdapter {

    public KeywordAdapter(Context context, int id, ArrayList<ArrayList<Keyword>> keywordList) {
        this.context = context;
        if (id >= keywordList.size()) {
            keyWordList = new ArrayList<Keyword>();
        } else
            keyWordList = keywordList.get(id);
    }

    // Implemented methods for BaseAdpter

    public class ViewHolder {
        TextView tagName;
        //.... more code
    }

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

        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.package_tag_details, null, true);
            holder = new ViewHolder();
            holder.tagName = (TextView) view.findViewById(R.id.tagName);
            //.... more code
            holder.layout = (LinearLayout) view
                    .findViewById(R.id.linearLayout1);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.tagName.setText(keyWordList.get(position).getName());
        ArrayList<Integer> rank = keyWordList.get(position).getRank();


        @SuppressWarnings("unused")

        holder.tagRank1.setText(rank.get(position));


        //.... more code

        return view;
    }

    public void forceReload() {
        notifyDataSetChanged();

    }
}

4 个答案:

答案 0 :(得分:1)

只需在代码中更改以下内容即可。

listTags = (ListView)findViewById(R.id.listPack);

你确定android:layout_width支持“match_parent”吗?请看一下控制台。 愿这对你有所帮助。

答案 1 :(得分:1)

您在listView中放置了哪些项目,将所有项目设为android:focusable="false"

答案 2 :(得分:0)

我没有看到你设置适配器,即 listTags.setAdapter(?);

    Try this code (works for me):

    // MainActivity

    public class MainActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

           final ListView listTags = (ListView) findViewById(R.id.listPack);
           listTags.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, mStrings));
           listTags.setOnItemClickListener(new OnItemClickListener() {

               @Override
               public void onItemClick(AdapterView<?> parent, View view,
                       int position, long id) {
                   listTags.setSelection(position);
    //               Toast.makeText(getParent(), "hello", Toast.LENGTH_LONG).show();
               }
           });



        }


         private String[] mStrings = {

            "Comte", "Coolea", "Cooleney", "Coquetdale", "Corleggy", "Cornish Pepper",
            "Cotherstone", "Cotija", "Cottage Cheese", "Cottage Cheese (Australian)",
            "Cougar Gold", "Coulommiers", "Coverdale", "Crayeux de Roncq", "Cream Cheese",
            "Cream Havarti", "Crema Agria", "Crema Mexicana", "Creme Fraiche", "Crescenza",
            "Croghan", "Crottin de Chavignol", "Crottin du Chavignol", "Crowdie", "Crowley",
            "Cuajada", "Curd", "Cure Nantais", "Curworthy", "Cwmtawe Pecorino",
           };

    // Main 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:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello" />

        <ListView
            android:id="@+id/listPack"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginBottom="5dip"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:layout_weight="1" >
        </ListView>

            </LinearLayout>

    // list_item.xml // layout

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dp"
        android:textSize="16sp" >
    </TextView>

答案 3 :(得分:0)

列表视图的自定义视图存在一个问题 我在我删除的视图中使用了水平scrollView,现在问题解决了