微调框仅显示在列表视图的第一项中,而不显示在其他项中

时间:2019-05-04 12:40:31

标签: android android-listview android-spinner

我有一个列表视图,其中包含一些数据。在每个列表视图项目中,我都添加了一个复选框和一个微调框。

我卡住的地方是我从API检索数据并在列表视图中显示数据,即使对于微调器我也是从另一个API获取数据。但是微调器仅显示在列表视图的第一项中,而不显示在其他项目中显示。

比方说,我有5个列表视图项目,在第0个项目中,微调器显示的是数据,而不是其余项目。请让我知道我将如何实现它。

这是MainActivity类:

    public class MainActivity extends Activity implements View.OnClickListener {

    ListView boxlistView;
    EditText totalAmount;
    TextView customerName;
    TextView balanceStatus;
    TextView balanceAmount;
    Button btnShowCheckedItems;
    List<MachinePojo> boxlist;
    ArrayList<String> planList = new ArrayList<String>();
    String strSelectedPlan="";

    CustomBoxAdapterList _customBoxAdapterList;
    Context mContext;
    Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prepaid);
        getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#034ea2")));

        customerName = findViewById(R.id.prepaid_customer_name);
        totalAmount = findViewById(R.id.total_amt_display);
        balanceStatus = findViewById(R.id.tv_balanceStatus);
        balanceAmount = findViewById(R.id.tv_balanceAmt);
        boxlistView = findViewById(R.id.lv_machineId);

        spinner = findViewById(R.id.box_plan_spinner);

        btnShowCheckedItems = findViewById(R.id.btnShowCheckedItems);

        new DisplayBoxList().execute();

        addListeners();

        new PlanSpinnerList().execute();

    }

    class DisplayBoxList extends AsyncTask<Object, String, String>{
        JSONObject requestObject = new JSONObject();


        @Override
        protected String doInBackground(Object... objects) {
            String result = "";
            try {
                String customerID = Global.getCustomerID();
                requestObject.put("customerId", customerID);

                result = ServiceHandler.postCustomerDetailsByID(requestObject.toString());

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

        @Override
        protected void onPostExecute(String result) {
            boxlist = new ArrayList<>();

            ArrayList<MachinePojo> testList = new ArrayList<>();


            boxlist.clear();
            try {
                JSONObject resultObject = new JSONObject(result);

                String accountName = resultObject.getString("Name");
                JSONArray subsscriptionArray = resultObject.getJSONArray("Machines");

                if (subsscriptionArray.length()>0) {
                    for (int i = 0; i < subsscriptionArray.length(); i++) {
                        JSONObject subscriptionObject = subsscriptionArray.getJSONObject(i);

                        MachinePojo item = new MachinePojo();
                        item.setBoxID(subscriptionObject.getString("machineId"));
                        Log.e("getting subscriptionId",item.getBoxID());

                        JSONObject planObject = subscriptionObject.getJSONObject("planDto");
                        String machineDescription = planObject.getString("description");
                        item.setMachineDescription(planDescription);
                        boxlist.add(item);
                    }

                }else
                {
                    boxlistView.setAdapter(null);
                }

                customerName.setText("Customer : "+accountName);
                fillListView();

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

    }


    public void fillListView(){
        _customBoxAdapterList = new CustomBoxAdapterList(getApplicationContext(),boxlist);

        boxlistView.setAdapter(_customBoxAdapterList);
        _customBoxAdapterList.notifyDataSetChanged();

        boxlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("clicked postion is", String.valueOf(position));
            }
        });

        boxlistView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {


            }


        });

    }

    class PlanSpinnerList extends AsyncTask<Object, String, String>{
        JSONObject requestObject = new JSONObject();
        @Override
        protected String doInBackground(Object... objects) {
            String result = "";
            try {


                String customerID = Global.getCustomerID();
                requestObject.put("customerId", customerID);

                result = ServiceHandler.getAllMachineDetails();

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

        @Override
        protected void onPostExecute(String result) {
            planList.clear();
            try {
                JSONArray responseArray = new JSONArray(result);
                for (int i=0;i<responseArray.length();i++){
                    JSONObject responseObj = responseArray.getJSONObject(i);
                    PlansPojo plans = new PlansPojo();
                    String descr = responseObj.getString("descr");
                    plans.setPlanName(description);
                    Log.e("plan descr",descr);

                    planList.add(description);
                }

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


            ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,planList);
            adapter.setDropDownViewResource(android.R.layout.simple_list_item_1);

            final Spinner sitems = findViewById(R.id.box_plan_spinner);

            sitems.setAdapter(adapter);

            sitems.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    int indx = planList.indexOf(sitems.getSelectedItem());
                    strSelectedPlan = planList.get(indx);

                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });


        }
    }

    private void addListeners() {
        btnShowCheckedItems.setOnClickListener(this);
    }




    @Override
    public void onClick(View v) {
        if(_customBoxAdapterList != null) {
            ArrayList<MachinePojo> mArrayProducts = _customBoxAdapterList.getCheckedItems();
            Log.d(MainActivity.class.getSimpleName(), "Selected Items: " + mArrayProducts.toString());
            Toast.makeText(getApplicationContext(), "Selected Items: " + mArrayProducts.toString(), Toast.LENGTH_LONG).show();
        }
    }

}

这是listview的自定义适配器类:

  public class CustomBoxAdapterList extends BaseAdapter{

    private Context mContext;
    private List<BoxPojo> boxes;
    SparseBooleanArray mSparseBooleanArray;
    int position=0;

    public CustomBoxAdapterList(Context context , List<BoxPojo> boxes){
        this.boxes = boxes;
        this.mContext = context;
        mSparseBooleanArray = new SparseBooleanArray();

    }

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

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

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

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

        if (convertView ==null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.boxes_list_item,parent,false);
        }

        BoxPojo TempBoxes = (BoxPojo) getItem(position);

        TextView tvBoxID = convertView.findViewById(R.id.tv_boxIDData);
        TextView tvPlanDes = convertView.findViewById(R.id.box_plan_name);

        if (TempBoxes.getBoxID() == " " || TempBoxes.getBoxID() == null){
            tvBoxID.setText("NA");
        }else{
            tvBoxID.setText(TempBoxes.getBoxID());
        }

        tvPlanDes.setText(TempBoxes.getPlanDescription());

        return convertView;
    }
}

0 个答案:

没有答案