listview位置值不超过5

时间:2017-07-17 09:21:01

标签: android listview position listitem getview

我使用自定义适配器来填充使用数据正确填充的列表视图。我有一些textview从数据库中获取数据。当我点击更新时,它应该从textview获取数据并将其呈现给edittext以进行更正或更新。但它无法正常工作。点击更新一段时间启用更多两行进行更新,并从原始数据更改数据。

请帮助纠正此问题

UpdatePathi.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    android:orientation="vertical"
    tools:context="com.app.nirvachan.rssb.UpdatePathiActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorHead"
        android:orientation="horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Pathi ID"
            android:padding="5dip"
            android:layout_weight="1"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Pathi Name"
            android:padding="5dip"
            android:layout_weight="1"/>



        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Center ID"
            android:padding="5dip"
            android:layout_weight="1"/>


        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Point ID"
            android:padding="5dip"
            android:layout_weight="1"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Contact No"
            android:padding="5dip"
            android:layout_weight="1"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.1"
            android:text="Options"
            android:padding="5dip"/>

    </LinearLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listpathi"></ListView>

</LinearLayout>

Custom_Pathi_Item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorList"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/layoutEdit"
        android:orientation="horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/txtPathiID"
            android:layout_weight="1"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/txtPathiName"
            android:layout_weight="1"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/txtCenterID"
            android:layout_weight="1"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/txtPointID"
            android:layout_weight="1"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/txtContactNo"
            android:layout_weight="1"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1.1">

            <ImageButton
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/btnEdit"
                android:layout_weight="1"
                android:src="@mipmap/ic_edit"
                android:textAllCaps="false"
                android:background="@android:color/transparent"/>

            <ImageButton
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/btnDelete"
                android:layout_weight="1"
                android:src="@mipmap/ic_delete"
                android:text="Delete"
                android:textAllCaps="false"
                android:background="@android:color/transparent"/>

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/layoutUpdate"
        android:visibility="gone"
        android:orientation="horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/etPathiID"
            android:layout_weight="1"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/etPathiName"
            android:textSize="10sp"
            android:inputType="textPersonName"
            android:layout_weight="1"/>

        <Spinner
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/etCenterID"
            android:layout_weight="1"/>

        <Spinner
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/etPointID"
            android:layout_weight="1"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inputType="number"
            android:id="@+id/etContactNo"
            android:textSize="10sp"
            android:layout_weight="1"/>

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/btnUpdate"
            android:src="@mipmap/ic_update"
            android:layout_weight="1.1"
            android:textAllCaps="false"
            android:background="@android:color/transparent"/>
    </LinearLayout>
</LinearLayout>

UpdatePathiActivity.java

package com.app.nirvachan.rssb;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class UpdatePathiActivity extends AppCompatActivity {

    ListView listPathi;
    JSONParser jsonParser = new JSONParser();
    String pathiID, pathiName, contactNo;
    int centerID, pointID;

    private static String url_get_pathi = "http://onkararts.000webhostapp.com/php/rssb_get_pathi.php";
    private static String url_get_point = "http://onkararts.000webhostapp.com/php/rssb_get_point.php";
    private static String url_get_center = "http://onkararts.000webhostapp.com/php/rssb_get_center.php";
    private static String url_update_pathi = "http://onkararts.000webhostapp.com/php/rssb_update_pathi.php";
    private static String url_delete_pathi = "http://onkararts.000webhostapp.com/php/rssb_delete_pathi.php";

    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PATHIS = "pathis";
    private static final String TAG_CENTERS = "centers";
    private static final String TAG_POINTS = "points";
    private static final String TAG_POINT_ID = "Point_ID";
    private static final String TAG_POINT_NAME = "Point_Name";
    private static final String TAG_CENTER_ID = "Center_ID";
    private static final String TAG_CENTER_NAME = "Center_Name";
    private static final String TAG_PATHI_ID = "Pathi_ID";
    private static final String TAG_PATHI_NAME = "Pathi_Name";
    private static final String TAG_CONTACT_NO = "Contact_No";


    ArrayList<Pathi> pathi_Data;
    UpdatePathiActivity.PathiAdapter a_pathi;
    User[] centers_data, points_data;
    SpinAdapter a_centers, a_points;
    JSONArray centers, points, pathis;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_pathi);

        listPathi = (ListView) findViewById(R.id.listpathi);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        UpdatePathiActivity.getCenters gc = new UpdatePathiActivity.getCenters();
        gc.execute();

        UpdatePathiActivity.getPoints gp = new UpdatePathiActivity.getPoints();
        gp.execute();

        UpdatePathiActivity.getPathis gr = new UpdatePathiActivity.getPathis();
        gr.execute();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle arrow click here
        if (item.getItemId() == android.R.id.home) {
            finish(); // close this activity and return to preview activity (if there is any)
        }

        return super.onOptionsItemSelected(item);
    }


    public class getPathis extends AsyncTask<String, String, ArrayList<Pathi>> {
        @Override
        protected void onPreExecute() {

        }

        @Override
        protected void onPostExecute(ArrayList<Pathi> pathis) {
            a_pathi = new UpdatePathiActivity.PathiAdapter(UpdatePathiActivity.this, pathis);
            listPathi.setAdapter(a_pathi);
        }


        @Override
        protected ArrayList<Pathi> doInBackground(String... args) {
            try {
                List<NameValuePair> params = new ArrayList<NameValuePair>();

                JSONObject json = jsonParser.makeHttpRequest(
                        url_get_pathi, "GET", params);

                Log.d("Getting Result", json.toString());

                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // vehicles found
                    // Getting Array of vehicles
                    pathis = json.getJSONArray(TAG_PATHIS);
                    pathi_Data = new ArrayList<Pathi>();
                    for (int i = 0; i < pathis.length(); i++) {
                        JSONObject c = pathis.getJSONObject(i);
                        Pathi r = new Pathi(Integer.parseInt(c.getString(TAG_PATHI_ID)), c.getString(TAG_PATHI_NAME), Integer.parseInt(c.getString(TAG_CENTER_ID)), Integer.parseInt(c.getString(TAG_POINT_ID)), Long.parseLong(c.getString(TAG_CONTACT_NO)));
                        pathi_Data.add(i,r);
                    }
                } else {

                }
            } catch (JSONException ex) {
                ex.getMessage().toString();
            }
            return pathi_Data;
        }
    }

    public class updatePathi extends AsyncTask<String, String, String>
    {
        String z = "";
        Boolean isSuccess = false;

        @Override
        protected void onPreExecute() {

        }

        @Override
        protected void onPostExecute(String r) {
            Toast.makeText(getBaseContext(),r,Toast.LENGTH_SHORT).show();

            if(isSuccess) {
                Intent i = new Intent(getBaseContext(), MainActivity.class);
                startActivity(i);
                finish();
            }

        }

        @Override
        protected String doInBackground(String... args) {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("Pathi_ID", pathiID));
            params.add(new BasicNameValuePair("Pathi_Name", pathiName));
            params.add(new BasicNameValuePair("Center_ID", centerID+""));
            params.add(new BasicNameValuePair("Point_ID", pointID+""));
            params.add(new BasicNameValuePair("Contact_No", contactNo));

            JSONObject json = jsonParser.makeHttpRequest(url_update_pathi,"POST", params);
            Log.d("Create Response", json.toString());
            try {
                int r = json.getInt(TAG_SUCCESS);
                if(r == 1){
                    z = "Record Updated Successfully";
                    isSuccess = true;
                }
                else{
                    isSuccess = false;
                    z = "Pathi Updation Failed";
                }
            }
            catch (JSONException ex) {
                z = ex.getMessage();
            }
            return z;
        }
    }

    public class deletePathi extends AsyncTask<String, String, String>
    {
        String z = "";
        Boolean isSuccess = false;

        @Override
        protected void onPreExecute() {

        }

        @Override
        protected void onPostExecute(String r) {
            Toast.makeText(getBaseContext(),r,Toast.LENGTH_SHORT).show();

            if(isSuccess) {
                Intent i = new Intent(getBaseContext(), MainActivity.class);
                startActivity(i);
                finish();
            }

        }

        @Override
        protected String doInBackground(String... args) {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("Pathi_ID", pathiID));

            JSONObject json = jsonParser.makeHttpRequest(url_delete_pathi,"POST", params);
            Log.d("Create Response", json.toString());
            try {
                int r = json.getInt(TAG_SUCCESS);
                if(r == 1){
                    z = "Record Deleted Successfully";
                    isSuccess = true;
                }
                else{
                    isSuccess = false;
                    z = "Pathi deletion Failed";
                }
            }
            catch (JSONException ex) {
                z = ex.getMessage();
            }
            return z;
        }
    }

    public class PathiAdapter extends BaseAdapter {

        Context context;
        List<Pathi> pathiList;

        public PathiAdapter(Context context, List<Pathi> pathiList) {
            this.context = context;
            this.pathiList = pathiList;
        }

        @Override
        public int getCount() {
            return pathiList.size();
        }

        @Override
        public Pathi getItem(int position) {
            return pathiList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return pathiList.indexOf(getItem(position));
        }

        private class ViewHolder{
            LinearLayout l_edit;
            TextView t_pathiID;
            TextView t_pathiName;
            TextView t_centerID;
            TextView t_pointID;
            TextView t_contactNo;
            ImageButton b_edit;
            ImageButton b_delete;
            LinearLayout l_update;
            TextView e_pathiID;
            EditText e_pathiName;
            Spinner e_centerID;
            Spinner e_pointID;
            EditText e_contactNo;
            ImageButton b_update;
        }

        UpdatePathiActivity.PathiAdapter.ViewHolder d_holder = null;

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.custom_pathi_item, null);
                d_holder = new UpdatePathiActivity.PathiAdapter.ViewHolder();
                d_holder.t_pathiID = (TextView) convertView.findViewById(R.id.txtPathiID);
                d_holder.t_pathiName = (TextView) convertView.findViewById(R.id.txtPathiName);
                d_holder.t_contactNo = (TextView) convertView.findViewById(R.id.txtContactNo);
                d_holder.t_centerID = (TextView) convertView.findViewById(R.id.txtCenterID);
                d_holder.t_pointID = (TextView) convertView.findViewById(R.id.txtPointID);
                d_holder.b_edit = (ImageButton) convertView.findViewById(R.id.btnEdit);
                d_holder.b_delete = (ImageButton) convertView.findViewById(R.id.btnDelete);
                d_holder.l_edit = (LinearLayout) convertView.findViewById(R.id.layoutEdit);
                d_holder.l_update = (LinearLayout) convertView.findViewById(R.id.layoutUpdate);
                d_holder.e_pathiID = (TextView) convertView.findViewById(R.id.etPathiID);
                d_holder.e_pathiName = (EditText) convertView.findViewById(R.id.etPathiName);
                d_holder.e_contactNo = (EditText) convertView.findViewById(R.id.etContactNo);
                d_holder.e_centerID = (Spinner) convertView.findViewById(R.id.etCenterID);
                d_holder.e_pointID = (Spinner) convertView.findViewById(R.id.etPointID);
                d_holder.b_update = (ImageButton) convertView.findViewById(R.id.btnUpdate);
                convertView.setTag(d_holder);
            }
            else{
                d_holder = (UpdatePathiActivity.PathiAdapter.ViewHolder) convertView.getTag();
            }

            Pathi row_pos = pathiList.get(position);
            d_holder.t_pathiID.setText(String.valueOf(row_pos.getPathiID()));
            d_holder.t_pathiName.setText(row_pos.getPathiName());
            d_holder.t_contactNo.setText(String.valueOf(row_pos.getContactNo()));
            d_holder.t_centerID.setText(String.valueOf(row_pos.getCenterID()));
            d_holder.t_pointID.setText(String.valueOf(row_pos.getPointID()));

            d_holder.b_edit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    View view = listPathi.getChildAt(position);
                    TextView u_pathiID = (TextView) view.findViewById(R.id.etPathiID);
                    EditText u_pathiName = (EditText) view.findViewById(R.id.etPathiName);
                    EditText u_contactNo = (EditText) view.findViewById(R.id.etContactNo);
                    Spinner u_centerID = (Spinner) view.findViewById(R.id.etCenterID);
                    Spinner u_pointID = (Spinner) view.findViewById(R.id.etPointID);
                    LinearLayout u_edit = (LinearLayout) view.findViewById(R.id.layoutEdit);
                    LinearLayout u_update = (LinearLayout) view.findViewById(R.id.layoutUpdate);
                    Pathi pathi  = getItem(position);
                    u_pathiID.setText(String.valueOf(pathi.getPointID()));
                    u_pathiName.setText(pathi.getPathiName());
                    u_contactNo.setText(String.valueOf(pathi.getContactNo()));
                    u_centerID.setAdapter(a_centers);
                    u_centerID.setSelection(pathi.getCenterID());
                    u_pointID.setAdapter(a_points);
                    u_pointID.setSelection(pathi.getPointID());
                    u_edit.setVisibility(View.GONE);
                    u_update.setVisibility(View.VISIBLE);
                }
            });

            d_holder.b_update.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    View view = listPathi.getChildAt(position);
                    TextView u_pathiID = (TextView) view.findViewById(R.id.etPathiID);
                    EditText u_pathiName = (EditText) view.findViewById(R.id.etPathiName);
                    EditText u_contactNo = (EditText) view.findViewById(R.id.etContactNo);
                    Spinner u_centerID = (Spinner) view.findViewById(R.id.etCenterID);
                    Spinner u_pointID = (Spinner) view.findViewById(R.id.etPointID);
                    pathiID = u_pathiID.getText().toString();
                    pathiName = u_pathiName.getText().toString();
                    contactNo = u_contactNo.getText().toString();
                    centerID = a_centers.getItem(u_centerID.getSelectedItemPosition()).getId();
                    pointID = a_centers.getItem(u_pointID.getSelectedItemPosition()).getId();
                    UpdatePathiActivity.updatePathi ur = new UpdatePathiActivity.updatePathi();
                    ur.execute();
                }
            });

            d_holder.b_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Pathi row_pos = pathiList.get(position);
                    pathiID = String.valueOf(row_pos.getPathiID());
                    //Toast.makeText(UpdateCenterActivity.this, centerID+centerName+address+contactNo+eMailID+secretaryName+secretaryNo, Toast.LENGTH_LONG).show();
                    UpdatePathiActivity.deletePathi dr = new UpdatePathiActivity.deletePathi();
                    dr.execute();
                }
            });

            return convertView;
        }
    }

    public class getCenters extends AsyncTask<String, String, User[]> {
        @Override
        protected void onPreExecute() {

        }

        @Override
        protected void onPostExecute(User[] centers) {
            a_centers = new SpinAdapter(getBaseContext(), android.R.layout.simple_spinner_item, centers);
        }


        @Override
        protected User[] doInBackground(String... args) {
            try {
                List<NameValuePair> params = new ArrayList<NameValuePair>();

                JSONObject json = jsonParser.makeHttpRequest(
                        url_get_center, "GET", params);

                Log.d("Getting Result", json.toString());

                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // vehicles found
                    // Getting Array of vehicles
                    centers = json.getJSONArray(TAG_CENTERS);
                    centers_data = new User[centers.length()+1];
                    // looping through All Vehicles
                    centers_data[0] = new User();
                    centers_data[0].setId(0);
                    centers_data[0].setName("Please Select Center Name");
                    for (int i = 0; i < centers.length(); i++) {
                        JSONObject c = centers.getJSONObject(i);
                        centers_data[i+1] = new User();
                        centers_data[i+1].setId(c.getInt(TAG_CENTER_ID));
                        centers_data[i+1].setName(c.getString(TAG_CENTER_NAME));
                    }
                } else {

                }
            } catch (JSONException ex) {
                ex.getMessage().toString();
            }
            return centers_data;
        }
    }


    public class getPoints extends AsyncTask<String, String, User[]> {
        @Override
        protected void onPreExecute() {

        }

        @Override
        protected void onPostExecute(User[] points) {
            a_points = new SpinAdapter(getBaseContext(), android.R.layout.simple_spinner_item, points);
        }


        @Override
        protected User[] doInBackground(String... args) {
            try {
                List<NameValuePair> params = new ArrayList<NameValuePair>();

                JSONObject json = jsonParser.makeHttpRequest(
                        url_get_point, "GET", params);

                Log.d("Getting Result", json.toString());

                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // vehicles found
                    // Getting Array of vehicles
                    points = json.getJSONArray(TAG_POINTS);
                    points_data = new User[points.length()+1];
                    points_data[0] = new User();
                    points_data[0].setId(0);
                    points_data[0].setName("Please Select Point Name");
                    for (int i = 0; i < points.length(); i++) {
                        JSONObject c = points.getJSONObject(i);
                        points_data[i+1] = new User();
                        points_data[i+1].setId(c.getInt(TAG_POINT_ID));
                        points_data[i+1].setName(c.getString(TAG_POINT_NAME));
                    }
                } else {

                }
            } catch (JSONException ex) {
                ex.getMessage().toString();
            }
            return points_data;
        }
    }

}

请找到问题的截图

这时我点击第6项 image

这时我点击第7项 image

3 个答案:

答案 0 :(得分:1)

修改您的代码,onClickListener应该在if else阻止

之外
public class PathiAdapter extends BaseAdapter {

Context context;
List<Pathi> pathiList;

public PathiAdapter(Context context, List<Pathi> pathiList) {
    this.context = context;
    this.pathiList = pathiList;
}

@Override
public int getCount() {
    return pathiList.size();
}

@Override
public Pathi getItem(int position) {
    return pathiList.get(position);
}

@Override
public long getItemId(int position) {
    return pathiList.indexOf(getItem(position));
}

private class ViewHolder{
    LinearLayout l_edit;
    TextView t_pathiID;
    TextView t_pathiName;
    TextView t_centerID;
    TextView t_pointID;
    TextView t_contactNo;
    ImageButton b_edit;
    ImageButton b_delete;
    LinearLayout l_update;
    TextView e_pathiID;
    EditText e_pathiName;
    Spinner e_centerID;
    Spinner e_pointID;
    EditText e_contactNo;
    ImageButton b_update;
}

UpdatePathiActivity.PathiAdapter.ViewHolder d_holder = null;

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.custom_pathi_item, null);
        d_holder = new UpdatePathiActivity.PathiAdapter.ViewHolder();
        d_holder.t_pathiID = (TextView) convertView.findViewById(R.id.txtPathiID);
        d_holder.t_pathiName = (TextView) convertView.findViewById(R.id.txtPathiName);
        d_holder.t_contactNo = (TextView) convertView.findViewById(R.id.txtContactNo);
        d_holder.t_centerID = (TextView) convertView.findViewById(R.id.txtCenterID);
        d_holder.t_pointID = (TextView) convertView.findViewById(R.id.txtPointID);
        d_holder.b_edit = (ImageButton) convertView.findViewById(R.id.btnEdit);
        d_holder.b_delete = (ImageButton) convertView.findViewById(R.id.btnDelete);
        d_holder.l_edit = (LinearLayout) convertView.findViewById(R.id.layoutEdit);
        d_holder.l_update = (LinearLayout) convertView.findViewById(R.id.layoutUpdate);
        d_holder.e_pathiID = (TextView) convertView.findViewById(R.id.etPathiID);
        d_holder.e_pathiName = (EditText) convertView.findViewById(R.id.etPathiName);
        d_holder.e_contactNo = (EditText) convertView.findViewById(R.id.etContactNo);
        d_holder.e_centerID = (Spinner) convertView.findViewById(R.id.etCenterID);
        d_holder.e_pointID = (Spinner) convertView.findViewById(R.id.etPointID);
        d_holder.b_update = (ImageButton) convertView.findViewById(R.id.btnUpdate);          

        convertView.setTag(d_holder);
    }
    else{
        d_holder = (UpdatePathiActivity.PathiAdapter.ViewHolder) convertView.getTag();

    }

    d_holder.b_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(context, "Clicked at :" +position, Toast.LENGTH_SHORT).show();

            }
        });   

    return convertView;
}
}

答案 1 :(得分:0)

只需在getView

中修改此代码即可
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.custom_pathi_item, null);
        d_holder = new UpdatePathiActivity.PathiAdapter.ViewHolder();
        d_holder.t_pathiID = (TextView) convertView.findViewById(R.id.txtPathiID);
        d_holder.t_pathiName = (TextView) convertView.findViewById(R.id.txtPathiName);
        d_holder.t_contactNo = (TextView) convertView.findViewById(R.id.txtContactNo);
        d_holder.t_centerID = (TextView) convertView.findViewById(R.id.txtCenterID);
        d_holder.t_pointID = (TextView) convertView.findViewById(R.id.txtPointID);
        d_holder.b_edit = (ImageButton) convertView.findViewById(R.id.btnEdit);
        d_holder.b_delete = (ImageButton) convertView.findViewById(R.id.btnDelete);
        d_holder.l_edit = (LinearLayout) convertView.findViewById(R.id.layoutEdit);
        d_holder.l_update = (LinearLayout) convertView.findViewById(R.id.layoutUpdate);
        d_holder.e_pathiID = (TextView) convertView.findViewById(R.id.etPathiID);
        d_holder.e_pathiName = (EditText) convertView.findViewById(R.id.etPathiName);
        d_holder.e_contactNo = (EditText) convertView.findViewById(R.id.etContactNo);
        d_holder.e_centerID = (Spinner) convertView.findViewById(R.id.etCenterID);
        d_holder.e_pointID = (Spinner) convertView.findViewById(R.id.etPointID);
        d_holder.b_update = (ImageButton) convertView.findViewById(R.id.btnUpdate);          

        convertView.setTag(d_holder);
    }
    else{
        d_holder = (UpdatePathiActivity.PathiAdapter.ViewHolder) convertView.getTag();

    }

    d_holder.b_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(context, "Clicked at :" +position, Toast.LENGTH_SHORT).show();

            }
        });   

    return convertView;
}

答案 2 :(得分:0)

Listview's onClickListener置于ifelse

之外
     if()
      {

      }
     else
      {

      }

   d_holder.b_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(context, "Clicked at :" +position, Toast.LENGTH_SHORT).show();

            }
        });