无法使用动态数据在RecyclerView中显示项目

时间:2017-10-17 16:49:04

标签: java android json android-recyclerview adapter

我正在尝试显示我从服务器获取的一些数据但是我无法在recyclerView中显示所获取的数据。数据有效且解析(JSON解析)运行良好,但我的活动仅显示白屏。 当我运行应用程序与一些虚拟数据应用工作正常,当我尝试设置从服务器解析的数据时问题到了。 这是我的MainClass

public class JSONDataViewer extends AppCompatActivity {

    Context context;
    //    private List<Movie> movieList
    private RecyclerView mRecyclerView;
    private CustomAdapter mAdapter;
    private List<MyData> dataList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jsondata_viewer);
        this.context = this;
        AndroidNetworking.initialize(getApplicationContext());

        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
//        mRecyclerView.setHasFixedSize(true);

//        loadDummyData();
        loadData();
        Log.e("DataListPopulated", "Data list populated");
        mAdapter = new CustomAdapter(dataList);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        Log.e("SettingAdapter", "Setting Adapter");
        mRecyclerView.setAdapter(mAdapter);
        Log.e("AdapterSet", "Adapter Set Success");
    }

    private void loadDummyData() {

        String question = "Question ";
        int qNo = 1;
        for (int i = 0; i < 10; i++) {
            qNo++;
            MyData data = new MyData("" + qNo, question + qNo);
            Log.e("Question" + " " + qNo,
                    "\nQuestion id = " + data.getQuestionId() +
                            " Question text = " + data.getQuestionText());
            dataList.add(data);

        }
    }

    private void loadData() {
        AndroidNetworking.post("http://www.skafs.com/survey/api/index.php")
                .addBodyParameter("tag", "getquestion")
                .addBodyParameter("category_id", "2")
                .setPriority(Priority.MEDIUM)
                .build()
                .getAsJSONObject(new JSONObjectRequestListener() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.i("LoadingData", "" + "Successful" + response);
                        Toast.makeText(context, "Response:\n" + "Loading Data" + response,
                                Toast.LENGTH_LONG).show();
                        boolean success = response.optBoolean("success");
                        if (success) {
                            Log.e("InsideSuccess", "Success = " + success);
                            try {
                                JSONArray questions =
                                        response.optJSONArray("questions");
                                Log.e("Questions", questions.toString());
                                for (int i = 0; i < questions.length(); i++) {
                                    JSONObject obj = questions.getJSONObject(i);
                                    MyData data = new MyData(
                                            obj.getString("question_id"),
                                            obj.getString("question_text"));
                                    Log.e("Question" + " " + i,
                                            "\nQuestion id = " + data.getQuestionId() +
                                                    " Question text = " + data.getQuestionText());
                                    dataList.add(data);
                                }
                                Log.e("ListData", "List Size is = "+dataList.size());
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onError(ANError anError) {
                        anError.printStackTrace();
                        Log.e("ParsingJSON", "" + "Failed getting json");
                    }
                });
    }
}

您可以看到上述代码loadDummyData();loadData();有两种方法尝试第一种方法没有问题。
我有一些Log语句,显示来自服务器的数据有效,dataList的大小为9。 这是我的数据格式Log

10-17 21:25:25.217 3078-3078/hostflippa.com.opencart_android E/InsideSuccess: Success = true
10-17 21:25:25.219 3078-3078/hostflippa.com.opencart_android E/Questions: [{"question_id":"1","question_text":"License Status","question_type":"radio","options":[{"option_id":"1","option_text":"Valid","option_value":"5"},{"option_id":"2","option_text":"Expired","option_value":"0"},{"option_id":"3","option_text":"Without License","option_value":"0"}]},{"question_id":"2","question_text":"License date of validity","question_type":"editText","options":[]},{"question_id":"3","question_text":"Type of License","question_type":"radio","options":[{"option_id":"4","option_text":"A-Category","option_value":"5"},{"option_id":"5","option_text":"B-Category","option_value":"2"},{"option_id":"6","option_text":"C-Category","option_value":"2"},{"option_id":"7","option_text":"Dispensar","option_value":"1"},{"option_id":"457","option_text":"20\/21","option_value":"1"}]},{"question_id":"4","question_text":"License Ownership","question_type":"radio","options":[{"option_id":"8","option_text":"Individual","option_value":"0"},{"option_id":"9","option_text":"Chain Pharmacy","option_value":"0"},{"option_id":"10","option_text":"Company","option_value":"0"}]},{"question_id":"5","question_text":"License Number","question_type":"editText","options":[]},{"question_id":"6","question_text":"Computerized License No","question_type":"editText","options":[]},{"question_id":"7","question_text":"NTN Present","question_type":"checkbox","options":[{"option_id":"11","option_text":"Yes","option_value":"1"},{"option_id":"12","option_text":"No","option_value":"0"}]},{"question_id":"8","question_text":"NTN No.","question_type":"editText","options":[]},{"question_id":"9","question_text":"Visit Type","question_type":"radio","options":[{"option_id":"13","option_text":"First visit","option_value":"0"},{"option_id":"14","option_text":"Second visit","option_value":"0"}]}]
10-17 21:25:25.219 3078-3078/hostflippa.com.opencart_android E/Question 0: Question id = 1 Question text = License Status
10-17 21:25:25.219 3078-3078/hostflippa.com.opencart_android E/Question 1: Question id = 2 Question text = License date of validity
10-17 21:25:25.220 3078-3078/hostflippa.com.opencart_android E/Question 2: Question id = 3 Question text = Type of License
10-17 21:25:25.220 3078-3078/hostflippa.com.opencart_android E/Question 3: Question id = 4 Question text = License Ownership
10-17 21:25:25.220 3078-3078/hostflippa.com.opencart_android E/Question 4: Question id = 5 Question text = License Number
10-17 21:25:25.220 3078-3078/hostflippa.com.opencart_android E/Question 5: Question id = 6 Question text = Computerized License No
10-17 21:25:25.221 3078-3078/hostflippa.com.opencart_android E/Question 6: Question id = 7 Question text = NTN Present
10-17 21:25:25.221 3078-3078/hostflippa.com.opencart_android E/Question 7: Question id = 8 Question text = NTN No.
10-17 21:25:25.221 3078-3078/hostflippa.com.opencart_android E/Question 8: Question id = 9 Question text = Visit Type
10-17 21:25:25.221 3078-3078/hostflippa.com.opencart_android E/ListData: List Size is = 9

请指出我做错事的方向。

没有任何异常。这是我的CustomAdapter课程

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {

    private List<MyData> dataList;

    public CustomAdapter(List<MyData> dataList) {
        this.dataList = dataList;
        Log.e("Constructor", "Working");
        Log.e("Constructor", "DataList Size = " + dataList.size());
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_lay, parent, false);
        Log.e("LayoutInflated", "Working");

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Log.e("OnBIndMethod", "OnBind Working");
        MyData data = dataList.get(position);
        holder.questionId.setText(data.getQuestionId());
        holder.questionText.setText(data.getQuestionText());
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView questionId, questionText;

        public MyViewHolder(View itemView) {
            super(itemView);
            questionId = (TextView) itemView.findViewById(R.id.question_id);
            questionText = (TextView) itemView.findViewById(R.id.question_text);
            Log.e("FindViewById", "Working");
        }
    }
}

MyData类有两个字符串类型及其getter / setter方法。

1 个答案:

答案 0 :(得分:3)

for (int i = 0; i < questions.length(); i++) {
                                JSONObject obj = questions.getJSONObject(i);
                                MyData data = new MyData(
                                        obj.getString("question_id"),
                                        obj.getString("question_text"));
                                Log.e("Question" + " " + i,
                                        "\nQuestion id = " + data.getQuestionId() +
                                                " Question text = " + data.getQuestionText());
                                dataList.add(data);
                            }
// notify data set change call missing
mAdapter.notifyDataSetChanged();