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

标签 java android json android-recyclerview adapter

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

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(); 尝试使用第一种方法没有问题。
我有一些日志语句显示来自服务器的数据有效,并且 dataList 的大小为 9。 这是我的数据表格日志

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 方法。

最佳答案

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();

关于java - 无法使用动态数据在 RecyclerView 中显示项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46795242/

相关文章:

java - 如何在 AsyncTask 之后更改 ActionBar 颜色?

java - 部署到 Tomcat 的 Spring boot WAR 和缺少静态资源的上下文

android - 在 Android 上使用模块库

java - 在 Android 中从 JSON 数组中获取值

ruby-on-rails - 从 Json 响应中删除属性

java - 两个用不同语言编写的应用程序可以通信吗?

java - 当鼠标移动而不是拖动时,如何在 Canvas 上移动二维图形

javascript - JSON中的 '@'是什么意思?

android - HTML 选择在 Android 中不起作用

具有多行的Android TextView