从JSON填充ListView [英] Populating listview from json

查看:115
本文介绍了从JSON填充ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know that this question is asked 100 times, but for some reason i cannot find what is wrong in my code. I am very new to android and java. Basically i have a wordpress website from which I want to get the articles. I have installed the wp-rest-api v2 to get the data in json array format. In my android app I have

MainActivity.java

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布局

My article_list.xml layout

<?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类

and my JSONAdapter.java class

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

}

我不知道为什么这行不通.当我尝试记录数据

I dont know why this does not work. When i try to log the data like

 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响应方面不是问题,而是填充列表视图.

it works perfectly, so I guess it is not a problem in getting the json response, but populating the listview.

请帮助我

推荐答案

更新数据后,尝试执行诸如刷新列表视图之类的操作,例如lstTest.invalidateViews();-这样您的queryImp()代码将更改为:

After updating the data, try something like refreshing the list view like lstTest.invalidateViews(); - so your queryImp() code will change into:

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

    }

让我知道更改是否有帮助.

Let me know if the changes help.

这篇关于从JSON填充ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆