JSON 加载/获取正确但无法显示

Posted

技术标签:

【中文标题】JSON 加载/获取正确但无法显示【英文标题】:JSON loading/fetching properly but unable to display 【发布时间】:2017-10-08 15:32:20 【问题描述】:

检查了我的 JSON 提取是否正确,没有错误,因为解析完成得很完美,但应用程序没有显示任何结果(显示了一个空列表视图)。 如果有人可以帮助我解决这个问题,那就提供我所有的文件。 注意:该应用程序没有显示任何错误,它只是显示空白列表视图。

WordActivity.java

package com.example.anandparmeetsingh.books;

import android.app.LoaderManager;
import android.content.Context;
import android.content.Loader;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class WordActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<Word>> 
    private static final int EARTHQUAKE_LOADER_ID = 1;
    private static final String LOG_TAG = WordActivity.class.getName();
    /**
     * URL for earthquake data from the USGS dataset
     */
    private static final String USGS_REQUEST_URL =
            "https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2014-01-01&endtime=2014-01-02";
    /**
     * Adapter for the list of earthquakes
     */
    public WordAdapter mAdapter;
    private TextView mEmptyStateTextView;

    @Override
    protected void onCreate(Bundle bundle) 
        super.onCreate(bundle);
        setContentView(R.layout.activity_word);
        // Get a reference to the LoaderManager, in order to interact with loaders.

        mAdapter = new WordAdapter(this, new ArrayList<Word>());


        ListView earthquakeListView = (ListView) findViewById(R.id.list);
        // Create a new adapter that takes an empty list of earthquakes as input
        final WordAdapter adapter = new WordAdapter(this, new ArrayList<Word>());


        // Set the adapter on the @link ListView
        // so the list can be populated in the user interface

        earthquakeListView.setAdapter(mAdapter);

        // Set an item click listener on the ListView, which sends an intent to a web browser
        // to open a website with more information about the selected earthquake.


        // Start the AsyncTask to fetch the earthquake data
        //mEmptyStateTextView = (TextView) findViewById(R.id.empty_view);
        //earthquakeListView.setEmptyView(mEmptyStateTextView);
        ConnectivityManager connMgr = (ConnectivityManager)
                getSystemService(Context.CONNECTIVITY_SERVICE);

        // Get details on the currently active default data network
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

        // If there is a network connection, fetch data
        if (networkInfo != null && networkInfo.isConnected()) 
            // Get a reference to the LoaderManager, in order to interact with loaders.
            LoaderManager loaderManager = getLoaderManager();

            // Initialize the loader. Pass in the int ID constant defined above and pass in null for
            // the bundle. Pass in this activity for the LoaderCallbacks parameter (which is valid
            // because this activity implements the LoaderCallbacks interface).
            loaderManager.initLoader(EARTHQUAKE_LOADER_ID, null, this);
         else 
            // Otherwise, display error
            // First, hide loading indicator so error message will be visible
            //View loadingIndicator = findViewById(R.id.loading_indicator);
            //loadingIndicator.setVisibility(View.GONE);

             //Update empty state with no connection error message
            //mEmptyStateTextView.setText("No Connection");
        

    

    public Loader<List<Word>> onCreateLoader(int i, Bundle bundle) 
        return new WordLoader(this, USGS_REQUEST_URL);
    

    /**
     * @link  to perform the network request on a background thread, and then
     * update the UI with the list of earthquakes in the response.
     * <p>
     * AsyncTask has three generic parameters: the input type, a type used for progress updates, and
     * an output type. Our task will take a String URL, and return an EarthquakeAdapter. We won't do
     * progress updates, so the second generic is just Void.
     * <p>
     * We'll only override two of the methods of AsyncTask: doInBackground() and onPostExecute().
     * The doInBackground() method runs on a background thread, so it can run long-running code
     * (like network activity), without interfering with the responsiveness of the app.
     * Then onPostExecute() is passed the result of doInBackground() method, but runs on the
     * UI thread, so it can use the produced data to update the UI.
     */

    @Override
    public void onLoadFinished(Loader<List<Word>> loader, List<Word> earthquakes) 
        // Clear the adapter of previous earthquake data
        //View loadingIndicator = findViewById(R.id.loading_indicator);
        //loadingIndicator.setVisibility(View.GONE);
        //mEmptyStateTextView.setText(R.string.no_earthquakes);
        mAdapter.clear();
        // If there is a valid list of @link Earthquakes, then add them to the adapter's
        // data set. This will trigger the ListView to update.
        if (earthquakes != null && !earthquakes.isEmpty()) 
            mAdapter.addAll(earthquakes);
        
    

    @Override
    public void onLoaderReset(Loader<List<Word>> loader) 
        // Loader reset, so we can clear out our existing data.
        mAdapter.clear();
    



Word.java

package com.example.anandparmeetsingh.books;

public class Word 

    private String mMagnitude;
    private String mLocation;
    private Long mDate;
    private String mUrl;

    public Word(String magnitude, String location, String url) 
        mMagnitude = magnitude;
        mLocation = location;
        mUrl = url;


    
    public Word(String magnitude, String location, Long date, String url) 
        mMagnitude = magnitude;
        mLocation = location;
        mDate = date;
        mUrl = url;


    

    public String getMagnitude() 
        return mMagnitude;
    

    public String geLocation() 
        return mLocation;
    

    public Long getDate() 
        return mDate;
    
    public String getUrl()return mUrl;





WordAdapter.java

package com.example.anandparmeetsingh.books;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by ParmeetSingh on 5/9/2017.
 */

public class WordAdapter extends ArrayAdapter<Word> 
    String primaryLocation;
    String locationOffset;
    String description;
    private static final String LOCATION_SEPARATOR = " of ";

    public WordAdapter(Activity context, ArrayList<Word> words) 
        // Here, we initialize the ArrayAdapter's internal storage for the context and the list.
        // the second argument is used when the ArrayAdapter is populating a single TextView.
        // Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
        // going to use this second argument, so it can be any value. Here, we used 0.
        super(context, 0, words);
    

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

        // Check if the existing view is being reused, otherwise inflate the view
        View listItemView = convertView;
        if (listItemView == null) 
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.activity_list_display, parent, false);
        
        // Get the @link AndroidFlavor object located at this position in the list
        Word currentWord = getItem(position);

        // Find the TextView with view ID magnitude
        TextView magnitudeView = (TextView) listItemView.findViewById(R.id.magnitude);
        // Format the magnitude to show 1 decimal place

        // Display the magnitude of the current earthquake in that TextView
        magnitudeView.setText(description);

        String originalLocation = currentWord.geLocation();
        // Find the TextView in the list_item.xml layout with the ID version_number
        TextView primaryLocationView = (TextView) listItemView.findViewById(R.id.primary_location);
        // Display the location of the current earthquake in that TextView
        primaryLocationView.setText(primaryLocation);

        // Find the TextView with view ID location offset
        TextView locationOffsetView = (TextView) listItemView.findViewById(R.id.location_offset);
        // Display the location offset of the current earthquake in that TextView
        locationOffsetView.setText(locationOffset);


        //Date dateObject = new Date(currentWord.getDate());

        // Find the TextView with view ID date
        TextView dateView = (TextView) listItemView.findViewById(R.id.date);
        // Format the date string (i.e. "Mar 3, 1984")
        //String formattedDate = formatDate(dateObject);
        // Display the date of the current earthquake in that TextView
        dateView.setText(primaryLocation);

        // Find the TextView with view ID time
        TextView timeView = (TextView) listItemView.findViewById(R.id.time);
        // Format the time string (i.e. "4:30PM")
        //String formattedTime = formatTime(locationOffset);
        // Display the time of the current earthquake in that TextView
        timeView.setText(locationOffset);


        return listItemView;
    
    private String formatMagnitude(double magnitude) 
        DecimalFormat magnitudeFormat = new DecimalFormat("0.0");
        return magnitudeFormat.format(magnitude);
    
    /**
     * Return the formatted date string (i.e. "Mar 3, 1984") from a Date object.
     */
    private String formatDate(Date dateObject) 
        SimpleDateFormat dateFormat = new SimpleDateFormat("LLL dd, yyyy");
        return dateFormat.format(dateObject);
    
    private String formatTime(Date dateObject) 
        SimpleDateFormat timeFormat = new SimpleDateFormat("h:mm a");
        return timeFormat.format(dateObject);
    


WordLoader.java

package com.example.anandparmeetsingh.books;

import android.content.AsyncTaskLoader;
import android.content.Context;

import java.util.List;

/**
 * Created by ParmeetSingh on 5/9/2017.
 */

public class WordLoader extends AsyncTaskLoader<List<Word>> 

    /** Tag for log messages */
    private static final String LOG_TAG = WordLoader.class.getName();

    /** Query URL */
    private String mUrl;

    /**
     * Constructs a new @link WordLoader.
     *
     * @param context of the activity
     * @param url to load data from
     */
    public WordLoader(Context context, String url) 
        super(context);
        mUrl = url;
    

    @Override
    protected void onStartLoading() 
        forceLoad();
    

    /**
     * This is on a background thread.
     */
    @Override
    public List<Word> loadInBackground() 
        if (mUrl == null) 
            return null;
        

        // Perform the network request, parse the response, and extract a list of earthquakes.
        List<Word> earthquakes = QueryUtils.fetchEarthquakeData(mUrl);
        return earthquakes;
    

QueryUtils.java

package com.example.anandparmeetsingh.books;

import android.text.TextUtils;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ParmeetSingh on 5/9/2017.
 */

public class QueryUtils 

    /**
     * Tag for the log messages
     */
    private static final String LOG_TAG = QueryUtils.class.getSimpleName();

    /**
     * Create a private constructor because no one should ever create a @link QueryUtils object.
     * This class is only meant to hold static variables and methods, which can be accessed
     * directly from the class name QueryUtils (and an object instance of QueryUtils is not needed).
     */
    private QueryUtils() 

    

    /**
     * Query the USGS dataset and return a list of @link WordAdapter objects.
     */
    public static List<Word> fetchEarthquakeData(String requestUrl) 
        // Create URL object
        URL url = createUrl(requestUrl);

        // Perform HTTP request to the URL and receive a JSON response back
        String jsonResponse = null;
        try 
            Thread.sleep(2000);
         catch (InterruptedException e) 
            e.printStackTrace();
        
        try 
            jsonResponse = makeHttpRequest(url);
         catch (IOException e) 
            Log.e(LOG_TAG, "Problem making the HTTP request.", e);
        

        // Extract relevant fields from the JSON response and create a list of @link EarthquakeAdapters
        List<Word> earthquake = extractFeatureFromJson(jsonResponse);

        // Return the list of @link EarthquakeAdapters
        return earthquake;
    

    /**
     * Returns new URL object from the given string URL.
     */
    private static URL createUrl(String stringUrl) 
        URL url = null;
        try 
            url = new URL(stringUrl);
         catch (MalformedURLException e) 
            Log.e(LOG_TAG, "Problem building the URL ", e);
        
        return url;
    

    /**
     * Make an HTTP request to the given URL and return a String as the response.
     */
    private static String makeHttpRequest(URL url) throws IOException 
        String jsonResponse = "";

        // If the URL is null, then return early.
        if (url == null) 
            return jsonResponse;
        

        HttpURLConnection urlConnection = null;
        InputStream inputStream = null;
        try 
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setReadTimeout(10000 /* milliseconds */);
            urlConnection.setConnectTimeout(15000 /* milliseconds */);
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            // If the request was successful (response code 200),
            // then read the input stream and parse the response.
            if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) 

                inputStream = urlConnection.getInputStream();
                jsonResponse = readFromStream(inputStream);
             else 
                Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode());
            
         catch (IOException e) 
            Log.e(LOG_TAG, "Problem retrieving the earthquake JSON results.", e);
         finally 
            if (urlConnection != null) 
                urlConnection.disconnect();
            
            if (inputStream != null) 
                // Closing the input stream could throw an IOException, which is why
                // the makeHttpRequest(URL url) method signature specifies than an IOException
                // could be thrown.
                inputStream.close();
            
        
        return jsonResponse;
    

    /**
     * Convert the @link InputStream into a String which contains the
     * whole JSON response from the server.
     */
    private static String readFromStream(InputStream inputStream) throws IOException 
        StringBuilder output = new StringBuilder();
        if (inputStream != null) 
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
            BufferedReader reader = new BufferedReader(inputStreamReader);
            String line = reader.readLine();
            while (line != null) 
                output.append(line);
                line = reader.readLine();
            
        
        return output.toString();
    

    /**
     * Return a list of @link WordAdapter objects that has been built up from
     * parsing the given JSON response.
     */

    private static List<Word> extractFeatureFromJson(String earthquakeJSON) 
        // If the JSON string is empty or null, then return early.
        if (TextUtils.isEmpty(earthquakeJSON)) 
            return null;
        

        // Create an empty ArrayList that we can start adding earthquakes to

        List<Word> earthquakes = new ArrayList<>();


        // Try to parse the JSON response string. If there's a problem with the way the JSON
        // is formatted, a JSONException exception object will be thrown.
        // Catch the exception so the app doesn't crash, and print the error message to the logs.
        try 


            // Create a JSONObject from the JSON response string
            JSONObject baseJsonResponse = new JSONObject(earthquakeJSON);
            // Extract the JSONArray associated with the key called "features",
            // which represents a list of features (or earthquakes).
            JSONArray earthquakeArray = baseJsonResponse.getJSONArray("features");

            // For each earthquake in the earthquakeArray, create an @link EarthquakeAdapter object
            for (int i = 0; i < earthquakeArray.length(); i++) 

                // Get a single earthquake at position i within the list of earthquakes
                JSONObject currentEarthquake = earthquakeArray.getJSONObject(i);

                // For a given earthquake, extract the JSONObject associated with the
                // key called "properties", which represents a list of all properties
                // for that earthquake.
                JSONObject properties = currentEarthquake.getJSONObject("properties");

                // Extract the value for the key called "mag"
                String magnitude = properties.getString("mag");

                // Extract the value for the key called "place"
                String location = properties.getString("place");

                // Extract the value for the key called "time"
                long time = properties.getLong("time");

                // Extract the value for the key called "url"
                String url = properties.getString("url");

                // Create a new @link EarthquakeAdapter object with the magnitude, location, time,
                // and url from the JSON response.
                Word earthquake = new Word(magnitude, location, time, url);

                // Add the new @link EarthquakeAdapter to the list of earthquakes.
                earthquakes.add(earthquake);
            

         catch (JSONException e) 
            // If an error is thrown when executing any of the above statements in the "try" block,
            // catch the exception here, so the app doesn't crash. Print a log message
            // with the message from the exception.
            Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e);
        
        // Return the list of earthquakes
        return earthquakes;
    


activity_list_display.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:orientation="vertical"
    android:paddingEnd="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingStart="16dp"
    tools:context="com.example.a.books.Word">

    <TextView
        android:id="@+id/magnitude"
        android:layout_
        android:layout_
        android:textColor="#000000"
        android:layout_gravity="center_vertical"
        android:fontFamily="sans-serif-medium"
        android:gravity="center"
        android:layout_margin="5dp"
        android:textSize="16sp"
        tools:text="8.9" />

    <TextView
        android:id="@+id/location_offset"
        android:textColor="#000000"
        android:layout_
        android:layout_
        android:ellipsize="end"
        android:fontFamily="sans-serif-medium"
        android:maxLines="1"
        android:textAllCaps="true"
        android:textSize="12sp"
        tools:text="30km S of" />

    <TextView
        android:id="@+id/primary_location"
        android:layout_
        android:layout_
        android:ellipsize="end"
        android:textColor="#000000"
        android:maxLines="2"
        android:textSize="16sp"
        tools:text="Long placeholder location that should wrap to more than 2 lines of text" />

    <TextView
        android:id="@+id/date"
        android:textColor="#000000"
        android:layout_
        android:layout_
        android:layout_gravity="end"
        android:textSize="12sp"
        tools:text="Mar 6, 2010" />

    <TextView
        android:id="@+id/time"
        android:textColor="#000000"
        android:layout_
        android:layout_
        android:layout_gravity="end"
        android:textSize="12sp"
        tools:text="3:00 PM" />

</LinearLayout>

activity_word.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="horizontal">

    <ListView
        android:id="@+id/list"
        android:layout_
        android:layout_
        android:orientation="vertical" />


</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.a.books">
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".WordActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Word"></activity>
    </application>

</manifest>

【问题讨论】:

ArrayList&lt;Word&gt; words检查字数 你调试响应了吗? 你在哪里设置了适配器?你调用了多少次? 与此混淆 mAdapter = new WordAdapter(this, new ArrayList());最终 WordAdapter 适配器 = new WordAdapter(this, new ArrayList());通知你的适配器 @mertsimsek 是的,没有找到。 【参考方案1】:

使用

 @Override
public void onLoadFinished(Loader<List<Word>> loader, List<Word> earthquakes) 
    mAdapter.clear();

    if (earthquakes != null && !earthquakes.isEmpty()) 
          mAdapter = new WordAdapter(this, earthquakes)
    

    if (earthquakeListView.getAdapter() != null) 
        if (earthquakeListView.getAdapter().getCount() == 0) 
            earthquakeListView.setAdapter(mAdapter);
         else 
            mAdapter.notifyDataSetChanged();
        
     else

    
        earthquakeListView.setAdapter(mAdapter);
    

【讨论】:

没用。错误:无法解析地震列表视图 它是您的列表视图对象使其成为全局对象。 做到了。仍然屏幕是空白的。 Wordadapter(this,earthquakes) 错误:WordAdapter 中的 WordAdapter (Activity, java.util.ArrayList) 无法应用于 (WordActivity, java.util.列表) 修改你的适配器构造函数【参考方案2】:

截图在附件中,用这个替换你的代码,

import android.app.LoaderManager;
import android.content.Context;
import android.content.Loader;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class WordActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<Word>> 
    private static final int EARTHQUAKE_LOADER_ID = 1;
    private static final String LOG_TAG = WordActivity.class.getName();
    /**
     * URL for earthquake data from the USGS dataset
     */
    private static final String USGS_REQUEST_URL =
            "https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2014-01-01&endtime=2014-01-02";
    /**
     * Adapter for the list of earthquakes
     */
    public wordAdap mAdapter;
    private TextView mEmptyStateTextView;
    ListView earthquakeListView;
    ArrayList<Word> words;

    @Override
    protected void onCreate(Bundle bundle) 
        super.onCreate(bundle);
        setContentView(R.layout.activity_word);
        // Get a reference to the LoaderManager, in order to interact with loaders.
        earthquakeListView=(ListView)findViewById(R.id.list1) ;
        words=new ArrayList<>();
        mAdapter = new wordAdap(WordActivity.this, words);


        // Create a new adapter that takes an empty list of earthquakes as input


        // Set the adapter on the @link ListView
        // so the list can be populated in the user interface

        earthquakeListView.setAdapter(mAdapter);

        // Set an item click listener on the ListView, which sends an intent to a web browser
        // to open a website with more information about the selected earthquake.


        // Start the AsyncTask to fetch the earthquake data
        //mEmptyStateTextView = (TextView) findViewById(R.id.empty_view);
        //earthquakeListView.setEmptyView(mEmptyStateTextView);
        ConnectivityManager connMgr = (ConnectivityManager)
                getSystemService(Context.CONNECTIVITY_SERVICE);

        // Get details on the currently active default data network
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

        // If there is a network connection, fetch data
        if (networkInfo != null && networkInfo.isConnected()) 
            // Get a reference to the LoaderManager, in order to interact with loaders.
            LoaderManager loaderManager = getLoaderManager();

            // Initialize the loader. Pass in the int ID constant defined above and pass in null for
            // the bundle. Pass in this activity for the LoaderCallbacks parameter (which is valid
            // because this activity implements the LoaderCallbacks interface).
            loaderManager.initLoader(EARTHQUAKE_LOADER_ID, null, this);
         else 
            // Otherwise, display error
            // First, hide loading indicator so error message will be visible
            //View loadingIndicator = findViewById(R.id.loading_indicator);
            //loadingIndicator.setVisibility(View.GONE);

            //Update empty state with no connection error message
            //mEmptyStateTextView.setText("No Connection");
        

    

    public Loader<List<Word>> onCreateLoader(int i, Bundle bundle) 
        return new WordLoader(this, USGS_REQUEST_URL);
    

    /**
     * @link  to perform the network request on a background thread, and then
     * update the UI with the list of earthquakes in the response.
     * <p>
     * AsyncTask has three generic parameters: the input type, a type used for progress updates, and
     * an output type. Our task will take a String URL, and return an EarthquakeAdapter. We won't do
     * progress updates, so the second generic is just Void.
     * <p>
     * We'll only override two of the methods of AsyncTask: doInBackground() and onPostExecute().
     * The doInBackground() method runs on a background thread, so it can run long-running code
     * (like network activity), without interfering with the responsiveness of the app.
     * Then onPostExecute() is passed the result of doInBackground() method, but runs on the
     * UI thread, so it can use the produced data to update the UI.
     */

    @Override
    public void onLoadFinished(Loader<List<Word>> loader, List<Word> earthquakes) 
        // Clear the adapter of previous earthquake data
        //View loadingIndicator = findViewById(R.id.loading_indicator);
        //loadingIndicator.setVisibility(View.GONE);
        //mEmptyStateTextView.setText(R.string.no_earthquakes);
//        mAdapter.clear();
        // If there is a valid list of @link Earthquakes, then add them to the adapter's
        // data set. This will trigger the ListView to update.

        if (earthquakes != null && !earthquakes.isEmpty()) 
            words.addAll(earthquakes);
        
        mAdapter.notifyDataSetChanged();

      /*  words.addAll(earthquakes);
        mAdapter.notifyDataSetChanged();*/
    

    @Override
    public void onLoaderReset(Loader<List<Word>> loader) 
        // Loader reset, so we can clear out our existing data.
//        mAdapter.clear();
    



和你的适配器一样,

    import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by sunisha on 10/5/17.
 */

public class wordAdap extends BaseAdapter 
    ArrayList<Word> Word=new ArrayList<>();
    Activity mContext;

    public wordAdap(Activity context, ArrayList<Word> Word) 
        mContext = context;
        this.Word = Word;
    

    @Override
    public int getCount() 
        return Word.size();
    

    @Override
    public Object getItem(int pos) 
        return Word.get(pos);
    

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
        View v = convertView;
        final CampaignItemViewHolders holder;
        if (convertView == null) 
            LayoutInflater li = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = li.inflate(R.layout.activity_list_display, null);
//            v = li.inflate(R.layout.aa, null);
            holder = new CampaignItemViewHolders(v);
            v.setTag(holder);
         else 
            holder = (CampaignItemViewHolders) v.getTag();
        



        String magnitude=Word.get(position).getMagnitude();
        holder.magnitudeView.setText(magnitude);


        return v;
    



class CampaignItemViewHolders 
    TextView magnitudeView;

    public CampaignItemViewHolders(View base) 

        magnitudeView = (TextView) base.findViewById(R.id.magnitude);



    



【讨论】:

我刚刚设置了幅度,只对其他细节做同样的操作...... 嘿,谢谢你真棒。首先有一些疑问,它只是显示量级。我是Android开发的初学者,我真的不知道您使用哪种方法来获取输出。 2)如何获取其他Json数据? 3)我的代码有什么问题?投票。请解释并解决其余问题。非常感谢。 哦,好吧....我已将您添加到聊天中,您可以检查一下吗?chat.***.com/rooms/143841/…【参考方案3】:

也许您缺少notifyDataSetChanged()

@Override
public void onLoadFinished(Loader<List<Word>> loader, List<Word> earthquakes) 
    mAdapter.clear();

    if (earthquakes != null && !earthquakes.isEmpty()) 
        mAdapter.addAll(earthquakes);
    

    mAdapter.notifyDataSetChanged(); //<---- You're missing this

ArrayAdapter 仅在使用 add()、insert()、remove() 和 clear() 时自动更新

【讨论】:

添加它仍然是一个空白屏幕。

以上是关于JSON 加载/获取正确但无法显示的主要内容,如果未能解决你的问题,请参考以下文章

无法从 JSON 数据显示 Google Gauge

无法加载 JSON 数据以在 ListView 中显示

无法加载 JSON 数据

我正在从 json Web 服务获取数据,但无法在我的 ui 上显示它

数据表中的错误:无法显示正确的行数

无法通过 json 数据加载 Extjs Grid