2

Hello I'm new to android programming and trying to learn it. I followed this tutorial and it's working fine however I don't know how to display the records in the listview.

The PostActivity.java file is from the tutorial is below,

package com.kylewbanks.gsonvolleytutorial;

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.ArrayAdapter;

    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 com.google.gson.GsonBuilder;

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

    import static android.R.id.list;

    public class PostActivity extends Activity {

        private static final String ENDPOINT = "https://kylewbanks.com/rest/posts.json";

        private RequestQueue requestQueue;
        private Gson gson;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_post);

            requestQueue = Volley.newRequestQueue(getApplicationContext());
            GsonBuilder gsonBuilder = new GsonBuilder();
            gsonBuilder.setDateFormat("M/d/yy hh:mm a");
            gson = gsonBuilder.create();

            fetchPosts();
        }

        private void fetchPosts() {
            StringRequest request = new StringRequest(Request.Method.GET, ENDPOINT, onPostsLoaded, onPostsError);

            requestQueue.add(request);
        }

        private final Response.Listener<String> onPostsLoaded = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //Log.i(PostActivity.class.getSimpleName(), response);

                List<Post> posts = Arrays.asList(gson.fromJson(response, Post[].class));
                //Log.i(PostActivity.class.getSimpleName(), posts.size() + " posts loaded.");

                List listview = (List) findViewById(R.id.postListView);

                for (Post post : posts) {
                    //Log.i(PostActivity.class.getSimpleName(), post.ID + ": " + post.title);
                }
                ArrayAdapter adapter = new ArrayAdapter(this,R.layout.single_post_item, posts);
                listview.setAdapter(adapter);

            }
        };

        private final Response.ErrorListener onPostsError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(PostActivity.class.getSimpleName(), error.toString());
            }
        };
    }

Post.java

import java.util.Date;
import com.google.gson.annotations.SerializedName;

public class Post {

    @SerializedName("id")
    long ID;

    @SerializedName("date")
    Date dateCreated;

    String title;
    String author;
    String url;
    String body;

}

activity_post.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".PostActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/postListView"
        android:layout_alignParentBottom="true" />
</RelativeLayout>

single_post_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />
</LinearLayout>

Update Error 1 :

:app:processDebugManifest
:app:processDebugResources
AGPBI: {"kind":"error","text":"Error parsing XML: XML or text declaration not at start of entity","sources":[{"file":"C:\\Users\\xxx\\Downloads\\GSONVolleyTutorial-master\\GSONVolleyTutorial-master\\app\\src\\main\\res\\layout\\activity_post.xml","position":{"startLine":0}}],"original":"","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Error parsing XML: XML or text declaration not at start of entity","sources":[{"file":"C:\\Users\\xxx\\Downloads\\GSONVolleyTutorial-master\\GSONVolleyTutorial-master\\app\\src\\main\\res\\layout\\single_post_item.xml","position":{"startLine":0}}],"original":"","tool":"AAPT"}
C:\Users\xxx\Downloads\GSONVolleyTutorial-master\GSONVolleyTutorial-master\app\build\intermediates\res\merged\debug\layout\activity_post.xml:1: error: Error parsing XML: XML or text declaration not at start of entity

C:\Users\xxx\Downloads\GSONVolleyTutorial-master\GSONVolleyTutorial-master\app\build\intermediates\res\merged\debug\layout\single_post_item.xml:1: error: Error parsing XML: XML or text declaration not at start of entity

 FAILED

FAILURE: Build failed with an exception.

Update Error 2 :

C:\Users\xxx\Downloads\GSONVolleyTutorial-master\GSONVolleyTutorial-master\app\src\main\java\com\kylewbanks\gsonvolleytutorial\PostActivity.java:70: error: no suitable constructor found for ArrayAdapter(<anonymous Listener<String>>,int,int,List<String>)
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.single_post_item,R.id.textView, posts_strs);
                                               ^
    constructor ArrayAdapter.ArrayAdapter(Context,int,int,String[]) is not applicable
      (argument mismatch; <anonymous Listener<String>> cannot be converted to Context)
    constructor ArrayAdapter.ArrayAdapter(Context,int,int,List<String>) is not applicable
      (argument mismatch; <anonymous Listener<String>> cannot be converted to Context)
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

:app:compileDebugJavaWithJavac FAILED

FAILURE: Build failed with an exception.
Community
  • 1
  • 1
zeusukdm
  • 155
  • 3
  • 11

2 Answers2

3

Since there is only one TextView in your list layout mean you intended to display single item

1.) Create a String arraylist and add elements to it

2.) Mention @+id/textView" as 3rd parameter while creating adapter

ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

public class PostActivity extends Activity {

    private static final String ENDPOINT = "https://kylewbanks.com/rest/posts.json";

    private RequestQueue requestQueue;
    private Gson gson;
    private List<String> posts_strs ;

    // keep it here for later use in other methods
    private List<Post> posts ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // code

        // initialize list
        posts_strs = new ArrayList<String>();

        // code
    }


    private final Response.Listener<String> onPostsLoaded = new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            //Log.i(PostActivity.class.getSimpleName(), response);

            posts = Arrays.asList(gson.fromJson(response, Post[].class));
            //Log.i(PostActivity.class.getSimpleName(), posts.size() + " posts loaded.");

            List listview = (List) findViewById(R.id.postListView);

            for (Post post : posts) {
                 // add items to list
                 posts_strs.add( post.ID + ": " + post.title);
                //Log.i(PostActivity.class.getSimpleName(), post.ID + ": " + post.title);
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(PostActivity.this,R.layout.single_post_item
                     ,R.id.textView, posts_strs);
            //   use the textview id posts_strs while creating adapter
            listview.setAdapter(adapter);

        }
    };
        // code

}

Issues :

1.) Add <?xml version="1.0" encoding="utf-8"?> at the top in your both XML layouts activity_post and single_post_item

2.) Use PostActivity.this instead of this because this will refer to anonymous Response.Listener<String> class instead of PostActivity.

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • try the updated code and mention the error details if there will be any – Pavneet_Singh Feb 12 '17 at 09:35
  • you have to use `posts_strs` in adapter i updated this in my code way back in time , simply try the current code , surely work – Pavneet_Singh Feb 12 '17 at 09:51
  • I tried the updated code and here is the errors: `C:\Users\xxx\Downloads\GSONVolleyTutorial-master\GSONVolleyTutorial-master\app\src\main\res\layout\activity_post.xml` and `C:\Users\xxx\Downloads\GSONVolleyTutorial-master\GSONVolleyTutorial-master\app\src\main\res\layout\single_post_item.xml` @Pavneet Singh – zeusukdm Feb 12 '17 at 09:53
  • post complete errors in your post at the end , these lines are just paths there is useful error details – Pavneet_Singh Feb 12 '17 at 09:55
  • I have posted the complete errors also I have updated the PostActivity.java file with your updated code which I put into my post as well @Pavneet Singh – zeusukdm Feb 12 '17 at 10:03
  • you need to add this line `` at the top in your both `XML` layouts `activity_post` and `single_post_item` – Pavneet_Singh Feb 12 '17 at 10:07
  • I added the line `` to the files now I have another error, which is ArrayAdapter error, I have updated the error in the post to see the details. @Pavneet Singh – zeusukdm Feb 12 '17 at 10:26
  • use `PostActivity.this` instead of `this` OMG , you said your current code was working fine – Pavneet_Singh Feb 12 '17 at 10:29
  • Thank you very much for all your help, really really appreciated. @Pavneet Singh – zeusukdm Feb 12 '17 at 10:32
  • Can I use POST method and send some parameters to the SERVER so I can get the needed json array instead of the whole information? @Pavneet Singh – zeusukdm Feb 12 '17 at 10:40
  • yes you can but that depends if the link (https://kylewbanks.com/rest/posts.json) supports the Post request with your requirements although it's just a json file given to you for demo and is there is still any issue with current code ? – Pavneet_Singh Feb 12 '17 at 10:44
  • 1
    Yes, thank you for the current code, it is working perfectly, all I need to do some how to use the POST method and send operation and id parameters to the server which accepts post requests. For example, I need to send operation=show,id=1 to get the list with the id 1 @Pavneet Singh – zeusukdm Feb 12 '17 at 10:47
  • since you can't control the server link and this seems like just a demo file so not possible – Pavneet_Singh Feb 12 '17 at 10:49
  • Your code is perfect for me and I am not going to use the demo link, I have a different php rest api which I will use for the post arrays. I find the post method for gson frrm this thread [link](http://stackoverflow.com/questions/36432152/android-volley-gson-post) How can I implement this into your code? @ Pavneet Singh – zeusukdm Feb 12 '17 at 10:54
  • now that i totally a different question and your posted issue has been resolved , read [this link](http://stackoverflow.com/questions/37481228/post-method-using-volley-not-working) and you can post another question after if you need – Pavneet_Singh Feb 12 '17 at 10:57
  • Sorry to bother you but you again but I am having an error `Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path` I have updated the java file in the post and the full error description in error update 3 @Pavneet Singh – zeusukdm Feb 12 '17 at 14:24
  • You should not change your original question which has been solved , if you have new issue then you can post new question instead of turning old one into completely a new question , that's how Stackoverflow works so you must rollback to previous edit and post new question Although the current issue is about your `GSON` code so you can do some research about this error `Expected BEGIN_ARRAY but was BEGIN_OBJECT` something like http://stackoverflow.com/questions/9598707/gson-throwing-expected-begin-object-but-was-begin-array – Pavneet_Singh Feb 12 '17 at 15:15
1
C:\Users\xxx\Downloads\GSONVolleyTutorial-master\GSONVolleyTutorial-master\app\src\main\java\com\kylewbanks\gsonvolleytutorial\PostActivity.java:70: error: no suitable constructor found for ArrayAdapter(<anonymous Listener<String>>,int,int,List<String>)
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.single_post_item,R.id.textView, posts_strs);
                                               ^
    constructor ArrayAdapter.ArrayAdapter(Context,int,int,String[]) is not applicable
      (argument mismatch; <anonymous Listener<String>> cannot be converted to Context)
    constructor ArrayAdapter.ArrayAdapter(Context,int,int,List<String>) is not applicable
      (argument mismatch; <anonymous Listener<String>> cannot be converted to Context)

Since the code which creates the ArrayAdapter is inside an anonymous inner class, this refers to the instance of that anonymous class. To access the this pointer of the PostActivity instance which contains the anonymous class, use PostActivity.this instead.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268