从json填充listview

时间:2016-05-03 21:23:18

标签: android json listview wordpress-json-api

我知道这个问题被问了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.java类

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响应的问题,而是填充listview。

请帮帮我

1 个答案:

答案 0 :(得分:0)

更新数据后,请尝试刷新列表视图,例如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());
                    }
                });

    }

让我知道这些变化是否有帮助。