ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – ListView项目不可点击.为什么?

2019-09-18 09:23:26  阅读:184  来源: 互联网

标签:android listview adaptor


我有一个使用自定义适配器的ListView,但我不能单击ListView项..

列表视图的活动..

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布局

<?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布局

<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>

解决方法:

试着这个来获得焦点:
    View.getFocus();

标签:android,listview,adaptor
来源: https://codeday.me/bug/20190918/1810997.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有