从 json 填充列表视图

Populating listview from json

我知道这个问题被问了 100 次,但出于某种原因,我找不到我的代码中有什么问题。 我对 android 和 java 很陌生。 基本上我有一个 wordpress 网站,我想从中获取文章。我已经安装了 wp-rest-api v2 以获取 json 数组格式的数据。 在我的 android 应用程序中,我有

MainActivity.java

package al.im.imp;
/*ALL THE IMPORTS*/

public class ImpHome extends AppCompatActivity implements View.OnClickListener {

ListView lstTest;
JSONAdapter mJSONAdapter;
Button search_Button;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imp_home);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        //Access Listview
        lstTest = (ListView) findViewById(R.id.home_list);




        search_Button = (Button) findViewById(R.id.button1);
        search_Button.setOnClickListener(this);


        mJSONAdapter = new JSONAdapter(this, getLayoutInflater());
        lstTest.setAdapter(mJSONAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_impakt_home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    private void queryImp() {

        // Create a client to perform networking
        AsyncHttpClient client = new AsyncHttpClient();

        client.get("http://example.com/wp-json/wp/v2/posts",


                new JsonHttpResponseHandler() {

                    @Override
                    public void onSuccess(JSONArray jsonArray) {
                        Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_LONG).show();

                        mJSONAdapter.updateData(jsonArray);
                    }

                    @Override
                    public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
                        // Display a "Toast" message
                        // to announce the failure
                        Toast.makeText(getApplicationContext(), "Error: " + statusCode + " " + throwable.getMessage(), Toast.LENGTH_LONG).show();

                        // Log error message
                        // to help solve any problems
                        Log.e("Imp android", statusCode + " " + throwable.getMessage());
                    }
                });

    }


    @Override
    public void onClick(View v) {
        queryImp();
    }

    @Override
    public void onStart() {
        super.onStart();


    }

    @Override
    public void onStop() {
        super.onStop();

    }
}

我的article_list.xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="75dp">

    <TextView
        android:id="@+id/text_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"/>


</RelativeLayout>

和我的JSONAdapter.javaclass

package al.imp.imp;

/*IMPORTS*/

public class JSONAdapter extends BaseAdapter{

    Context mContext;
    LayoutInflater mInflater;
    JSONArray mJsonArray;

    public JSONAdapter(Context context, LayoutInflater inflater) {
        mContext = context;
        mInflater = inflater;
        mJsonArray = new JSONArray();
    }

    @Override
    public int getCount() {
        return mJsonArray.length();
    }

    @Override
    public Object getItem(int position) {
        return mJsonArray.optJSONObject(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {

            convertView = mInflater.inflate(R.layout.article_list, null);

            holder = new ViewHolder();
            holder.titleTextView = (TextView) convertView.findViewById(R.id.text_title);

            convertView.setTag(holder);
        } else {

            holder = (ViewHolder) convertView.getTag();
        }

        JSONObject jsonObject = (JSONObject) getItem(position);

        String articleTitle = "";

            articleTitle = jsonObject.optJSONObject("title").optString("rendered");

        holder.titleTextView.setText(articleTitle);

        return convertView;
    }

    private static class ViewHolder {

        public TextView titleTextView;

    }


    public void updateData(JSONArray jsonArray) {
        // update the adapter's dataset
        mJsonArray = jsonArray;
        notifyDataSetChanged();
    }

}

我不知道为什么这不起作用。当我尝试记录

之类的数据时
 for (int i = 1; i < jsonArray.length(); i++) {
       Log.d("Title is:", jsonArray.optJSONObject(i).getJSONObject("title").getString("rendered").toString());
       Log.d("Image Url is:", jsonArray.optJSONObject(i).getString("featured_image_thumbnail_url").toString());
} 

它工作得很好,所以我想这不是获取 json 响应的问题,而是填充列表视图的问题。

请帮帮我

更新数据后,尝试像 lstTest.invalidateViews(); 这样刷新列表视图 - 这样您的 queryImp() 代码将变为:

private void queryImp() {
// Create a client to perform networking
   AsyncHttpClient client = new AsyncHttpClient();
   client.get("http://example.com/wp-json/wp/v2/posts",
       new JsonHttpResponseHandler() {
                    @Override
                    public void onSuccess(JSONArray jsonArray) {
                        Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_LONG).show();

                        mJSONAdapter.updateData(jsonArray);
                        mJSONAdapter.notifyDataSetChanged();
                        //try refreshing the list-view like this:
                        lstTest.invalidateViews()
                    }

                    @Override
                    public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
                        // Display a "Toast" message
                        // to announce the failure
                        Toast.makeText(getApplicationContext(), "Error: " + statusCode + " " + throwable.getMessage(), Toast.LENGTH_LONG).show();

                        // Log error message
                        // to help solve any problems
                        Log.e("Imp android", statusCode + " " + throwable.getMessage());
                    }
                });

    }

如果更改有帮助,请告诉我。