1

I am new to android programming. faced a problem that could not solve it by a lot searching. I am trying to get Json data from my wordpress site and put it to a simple android app.

It works when i get one parameter like post titles and display it by a listView. but when i am trying to get Titles, Images & excerpt of 10 recent posts and display them in a listView, seems it is a little complicated.

How shall i get title & photo of 10 recent posts and display them on android ? the following code is what i could write so far:

mainActivity.java

package com.example.test1;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import android.app.ProgressDialog;
import android.content.Intent;
//import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import java.util.List;
import java.util.Map;

public class MainActivity extends ListActivity {

    String url = "http://www.example.com/wp-json/wp/v2/posts";
    List<Object> list;
    Gson gson;
    ProgressDialog progressDialog;
    // ListView postList;
    Map<String, Object> mapPost;
    Map<String, Object> mapTitle;
    int postID;
    String postTitle[];
    String image[];


    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView ( R.layout.activity_main );

        progressDialog = new ProgressDialog ( MainActivity.this );
        progressDialog.setMessage ( "Loading..." );
        progressDialog.setProgressStyle ( ProgressDialog.STYLE_SPINNER );
        progressDialog.show ();

        StringRequest request = new StringRequest ( Request.Method.GET, url, new Response.Listener<String> () {
            @Override
            public void onResponse(String s) {
                gson = new Gson ();
                list = (List) gson.fromJson ( s, List.class );
                postTitle = new String[list.size ()];
                image = new String[list.size ()];

                for (int i = 0; i < list.size (); ++i) {
                    mapPost = (Map<String, Object>) list.get ( i );
                    mapTitle = (Map<String, Object>) mapPost.get ( "title" );
                    postTitle[i] = (String) mapTitle.get ( "rendered" );
                    image[i] = (String) mapPost.get ( "poster_big" );
                }

                // the following line display error
                // error : in picture
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_main, R.id.label, image);
                setListAdapter(adapter);


            }

         }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Toast.makeText(MainActivity.this, "Some error occurred", Toast.LENGTH_LONG).show();
                }
        });



    }
}

main_activity.xml

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

    <ImageView
        android:id="@+id/icon"
        android:layout_width="136dp"
        android:layout_height="139dp"
        android:layout_marginLeft="4px"
        android:layout_marginTop="4px"
        android:layout_marginRight="10px"
        android:src="@drawable/ic_launcher"></ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:textSize="20px"></TextView>

</LinearLayout>

and here is error from Android studio: error android studio

thanks.

1
  • I'd rather advice you the use of RecyclerView. There you can create a layout you can apply to a viewholder and then apply the elements. It seem to be a bit complicated to learn, but if you got it it's super easy. If you need help write me. Commented Oct 14, 2019 at 7:34

1 Answer 1

1

Use MainActivity.this instead of this. like

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.activity_main, R.id.label, image);

ArrayAdapter<>() constructor needs Context in the first parameter, but you have passed this which refers to the Object of StringRequest.

Note: this always refers to inner class.

Update:

You should close your progressDialog after the API response. like below

public class MainActivity extends ListActivity {

    String url = "http://www.example.com/wp-json/wp/v2/posts";
    List<Object> list;
    Gson gson;
    ProgressDialog progressDialog;
    // ListView postList;
    Map<String, Object> mapPost;
    Map<String, Object> mapTitle;
    int postID;
    String postTitle[];
    String image[];


    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView ( R.layout.activity_main );

        progressDialog = new ProgressDialog ( MainActivity.this );
        progressDialog.setMessage ( "Loading..." );
        progressDialog.setProgressStyle ( ProgressDialog.STYLE_SPINNER );
        progressDialog.show ();

        StringRequest request = new StringRequest ( Request.Method.GET, url, new Response.Listener<String> () {
            @Override
            public void onResponse(String s) {
                gson = new Gson ();
                list = (List) gson.fromJson ( s, List.class );
                postTitle = new String[list.size ()];
                image = new String[list.size ()];

                // here you should close your progress dialog.
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }

                for (int i = 0; i < list.size (); ++i) {
                    mapPost = (Map<String, Object>) list.get ( i );
                    mapTitle = (Map<String, Object>) mapPost.get ( "title" );
                    postTitle[i] = (String) mapTitle.get ( "rendered" );
                    image[i] = (String) mapPost.get ( "poster_big" );
                }

                // the following line display error
                // error : in picture
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_main, R.id.label, image);
                setListAdapter(adapter);
            }

         }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Toast.makeText(MainActivity.this, "Some error occurred", Toast.LENGTH_LONG).show();

                   // here you should close your progress dialog.
                   if (progressDialog.isShowing()) {
                      progressDialog.dismiss();
                   }
                }
        });



    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Jakir . thanks for replay . that worked but now app screen freeze at loading ... internet access is added and no error from debugger
Well nothing changed i've added it in 2 places . after setListAdapter() and where i added it . i feel it's a loop that can't end or something like that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.