You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android解析Assets本地JSON到RecyclerView不显示问题排查

Troubleshooting RecyclerView Not Showing Local JSON Data in Android

Hey there, let’s work through why your RecyclerView isn’t displaying data from your local JSON file. Based on the code and JSON you shared, here are key checks and fixes to try:

1. Validate Your JSON Format

First, your sample JSON has an invalid trailing comma and ellipsis at the end of the array:

{ "box": [ { "text" : "text1 " }, { "text" : "text2" }, { "text" : "text3" }, ............... ] }

This will cause a JSONException during parsing. Clean up the JSON to remove the extra comma and ellipsis, making sure it’s strictly valid. You can use your IDE’s built-in JSON validator to catch syntax errors.

2. Verify JSON File Location & Naming

  • Ensure file.json is placed directly in the assets folder (not inside a subfolder) under the main directory of your project.
  • Android is case-sensitive with filenames: confirm the filename matches exactly (e.g., File.json vs file.json will break the load).

3. Check Data Loading & Parsing

Add a log statement after parsing to confirm if data is actually being loaded:

// After adding items to the data list
Log.d("MainActivity", "Loaded item count: " + data.size());

If the count is 0, double-check:

  • The key name "box" matches exactly what’s in your JSON (case-sensitive).
  • The key "text" is spelled correctly in both the JSON and your AllTable.getString("text") call.

4. Fix RecyclerView Initialization Order

You’re setting the adapter before the Layout Manager. While this might work in some cases, it’s better to set the Layout Manager first—this is required for RecyclerView to properly layout items:

// Swap these two lines
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
recyclerView.setAdapter(mAdapter);

5. Inspect Your BoxAdapter Implementation

Make sure your adapter is correctly binding data and returning the right item count:

Check getItemCount()

@Override
public int getItemCount() {
    // Ensure this returns the size of your data list
    return mData != null ? mData.size() : 0;
}

Check onBindViewHolder()

@Override
public void onBindViewHolder(BoxViewHolder holder, int position) {
    Box item = mData.get(position);
    // Confirm you’re setting the text to the correct TextView in your item layout
    holder.textView.setText(item.getText());
}

Also, verify that your item layout’s TextView has the correct ID and isn’t set to gone or invisible.

6. Confirm RecyclerView Layout Attributes

In your XML layout, ensure the RecyclerView has a valid size (not wrap_content if you’re starting with empty data, though it should work if data is loaded):

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/mRecycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp"/>

7. Improve Error Logging

Your current Toast shows e.toString(), which isn’t very detailed. Replace it with proper logging to see exactly where parsing fails:

catch (JSONException e) {
    Log.e("MainActivity", "Failed to parse JSON", e);
    Toast.makeText(MainActivity.this, "JSON parse error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}

8. Ensure RequestJson() is Called at the Right Time

Make sure you’re calling RequestJson() after setContentView() in onCreate(), otherwise findViewById(R.id.mRecycler) will return null:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RequestJson(); // Call here, not before setContentView
}

Start with these checks—most likely the issue is either invalid JSON, a typo in key names, or a missing Layout Manager setup. Let me know if you still hit snags after trying these!

内容的提问来源于stack exchange,提问作者J DEO

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:12:32