没有从适配器获取适当的数据到下一个活动

时间:2015-10-12 10:36:56

标签: android listview baseadapter

我是Android新手。我正在开发一个应用程序,其中我有一个员工列表。列表视图包含文本视图,即员工姓名和三个图像视图,即单击一个图像视图我必须从数据库中删除该员工,并且在单击另一个图像时,我必须编辑我有另一个活动的详细信息。点击此编辑图像,我想开始新的活动,并使用将在edittext中显示的意图传递相应员工的详细信息。但这不能正常工作。如果我点击第一个员工的删除图像,其他员工将被删除。当将数据传递给EditActivity时,数据也不合适。

请帮帮我。怎么做?

在我的MainActivity中,我以JSONArray格式从服务器获取数据,我在列表视图中显示数据。我不明白为什么我没有得到正确的数据。我在MainActivity或适配器中有什么问题吗?

这是我的适配器

public class EmployeesAdapter extends BaseAdapter {
    TextView categoryName;
    Pojo pojo;
    private Context activity1;
    ArrayList<Pojo> data1;
    private ArrayList<Pojo> arraylist1 = null;
    public static LayoutInflater inflater;
    ImageView edit, delete, historyy;
    String del_empid;

    public EmployeesAdapter(Context ctx, ArrayList<Pojo> employee1) {
         activity1 = ctx;
         data1 = employee1;
         inflater = (LayoutInflater) activity1
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         this.arraylist1 = new ArrayList<Pojo>();
         this.arraylist1.addAll(data1);
    }

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

    @Override
    public Object getItem(int position) {
        return data1.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
         View v = convertView;
         v = inflater.inflate(R.layout.manage_emp_list, parent, false);
         pojo = data1.get(position);
         categoryName = (TextView) v.findViewById(R.id.employeeName);
         categoryName.setText(pojo.getMgEmpName());
         edit = (ImageView) v.findViewById(R.id.imgedit);
         delete = (ImageView) v.findViewById(R.id.imgdelete);
         historyy = (ImageView) v.findViewById(R.id.imgHistory);
         final int pos = position;
    edit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Pojo domain = data1.get(pos);
            String emp_name = domain.getMgEmpName();
            String emp_contact = domain.getMgEmpContact();
            String emp_email = domain.getMgEmpEmail();
            String emp_appcost = domain.getMgEmpAppCost();
            Intent i = new Intent(activity1, EditEmployee.class);
            i.putExtra("empname", emp_name);
            i.putExtra("empmobile", emp_contact);
            i.putExtra("empemail", emp_email);
            i.putExtra("empappcost", emp_appcost);
            activity1.startActivity(i);

        }
    });

    delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            del_empid = data1.get(position).getMgEmp_id();
            new NetCheck().execute();
        }
    });
    return v;
}  

这是Mainactivity,我从服务器获取数据,我在listview中显示它。在这里,我只显示员工姓名,列表项中有三个图像。但是我在列表视图中为每个项目提供了多个数据。我正在使用setter和getter函数。此外,我想在从服务器加载之前显示以前的数据。之前我使用过SharedPreferences。我在MainActivty中做错了什么,这就是为什么我在adpter中遇到这种类型的问题?

 public class EmployeesActivity extends AppCompatActivity {
Pojo pojo;
String strServerResponse = null;
ArrayList<Pojo> employee1;
EmployeesAdapter mainAdapter;
String tipsss;
ArrayList<String> tii;
ListView mainList;
String emplid;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_employees);
    mainList = (ListView) findViewById(R.id.manageemployeeList);
    employee1 = new ArrayList<Pojo>();
    tii = new ArrayList<String>();
    new NetCheck().execute();
};


private class NetCheck extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        SharedPreferences prefs = getSharedPreferences("MyPref4", MODE_PRIVATE);

        Pojo pojo;
        Set<String> names = prefs.getStringSet("employeename", null);
        Set<String> ids = prefs.getStringSet("employeeid", null);
        Set<String> mobiles = prefs.getStringSet("employeemobile", null);
        Set<String> emails = prefs.getStringSet("employeeemail", null);
        Set<String> appcosts = prefs.getStringSet("employeeappcost", null);

        List<String> nameList = new ArrayList<String>(0);
        nameList.addAll(names);
        List<String> idList = new ArrayList<String>(0);
        idList.addAll(ids);
        List<String> mobileList = new ArrayList<String>(0);
        mobileList.addAll(mobiles);
        List<String> emailidList = new ArrayList<String>(0);
        emailidList.addAll(emails);
        List<String> appcostidList = new ArrayList<String>(0);
        appcostidList.addAll(appcosts);
        employee1.clear();
        for (int i = 0; i < names.size(); i++) {
            pojo = new Pojo();
            pojo.setMgEmpName(nameList.get(i));
            pojo.setMgEmp_id(idList.get(i));
            pojo.setMgEmpContact(mobileList.get(i));
            pojo.setMgEmpEmail(emailidList.get(i));
            pojo.setMgEmpAppCost(appcostidList.get(i));
            employee1.add(pojo);
        }
        mainAdapter = new EmployeesAdapter(EmployeesActivity.this, employee1);
        mainList.setAdapter(mainAdapter);

        return;
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {

            SharedPreferences preff = getSharedPreferences(
                    "MyPref", MODE_PRIVATE);
            String id = preff.getString("id", null);
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpRequest = new HttpPost(
                    "http://url");
            httpRequest.setHeader("Content-Type", "application/json");
            JSONObject json = new JSONObject();
            json.put("employer_id", id);
            StringEntity se = new StringEntity(json.toString());
            se.setContentEncoding("UTF-8");
            se.setContentType("application/json");
            httpRequest.setEntity(se);
            HttpResponse httpRes = httpClient.execute(httpRequest);

            java.io.InputStream inputStream = httpRes.getEntity()
                    .getContent();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream);
            BufferedReader reader = new BufferedReader(inputStreamReader);
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            inputStream.close();
            strServerResponse = sb.toString();

            if (strServerResponse != null) {
                try {

                    JSONArray arr = new JSONArray(strServerResponse);
                    JSONObject jsonObj = arr.getJSONObject(0);
                    ArrayList<String> tii = new ArrayList<String>();
                    ArrayList<String> tii2 = new ArrayList<String>();
                    ArrayList<String> tii3 = new ArrayList<String>();
                    ArrayList<String> tii4 = new ArrayList<String>();
                    ArrayList<String> tii5 = new ArrayList<String>();
                    for (int i = 0; i < arr.length(); i++) {
                        pojo = new Pojo();
                        JSONObject jobj2 = arr.getJSONObject(i);
                        String empname = jobj2.optString("name");
                        String empid = jobj2.optString("id");
                        String empmobile = jobj2.optString("mobile");
                        String empemail = jobj2.optString("email");
                        String empappcost = jobj2.optString("app_cost");
                        tii.add(empname);
                        tii2.add(empid);
                        tii3.add(empmobile);
                        tii4.add(empemail);
                        tii5.add(empappcost);
                    }

                    List<String> listTemp = tii;
                    List<String> listTemp1 = tii2;
                    List<String> listTemp2 = tii3;
                    List<String> listTemp3 = tii4;
                    List<String> listTemp4 = tii5;

                    Set<String> temp = new HashSet<String>(listTemp);
                    Set<String> temp2 = new HashSet<String>(listTemp1);
                    Set<String> temp3 = new HashSet<String>(listTemp2);
                    Set<String> temp4 = new HashSet<String>(listTemp3);
                    Set<String> temp5 = new HashSet<String>(listTemp4);
                    SharedPreferences.Editor editor = getSharedPreferences("MyPref4", MODE_PRIVATE).edit();
                    temp.addAll(listTemp);
                    temp2.addAll(listTemp1);
                    temp3.addAll(listTemp2);
                    temp4.addAll(listTemp3);
                    temp5.addAll(listTemp4);
                    editor.putStringSet("employeename", temp);
                    editor.putStringSet("employeeid", temp2);
                    editor.putStringSet("employeemobile", temp3);
                    editor.putStringSet("employeeemail", temp4);
                    editor.putStringSet("employeeappcost", temp5);
                    editor.commit();

                } 

这是我的Pojo课程,我有员工详细信息的setter和getter函数。

  public class Pojo {
       private String mg_empName;
       private String mg_empEmail;
       private String mg_empContact;

       public void setMgEmpContact(String mg_empContact) {
            this.mg_empContact = mg_empContact;
       }

       public String getMgEmpContact() {
            return mg_empContact;
       }

       public void setMgEmpEmail(String mg_empEmail) {
            this.mg_empEmail = mg_empEmail;
       }

       public String getMgEmpEmail() {
            return mg_empEmail;
       }

       public void setMgEmpName(String mg_empName) {
             this.mg_empName = mg_empName;
       }

       public String getMgEmpName() {
            return mg_empName;
       }

  }

任何人都可以帮助我。为什么我会遇到这种类型的问题?这里有什么不对吗?我使用Arraylist然后在postExecute()方法中设置数据的方式是正确的吗?......

3 个答案:

答案 0 :(得分:0)

使用此代码...

 final int pos = position;
    edit.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
Pojo domain = data1.get(pos);
                String emp_name = domain.getMgEmpName();
                String emp_contact = domain.getMgEmpContact();
                String emp_email = domain.getMgEmpEmail();
                String emp_appcost = domain.getMgEmpAppCost();
                Intent i = new Intent(activity1, EditEmployee.class);
                i.putExtra("empname", emp_name);
                i.putExtra("empmobile", emp_contact);
                i.putExtra("empemail", emp_email);
                i.putExtra("empappcost", emp_appcost);
                activity1.startActivity(i);

            }
        });

它完美无缺。

答案 1 :(得分:0)

而不是初始化pojo = data1.get(position);使本地变量为final Pojo pojo = data1.get(position); inside the getView() 因为在你的情况下,pojo字段被视图化,因为getview被称为所有行都是可见的

答案 2 :(得分:0)

尝试使用

  this.arraylist1 = employee1;