如何在ListView上修复OnItemClickListener

时间:2018-11-13 06:56:38

标签: android android-listview android-adapter onitemclicklistener listview-adapter

这是Java类MainActivity.java

package com.example.mhn.intercoapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.mhn.intercoapp.Adapters.EmployeeDirAdapter;
import com.example.mhn.intercoapp.static_class.EmployeeDir;

import java.util.ArrayList;

public class EmployeeDirectoryActivity extends AppCompatActivity {
    ListView listView;
    ImageView back_button;
    EmployeeDir obj;
    ArrayList <EmployeeDir> empDir ;
    EmployeeDirAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_employee_directory);
        listView = (ListView) findViewById(R.id.listView_emp_directory_xml);
        back_button = (ImageView) findViewById(R.id.back_button_header_emp_directory_activity_xml);
        obj = new EmployeeDir();
        empDir = new ArrayList<>();
        adapter = new EmployeeDirAdapter(getApplicationContext(),empDir);

        back_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
        obj.setEmp_name("Hafiz Sadique Umar");
        obj.setEmp_email("hsu@gmail.com");
        obj.setEmp_contact_num("+923045607057");
        empDir.add(obj);
        empDir.add(obj);
        empDir.add(obj);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapter, View v, int position,
                                    long arg3)
            {
                String value = (String)adapter.getItemAtPosition(position);
                Intent intent= new Intent(getApplicationContext(),DoneActivity.class);
                startActivity(intent);

                // assuming string and if you want to get the value on click of list item
                // do what you intend to do on click of listview row
            }
        });


    }
}

这是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".EmployeeDirectoryActivity">

    <RelativeLayout
        android:id="@+id/relatice_layout_header_emp_directory_activity_xml"
        android:background="@color/colorAccent"
        android:layout_width="match_parent"
        android:layout_alignParentTop="true"
        android:layout_height="56dp">

        <ImageView
            android:id="@+id/back_button_header_emp_directory_activity_xml"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentLeft="true"
            android:src="@drawable/back_arrow_header"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Employee Directory"
            android:textColor="#fff"
            android:textSize="18dp"
            android:textStyle="bold"
            android:layout_centerInParent="true"
            />
    </RelativeLayout>

    <ListView
        android:clickable="true"
        android:id="@+id/listView_emp_directory_xml"
        android:layout_below="@id/relatice_layout_header_emp_directory_activity_xml"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>



</RelativeLayout>

这是适配器

package com.example.mhn.intercoapp.Adapters;

import android.content.Context;
import android.content.Intent;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.mhn.intercoapp.MainActivity;
import com.example.mhn.intercoapp.R;
import com.example.mhn.intercoapp.static_class.EmployeeDir;

import java.util.ArrayList;

public class EmployeeDirAdapter extends BaseAdapter {
    Context context;
    private LayoutInflater inflater;

    private ArrayList<EmployeeDir> menuData=new ArrayList<>();

    public EmployeeDirAdapter(Context context, ArrayList<EmployeeDir>EmpDir)
    {
        this.context = context;
        this.menuData=EmpDir;
        inflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return menuData.size();
    }

    @Override
    public Object getItem(int i) {
        return menuData.get(i);
    }


    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolderEmpDir holder;
        if (convertView == null) {

            holder = new ViewHolderEmpDir();
            convertView = inflater.inflate(R.layout.emp_directory_list_row_layout, parent, false);
            holder.name = (TextView) convertView.findViewById(R.id.name_textView_emp_directory_list_row_layout);
            holder.email = (TextView) convertView.findViewById(R.id.email_textView_emp_directory_list_row_layout);
            holder.phone = (TextView) convertView.findViewById(R.id.phone);
            holder.pic = (ImageView) convertView.findViewById(R.id.emp_pic_emp_directory_list_row_layout);
            holder.viewButton = (ImageView) convertView.findViewById(R.id.view_button_emp_dir_list_row_layout);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolderEmpDir) convertView.getTag();
        }

        holder.name.setText(menuData.get(position).getEmp_name());
        holder.email.setText(menuData.get(position).getEmp_email());
        holder.phone.setText(menuData.get(position).getEmp_contact_num());
        //Linkify.addLinks(holder.email,Linkify.EMAIL_ADDRESSES);
        //Linkify.addLinks(holder.phone,Linkify.PHONE_NUMBERS);
        holder.pic.setImageResource(R.drawable.user_sign_up);

        holder.viewButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,MainActivity.class);
            }
        });
        //downloadImage(position,holder);
        return convertView;
    }


//    private void downloadImage(int position,ViewHolderShowAllBooking holder)
//    {
//        Picasso.with(context)
//                .load("http://www.efefoundation.net/inklink/uploads/artist/"+menuData.get(position).getArtistId()+".jpg")
//                .fit() // will explain later
//                .into(holder.profile);
//    }


    static class ViewHolderEmpDir
    {
        TextView name;
        TextView email;
        TextView phone;
        ImageView pic;
        ImageView viewButton;
    }
}

这是list_row_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:weightSum="4"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_marginTop="5dp"
        android:layout_centerHorizontal="true"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="70dp"
        android:background="@color/colorAccent">
        <ImageView
            android:id="@+id/emp_pic_emp_directory_list_row_layout"
            android:layout_marginTop="5dp"
            android:layout_width="80dp"
            android:layout_height="70dp"
            android:layout_marginBottom="5dp"
            android:src="@drawable/user_sign_up"/>
    </RelativeLayout>

<RelativeLayout
    android:background="@color/colorAccent"
    android:layout_marginTop="5dp"
    android:layout_width="0dp"
    android:layout_height="70dp"
    android:layout_weight="3">
        <TextView
            android:id="@+id/name_textView_emp_directory_list_row_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp"
            android:textColor="#fff"
            android:textSize="18dp"
            android:layout_alignParentLeft="true"
            android:text="Husnain"/>
    <TextView
        android:id="@+id/phone"
        android:layout_toRightOf="@id/name_textView_emp_directory_list_row_layout"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:autoLink="phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="***********"
        android:textColor="#fff"/>
    <TextView
        android:id="@+id/email_textView_emp_directory_list_row_layout"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/name_textView_emp_directory_list_row_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="mhn786@gmail.com"
        android:autoLink="email"
        android:layout_marginLeft="10dp"
        android:textSize="18dp"
        android:textColor="#fff"
        />
    <ImageView
        android:id="@+id/view_button_emp_dir_list_row_layout"
        android:src="@drawable/view"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"/>
    </RelativeLayout>

</LinearLayout>

当我应用OnItemClickListener时,它不会像未调用的func内的意图那样执行任何操作。我也尝试烤面包,但单击listView Items时未显示任何内容。 我也尝试了OnItemSlectedListener,它对我也不起作用。 此代码在这里有什么问题。 ?

2 个答案:

答案 0 :(得分:0)

您的library(xlsx) write.xlsx(x = iris, file = "C:/Users/USER/Desktop/iris.xls", row.names = FALSE) write.csv(x = iris, file = "C:/Users/USER/Desktop/iris.csv", row.names = FALSE) library(h2o) h2o.init() h2o.iris <- h2o.importFile(path = "C:/Users/USER/Desktop/iris.xls") #h2o.iris <- h2o.importFile(path = "C:/Users/USER/Desktop/iris.csv") 中的属性android:clickable="true"可能会覆盖其子视图的所有click事件,您可能应该将其删除:

ListView

答案 1 :(得分:0)

谢谢大家。问题是电子邮件和电话上的自动链接,我将它们设置为focusable = false但什么也没发生。然后我将其删除并猜测是什么? Clicklistener也开始工作。