ListView仅显示JSON中的一个项目

时间:2018-10-24 10:16:19

标签: java android listview

请帮帮我。查看我的代码。我的列表视图仅显示索引0中的一项。我使用过模型类。这些代码适用于我其余的应用程序。但不能为此工作。请帮帮我。

public class Tickout extends AppCompatActivity {

ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;

public static String MyCLASS;
public static String MyLESSON;

private String Ques;
private String Opt1;
private String Opt2;
private String Ans;

private ListView listView;
private ArrayList<DataModel> dataModelList;
private ArrayAdapter<DataModel> adapter;

ArrayList<String> quesList = new ArrayList<>();
ArrayList<String> opt1List = new ArrayList<>();
ArrayList<String> opt2List = new ArrayList<>();
ArrayList<String> ansList = new ArrayList<>();

public List<DataModel> tickList = new ArrayList<>();

private static String ActivityURL = "http://khushalcomputer.com/anuj/common.php?action=getmultiplechoice";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tickout);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    listView = findViewById(R.id.lvExp);

    setListViewAdapter();
    getItemList();
}

private void setListViewAdapter() {

    dataModelList = new ArrayList<DataModel>();
    adapter = new TickListAdapter(this, R.layout.tick_out_listitem, dataModelList);
    listView.setAdapter(adapter);
}


public void getItemList() {

    final ProgressDialog nDialog;
    nDialog = new ProgressDialog(this);
    nDialog.setMessage("Loading..");
    nDialog.setIndeterminate(false);
    nDialog.setCancelable(true);
    nDialog.show();

    String getTickOut = ActivityURL + "&lid=" + MyLESSON + "&cid=" + MyCLASS + "&type=tick%20the%20correct%20ans";

    Log.v("Data", getTickOut);

    StringRequest stringRequest = new StringRequest(Request.Method.GET, getTickOut, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONArray jsonArray = new JSONArray(response);

                JSONObject jsonObject;

                for (int i = 0; i < jsonArray.length(); i++) {

                    jsonObject = jsonArray.getJSONObject(i);

                    DataModel model = new DataModel();
                    model.setTickOutQues(jsonObject.getString("QUESTION").trim());
                    Log.v("Check Mate", model.getTickOutQues());
                    model.setTickOutOpt1(jsonObject.getString("OPTION1").trim());
                    model.setTickOutOpt2(jsonObject.getString("OPTION2").trim());
                    model.setTickOutAns(jsonObject.getString("TRUE_ANS").trim());



                    dataModelList.add(model);

                    adapter.notifyDataSetChanged();

                }

                nDialog.dismiss();

            } catch (JSONException e) {
                System.out.println("Error in code: " + e.getMessage());
                nDialog.dismiss();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(Tickout.this, "Time Out", Toast.LENGTH_LONG).show();
            nDialog.dismiss();
        }
    });
    RequestQueue requestqueue = Volley.newRequestQueue(this);
    requestqueue.add(stringRequest);

}
}


public class TickListAdapter extends ArrayAdapter<DataModel> {

private Activity activity;

private String busID;
private String busTime;
private String busName;

private String tempBusID;

public TickListAdapter(Activity activity, int resource, List<DataModel> dataModelList) {
    super(activity, resource, dataModelList);

    this.activity = activity;
}

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

    ViewHolder holder = null;
    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    //If holder not exist then loacte all view from UI file.

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.tick_out_listitem, parent, false);
        holder = new ViewHolder(convertView);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    DataModel model = getItem(position);

    holder.tickQues.setText(model.getTickOutQues().trim());

    Log.v("Check Data", model.getTickOutQues());

    holder.opt1.setText(model.getTickOutOpt1().trim());
    holder.opt2.setText(model.getTickOutOpt2().trim());
    holder.ans.setText(model.getTickOutAns().trim());

    return convertView;
}

private static class ViewHolder {
    private TextView tickQues;
    private CheckBox opt1;
    private CheckBox opt2;
    private TextView ans;

    public ViewHolder(View view) {

        tickQues = view.findViewById(R.id.tick_ques);
        opt1 = view.findViewById(R.id.optionA);
        opt2 = view.findViewById(R.id.optionB);
        ans = view.findViewById(R.id.tick_ans);
    }
}
}

0 个答案:

没有答案