当OnClick项目时,ERROR在ListView中获取项目

时间:2017-10-23 03:51:45

标签: android listview onclick

我的名字ari ..我是Android开发的新手。也许你可以帮我解决我的问题。我想显示listview适配器中的数据,但是当我单击数据时。我的应用程序关闭。

我在listview中有一个显示数据的适配器,如:

ListAdapter.java

package com.santosa.sapasantosa.components;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.santosa.sapasantosa.R;

import java.util.ArrayList;

/**
 * Created by muhammadaa on 10/14/2017.
 */

public class ListAdapter extends BaseAdapter {

private Activity activity;
private static ArrayList nik;
private static ArrayList nama;
private static ArrayList email;
private static ArrayList phone;
private static ArrayList jabatan;
private static ArrayList departement;
private static ArrayList gender;
private static ArrayList status;
private static ArrayList label;
private static LayoutInflater inflater = null;


public ListAdapter(Activity a, ArrayList b, ArrayList c, ArrayList d, ArrayList e, ArrayList f, ArrayList g, ArrayList h, ArrayList i, ArrayList j) {
    activity = a;
    this.nik = b;
    this.nama = c;
    this.email = d;
    this.gender = e;
    this.status = f;
    this.label = g;
    this.phone = h;
    this.jabatan = i;
    this.departement = j;

    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
    return nik.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;

    if (convertView == null)
        vi = inflater.inflate(R.layout.listview_data_profile, null);


        TextView nik2 = (TextView) vi.findViewById(R.id.profNik); // nik
        String ambilNik = nik.get(position).toString();
        nik2.setText(ambilNik);


        TextView nama2 = (TextView) vi.findViewById(R.id.profNama); // nama
        String ambilNama = nama.get(position).toString();
        nama2.setText(ambilNama);

        TextView email2 = (TextView) vi.findViewById(R.id.profEmail); // email
        String ambilEmail = email.get(position).toString();
        email2.setText(ambilEmail);

        TextView phone2 = (TextView) vi.findViewById(R.id.profPhone); // phone
        String ambilPhone = phone.get(position).toString();
        phone2.setText(ambilPhone);

        TextView gender2 = (TextView) vi.findViewById(R.id.profGender); // gender
        String ambilGender = gender.get(position).toString();
        gender2.setText(ambilGender);

        TextView bagian = (TextView) vi.findViewById(R.id.profBagian); // Bagian
        String ambilJabatan = jabatan.get(position).toString();
        String ambilDepartement = departement.get(position).toString();
        bagian.setText(ambilJabatan+"/"+ambilDepartement);

        TextView status2 = (TextView) vi.findViewById(R.id.profStatus); // status
        String ambilStatus = status.get(position).toString();
        status2.setText(ambilStatus);

        TextView label2 = (TextView) vi.findViewById(R.id.countNumber); // label
        String ambilLabel = label.get(position).toString();
        label2.setText(ambilLabel);


    return vi;

}

}

此类片段用于使用适配器

AdminHomeFragment

package com.santosa.sapasantosa.view.admin;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringDef;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.santosa.sapasantosa.R;
import com.santosa.sapasantosa.components.ListAdapter;
import com.santosa.sapasantosa.components.RequestHandler;
import com.santosa.sapasantosa.components.SharedPrefManager;
import com.santosa.sapasantosa.configs.Constrant;
import com.santosa.sapasantosa.models.Employee;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import static android.provider.AlarmClock.EXTRA_MESSAGE;


/**
 * A simple {@link Fragment} subclass.
 */
 public class AdminHomeFragment extends Fragment {

ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

ArrayList<String> nik_array = new ArrayList<String>();
ArrayList<String> nama_array = new ArrayList<String>();
ArrayList<String> email_array = new ArrayList<String>();
ArrayList<String> phone_array = new ArrayList<String>();
ArrayList<String> gender_array = new ArrayList<String>();
ArrayList<String> jabatan_array = new ArrayList<String>();
ArrayList<String> departement_array = new ArrayList<String>();
ArrayList<String> status_array = new ArrayList<String>();
ArrayList<Integer> label_array = new ArrayList<>();
com.santosa.sapasantosa.components.ListAdapter adapter;


ListView listPeg;


public AdminHomeFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_admin_home, container, false);

    listPeg = (ListView) view.findViewById(R.id.pegListView);
    listPeg.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent i = new Intent(getActivity(),ProfileEmployeeActivity.class);

            HashMap<String,String> map = (HashMap) parent.getItemAtPosition(position);
            Log.e("","adad "+map);
            String empId = map.get("nik");


            i.putExtra("nik",empId);


            startActivity(i);
        }
    });

    loadDataPegawai();

    return view;
}



private void loadDataPegawai() {

    // get parameter
    final String users = SharedPrefManager.getInstance(getContext()).getUserEmployee().getNik();

    class LoadPegawai extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... voids) {
            //creating request handler object
            RequestHandler requestHandler = new RequestHandler();

            //creating request parameters
            HashMap<String, String> params = new HashMap<>();
            params.put("username", users);

            //returing the response
            return requestHandler.sendPostRequest(Constrant.URL_PROFIL, params);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);


            int n = 1;

            try {
                JSONObject obj = new JSONObject(s);


                if (!obj.getBoolean("error")) {
                    // get data user dari respone
                    JSONArray userJson = obj.getJSONArray("user");

                    for (int i = 0, count = userJson.length(); i < count; i++) {
                        try {
                            JSONObject jsonObject = userJson.getJSONObject(i);
                            if (jsonObject.getString("employeeStatus").toString().equals("0")) {
                                nik_array.add(jsonObject.getString("employeeID").toString());
                                nama_array.add(jsonObject.getString("employeeNama").toString());
                                email_array.add(jsonObject.getString("employeeEmail").toString());
                                gender_array.add(jsonObject.getString("employeeGender").toString());
                                status_array.add(jsonObject.getString("employeeStatus").toString());
                                phone_array.add(jsonObject.getString("employeePhone").toString());
                                jabatan_array.add(jsonObject.getString("employeeJabatan").toString());
                                departement_array.add(jsonObject.getString("employeeDepartement").toString());
                                label_array.add(n++);


                                String id = jsonObject.getString("employeeID").toString();
                                // adding to hashmap
                                HashMap<String,String> employees = new HashMap<>();
                                employees.put("nik",id);
                                list.add(employees);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    /* Set data to listview */
                    adapter = new com.santosa.sapasantosa.components.ListAdapter( getActivity(),
                            nik_array,
                            nama_array,
                            email_array,
                            gender_array,
                            status_array,
                            label_array,
                            phone_array,
                            jabatan_array,
                            departement_array);

                    listPeg.setAdapter(adapter);
                    listPeg.setTextFilterEnabled(true);


                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    //executing the async task
    LoadPegawai ru = new LoadPegawai();
    ru.execute();
}

}

当我在ListView中clik数据适配器时,我有一个错误:

enter image description here

您能帮我解决错误吗?我非常感谢你的帮助...

1 个答案:

答案 0 :(得分:0)

似乎您试图将所有员工显示为ListView ......如果我的理解错了,您应该:

  • 创建EmployeeBean
  • 将您的所有员工都放入一个集合......也许是ArrayList。

之后,当您单击ListView上的项目时,您获得的整数是集合中EmployeeBean的索引,以便您可以从集合中访问它。