本文介绍了ListView的项目是不能点击。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView ,它使用一个定制的适配器,但我不能点击的ListView项目。

活动的列表视图。

 包com.adhamenaya.projects;

进口的java.util.ArrayList;

进口android.app.Activity;
进口android.content.Context;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.Button;
进口android.widget.Filter;
进口android.widget.Filterable;
进口android.widget.ListView;
进口android.widget.TextView;
进口android.widget.Toast;

进口com.adhamenaya.classes.Place;

公共类PlacesListActivity延伸活动{
    私人的ArrayList<地方>地方;
    私人的ArrayList<字符串>项目;
    GridviewAdapter mAdapter;
    私人的ListView lvPlaces;
    私人EfficientAdapter ADAP;
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.places_list);
        lvPlaces =(ListView控件)this.findViewById(R.id.lvPlaces);
        新DowanloadPlaces()执行();
    }
    私人无效BindList中(ArrayList中<地方>地方){
        this.places =场所;
        //开始创建的列表视图中显示的文章
        项目=新的ArrayList<字符串>();
        的for(int i = 0; I< places.size();我++){
            items.add(将String.valueOf(places.get(ⅰ).mName));
        }
        ADAP =新EfficientAdapter(本);
        adap.notifyDataSetChanged();
        lvPlaces.setAdapter(ADAP);
    }

    // EfficientAdapter:做一个自定义列表视图项
    公共类EfficientAdapter扩展了BaseAdapter实现过滤的{

        //充气的函数从XML布局文件对象(即main.xml中)转换为可编程
        LayoutInflater充气;
        上下文语境;

        公共EfficientAdapter(上下文的背景下){
            充气= LayoutInflater.from(上下文);
            this.context =背景;
        }

        公众诠释getCount将(){
            //获取列表中的项目数
            返回items.size();
        }

        公共对象的getItem(INT位置){
            //要在给定的位置,从列表返回项
            返回items.get(位置);
        }

        众长getItemId(INT位置){
            // TODO自动生成方法存根
            返回0;
        }

        公共查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
            ViewHolder持有人;
            如果(convertView == NULL){
                convertView = inflater.inflate(R.layout.adaptor_content,NULL);

                持有人=新ViewHolder(); //创建一个对象来保存在列表视图项组件
                holder.textLine =(TextView中)convertView.findViewById(R.id.textLine);
                holder.buttonLine =(按钮)convertView.findViewById(R.id.buttonLine);
                holder.buttonLine.setOnClickListener(新OnClickListener(){
                    私人诠释POS =位置;

                    公共无效的onClick(视图v){
                        places.remove(POS);
                        BindList中(的地方); //绑定列表项
                        Toast.makeText(getApplicationContext(),已删除successfuly :),Toast.LENGTH_LONG).show();
                    }
                });
                convertView.setTag(保持器);
            } 其他 {
                支架=(ViewHolder)convertView.getTag();
            }
            //与支架有效地绑定的数据。
            holder.textLine.setText(将String.valueOf(places.get(位置).mName));
            返回convertView;
        }

        公共过滤用getFilter(){
            // TODO自动生成方法存根
            返回null;
        }

    }

    // ViewHolder:即重新$ P $类psents列表视图项目
    静态类ViewHolder {
        TextView的文本行;
        按钮buttonLine;
    }

    // DownloadRSSFeedsTask:工作在一个单独的线程
    私有类DowanloadPlaces扩展的AsyncTask<字符串,无效的ArrayList<地方>> {

        @覆盖
        受保护的ArrayList<地方> doInBackground(字符串... PARAMS){
            ArrayList的<地方>地方=新的ArrayList<地方>();
            将P =新的地方();
            的for(int i = 0;我α25;我++){
                p.mName =阿尔Mathaf的酒店;
                places.add(对);
            }

            返回的地方;
        }

        @覆盖
        保护无效onPostExecute(ArrayList中<地方>地方){
            BindList中(的地方);


        }

    }


}
 

places_list.xml布局

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
    <的ListView
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:ID =@ + ID / lvPlaces>

    < / ListView控件>
< / LinearLayout中>
 

adaptor_content.xml布局

 < ImageView的
    机器人:ID =@ + ID / imageView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignLeft =@ + ID / TEXTLINE
    机器人:layout_centerVertical =真
    机器人:SRC =@可绘制/设置/>

< / RelativeLayout的>
 

解决方案

试试这个获取焦点:    View.getFocus();

I have a ListView that uses a customized adapter, but I can't click on the ListView Item ..

Activity for list view ..

package com.adhamenaya.projects;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.adhamenaya.classes.Place;

public class PlacesListActivity extends Activity {
    private ArrayList<Place> places;
    private ArrayList<String> items;
    GridviewAdapter mAdapter;
    private ListView lvPlaces;
    private EfficientAdapter adap;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.places_list);
        lvPlaces = (ListView) this.findViewById(R.id.lvPlaces);
        new DowanloadPlaces().execute("");
    }
    private void bindList(ArrayList<Place> places) {
        this.places = places;
        // Start creating the list view to show articles
        items = new ArrayList<String>();
        for (int i = 0; i < places.size(); i++) {
            items.add(String.valueOf(places.get(i).mName));
        }
        adap = new EfficientAdapter(this);
        adap.notifyDataSetChanged();
        lvPlaces.setAdapter(adap);
    }

    // EfficientAdapter : to make a customized list view item
    public class EfficientAdapter extends BaseAdapter implements Filterable {

        // The function of inflater to convert objects from XML layout file (i.e. main.xml) to a programmable
        LayoutInflater inflater;
        Context context;

        public EfficientAdapter(Context context) {
            inflater = LayoutInflater.from(context);
            this.context = context;
        }

        public int getCount() {
            // Get the number of items in the list
            return items.size();
        }

        public Object getItem(int position) {
            // To return item from a list in the given position
            return items.get(position);
        }

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

        public View getView(final int position, View convertView,ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.adaptor_content, null);

                holder = new ViewHolder();// Create an object to hold at components in the list view item
                holder.textLine = (TextView) convertView.findViewById(R.id.textLine);
                holder.buttonLine = (Button) convertView.findViewById(R.id.buttonLine);
                holder.buttonLine.setOnClickListener(new OnClickListener() {
                    private int pos = position;

                    public void onClick(View v) {
                        places.remove(pos);
                        bindList(places);// to bind list items
                        Toast.makeText(getApplicationContext(),"Deleted successfuly :)", Toast.LENGTH_LONG).show();
                    }
                });
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            // Bind the data efficiently with the holder.
            holder.textLine.setText(String.valueOf(places.get(position).mName));
            return convertView;
        }

        public Filter getFilter() {
            // TODO Auto-generated method stub
            return null;
        }

    }

    // ViewHolder : class that represents a list view items
    static class ViewHolder {
        TextView textLine;
        Button buttonLine;
    }

    // DownloadRSSFeedsTask: works in a separate thread
    private class DowanloadPlaces extends AsyncTask<String, Void, ArrayList<Place>> {

        @Override
        protected ArrayList<Place> doInBackground(String... params) {
            ArrayList<Place> places = new ArrayList<Place>();
            Place p = new Place();
            for(int i =0;i<25;i++){
                p.mName = "Al Mathaf Hotel";
                places.add(p);
            }

            return places;
        }

        @Override
        protected void onPostExecute(ArrayList<Place> places) {
            bindList(places);


        }

    }


}

places_list.xml layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lvPlaces">

    </ListView>
</LinearLayout>

adaptor_content.xml layout

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textLine"
    android:layout_centerVertical="true"
    android:src="@drawable/settings" />

</RelativeLayout>
解决方案

try this to get the focus: View.getFocus();

这篇关于ListView的项目是不能点击。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:21