首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ListView中对JSON数据使用复选框

问如何在ListView中对JSON数据使用复选框
EN

Stack Overflow用户
提问于 2017-03-10 02:32:46
回答 2查看 668关注 0票数 0

我是新来的Android开发者。我想有人帮我。现在,我可以将JSON数据调用到我的ListView。我确实将复选框导入到ListView中。但这不是工作。当我在某些复选框上被选中时,将有其他复选框被选中,如下面的示例事件。

复选框名称

X A

_B

_C

_D

_E

_F

__________________ <<底部屏幕

X-G

_H

_i

当我被选中"A“复选框时。也选中了另一个复选框(复选框G)。

  1. 我需要知道如何解决这个案子。
  2. 如何做,如果我要显示所有复选框被选中后,单击按钮提交。

对不起,我的英语能力不好。请帮帮我先生。

SearchActivity.java

​

代码语言:javascript
复制
package com.example.rattapongt.rjp_b;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.rattapongt.rjp_b.Date.myCalendarView;
import com.example.rattapongt.rjp_b.Util.Config;
import com.example.rattapongt.rjp_b.Util.RequestHandler;

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

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

public class SearchActivity extends AppCompatActivity implements View.OnClickListener {

    private ProgressDialog loading;
    private String JSON_STRING;

    ListView listView;
    TextView tvDate, tvDateHide;
    EditText editTextId, editTextId2;
    ImageView ivSearch;
    Button btnDate, btnReject;

    private int mYear;
    private int mMonth;
    private int mDay;

    private String mYearCheck;

    static final int CALENDAR_VIEW_ID = 1;


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

        listView = (ListView) findViewById(R.id.listView);

        editTextId = (EditText) findViewById(R.id.editTextId);
        editTextId2 = (EditText) findViewById(R.id.editTextId2);

        ivSearch = (ImageView) findViewById(R.id.ivSearch);

        tvDate = (TextView) findViewById(R.id.tvDate);
        tvDateHide = (TextView) findViewById(R.id.tvDateHide);

        btnDate = (Button) findViewById(R.id.btnDate);
        btnReject = (Button) findViewById(R.id.btnReject);

        ivSearch.setOnClickListener(this);
        btnReject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //Intent intent = new Intent(SearchActivity.this, RejectActivity.class);
                //startActivity(intent);
            }
        });

        editTextId.setText("1992");
        editTextId2.setText("PCB116A731 ");

        // add a click listener to the button
        btnDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(SearchActivity.this, myCalendarView.class);
                startActivityForResult(intent, CALENDAR_VIEW_ID);

            }
        });


    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case CALENDAR_VIEW_ID:
                if (resultCode == RESULT_OK) {

                    Bundle bundle = data.getExtras();
                    tvDate.setText(bundle.getString("dateSelected1"));
                    tvDateHide.setText(bundle.getString("dateSelected"));
                    break;
                }
        }
    }



    private void getJSON() {
        class GetJSON extends AsyncTask<Void, Void, String> {

            ProgressDialog loading;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(SearchActivity.this, "Fetching Data", "Wait...", false, false);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                JSON_STRING = s;

              showEmployee();
            }

            @Override
            protected String doInBackground(Void... params) {
                RequestHandler rh = new RequestHandler();
                String s = rh.sendGetRequest(Config.URL_GET_ALL);

                return s;
            }
        }
        GetJSON gj = new GetJSON();
        gj.execute();

    }


    private void showEmployee() {
        JSONObject jsonObject = null;
        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        try {
            jsonObject = new JSONObject(JSON_STRING);
            JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);

            for (int i = 0; i < result.length(); i++) {
                JSONObject jo = result.getJSONObject(i);
                String Ta = jo.getString(Config.TAG_A);
                String Tb = jo.getString(Config.TAG_B);
                String Tc = jo.getString(Config.TAG_C);
                String Td = jo.getString(Config.TAG_D);
                String Te = jo.getString(Config.TAG_E);
                String Tf = jo.getString(Config.TAG_F);
                String Tg = jo.getString(Config.TAG_G);


                HashMap<String, String> employees = new HashMap<>();
                employees.put(Config.TAG_A, Ta);
                employees.put(Config.TAG_B, Tb);
                employees.put(Config.TAG_C, Tc);
                employees.put(Config.TAG_D, Td);
                employees.put(Config.TAG_E, Te);
                employees.put(Config.TAG_F, Tf);
                employees.put(Config.TAG_G, Tg);

                list.add(employees);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        ListAdapter adapter = new SimpleAdapter(
                SearchActivity.this, list, R.layout.item_list,
                new String[]{Config.TAG_A, Config.TAG_B, Config.TAG_C, Config.TAG_D, Config.TAG_E, Config.TAG_F, Config.TAG_G},
                new int[]{R.id.invoiceNo, R.id.code, R.id.name, R.id.itemNo, R.id.poNo, R.id.sq, R.id.qty});

        listView.setAdapter(adapter);
    }

    private void getData() {

        loading = ProgressDialog.show(this, "Please wait...", "Fetching...", false, false);

        //String url = Config.DATA_URL + editTextId.getText().toString().trim();
        String url = Config.DATA_URL + "&strKeyword=" + editTextId.getText().toString().trim()
                + "&strKeyword3=" + tvDateHide.getText().toString().trim();

        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                loading.dismiss();
                showJSON(response);

            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //Toast.makeText(SearchActivity.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
                        loading.dismiss();
                        lostConnection();

                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }



    private void showJSON(String response) {
        JSONObject jsonObject = null;
        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        try {
            JSONObject jsonObjectSearch = new JSONObject(response);
            JSONArray result = jsonObjectSearch.getJSONArray(Config.JSON_ARRAY);

            //For map values for JSON to android
            /*
            JSONObject collegeData = result.getJSONObject(0);
            name = collegeData.getString(Config.KEY_NAME);
            address = collegeData.getString(Config.KEY_ADDRESS);
            vc = collegeData.getString(Config.KEY_VC);
            */

            for (int i = 0; i < result.length(); i++) {
                JSONObject jo = result.getJSONObject(i);
                String Ka = jo.getString(Config.TAG_A);
                String Kb = jo.getString(Config.TAG_B);
                String Kc = jo.getString(Config.TAG_C);
                String Kd = jo.getString(Config.TAG_D);
                String Ke = jo.getString(Config.TAG_E);
                String Kf = jo.getString(Config.TAG_F);
                String Kg = jo.getString(Config.TAG_G);

                HashMap<String, String> employees = new HashMap<>();
                employees.put(Config.TAG_A, Ka);
                employees.put(Config.TAG_B, Kb);
                employees.put(Config.TAG_C, Kc);
                employees.put(Config.TAG_D, Kd);
                employees.put(Config.TAG_E, Ke);
                employees.put(Config.TAG_F, Kf);
                employees.put(Config.TAG_G, Kg);
                list.add(employees);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        //That will be show information to textView.
        /*
        if (!name.toString().equals("null")) {
            //textViewResult.setText("Name:\t" + name + "\nDesignation:\t" + address + "\nSalary:\t" + vc);
            Toast.makeText(this, response, Toast.LENGTH_LONG).show();
        } else {
            textViewResult.setText("Data not found!");
        }
        //textViewResult.setText("Name:\t" + name + "\nDesignation:\t" + address + "\nSalary:\t" + vc);
*/

        ListAdapter adapter = new SimpleAdapter(
                SearchActivity.this, list, R.layout.item_list,
                new String[]{Config.KEY_A, Config.KEY_B, Config.KEY_C, Config.KEY_D, Config.KEY_E, Config.KEY_F, Config.KEY_G},
                new int[]{R.id.invoiceNo, R.id.code, R.id.name, R.id.itemNo, R.id.poNo, R.id.sq, R.id.qty});

        //Toast.makeText(this, response, Toast.LENGTH_LONG).show();
        listView.setAdapter(adapter);


    }



   

}

​

CustomAdapter.java

​

代码语言:javascript
复制
package com.example.rattapongt.rjp_b.Util;

/**
 * Created by Rattapongt on 2/23/2017.
 */

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.example.rattapongt.rjp_b.R;

public class CustomAdapter extends BaseAdapter {
    Context mContext;
    String[] strName;
    String[] strName1;
    String[] strName2;
    String[] strName3;
    String[] strName4;
    String[] strName5;
    String[] strName6;
    int[] resId;

    public CustomAdapter(Context context, String[] strName, String[] strName1, String[] strName2, String[] strName3, String[] strName4, String[] strName5, String[] strName6) {
        this.mContext = context;
        this.strName = strName;
        this.strName1 = strName1;
        this.strName2 = strName2;
        this.strName3 = strName3;
        this.strName4 = strName4;
        this.strName5 = strName5;
        this.strName6 = strName6;
        this.resId = resId;
    }

    public int getCount() {
        return strName.length;
    }

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

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


    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater mInflater =
                (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (view == null)
            view = mInflater.inflate(R.layout.item_list, parent, false);

        TextView textView = (TextView) view.findViewById(R.id.textView);
        textView.setText(strName[position]);

        TextView textView1 = (TextView) view.findViewById(R.id.textView1);
        textView1.setText(strName1[position]);

        TextView textView2 = (TextView) view.findViewById(R.id.textView2);
        textView2.setText(strName2[position]);

        TextView textView3 = (TextView) view.findViewById(R.id.textView3);
        textView3.setText(strName3[position]);

        TextView textView4 = (TextView) view.findViewById(R.id.textView4);
        textView4.setText(strName4[position]);

        TextView textView5 = (TextView) view.findViewById(R.id.textView5);
        textView5.setText(strName5[position]);

        TextView textView6 = (TextView) view.findViewById(R.id.textView6);
        textView6.setText(strName6[position]);


        return view;
    }
}

​

EN

回答 2

Stack Overflow用户

发布于 2017-03-10 10:21:04

获取所选复选框的位置,并将其存储在数组中,然后在getView方法中将其与选定的位置列表进行比较。如果列表设置了该位置,则选中复选框,将其取消。

票数 1
EN

Stack Overflow用户

发布于 2017-03-10 06:18:48

我认为删除if(view==null)将执行job.Try,如下所示:

代码语言:javascript
复制
   @Override
        public View getView(int position, View view, ViewGroup parent) {
     LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  view=inflater.inflate(R.layout.grid_parent_cat_individual_items, null);


      TextView textView = (TextView) view.findViewById(R.id.textView);
            textView.setText(strName[position]);

            TextView textView1 = (TextView) view.findViewById(R.id.textView1);
            textView1.setText(strName1[position]);

            TextView textView2 = (TextView) view.findViewById(R.id.textView2);
            textView2.setText(strName2[position]);

            TextView textView3 = (TextView) view.findViewById(R.id.textView3);
            textView3.setText(strName3[position]);

            TextView textView4 = (TextView) view.findViewById(R.id.textView4);
            textView4.setText(strName4[position]);

            TextView textView5 = (TextView) view.findViewById(R.id.textView5);
            textView5.setText(strName5[position]);

            TextView textView6 = (TextView) view.findViewById(R.id.textView6);
            textView6.setText(strName6[position]);


            return view;
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42709125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档