0

Although this is just a rough sketch..i just want to see if it will work..Anytime i run the program it doesn't crash and it doesn't show anything. The error showing on the logcat is JSON result form the php page..i don't know why the Postexecute() method is not working...

this is the error

09-14 18:21:07.079: E/JSON(21310): {"tag":"getTopic","success":1,"error":0,"data":[{"title":"Facebook finally rolls GRAPH Search","tid":"81","time":"2013-06-22 10:05:18"},{"title":"What is Ubuntu for Android?","tid":"69","time":"2013-06-22 10:18:00"},{"title":"Android and Windows on same device(samsung)","tid":"98","time":"2013-06-22 10:18:35"},{"title":"\"Tweet\" has been verified as a real word in English","tid":"67","time":"2013-06-22 10:27:33"},{"title":"Instagram now has video recording","tid":"97","time":"2013-06-22 10:27:45"},{"title":"An Open Letter to DBanj","tid":"70","time":"2013-06-22 10:31:41"},{"title":"MTN call rate wahala","tid":"84","time":"2013-06-22 10:32:44"},{"title":"Types of friends essential for women","tid":"33","time":"2013-06-22 10:45:41"},{"title":"Not to be loved in return","tid":"106","time":"2013-06-22 18:15:06"},{"title":"I am in love with you","tid":"107","time":"2013-06-22 18:28:56"},{"title":"I hate it!!!","tid":"115","time":"2013-06-23 19:06:26"},{"title":"Spoils of love","tid":"116","time":"2013-06-23 19:14:43"},{"title":"An Apology Letter from Men To Women","tid":"117","time":"2013-06-26 12:43:02"},{"title":"Ramadan Starts tomorrow (in sha Allah) !!!","tid":"120","time":"2013-07-09 15:06:09"},{"title":"Preventing infidelity on your marriage","tid":"29","time":"2013-07-14 12:06:31"},{"title":"How to have a healthy relationship","tid":"121","time":"2013-08-02 17:17:32"},{"title":"ASUU strike not ending anytime soon","tid":"124","time":"2013-08-27 12:56:26"},{"title":"Google Announces Android 4.4 KitKat","tid":"126","time":"2013-09-06 20:51:32"},{"title":"Apple Launches iPhone 5s and iPhone 5c","tid":"127","time":"2013-09-11 15:29:40"}]}

and this is the class

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.nairation.library.JSONParser;
import com.nairation.library.UserFunctions;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class Topic_page extends Activity implements OnItemClickListener{

    public static final String TOPIC_DURATION = "relative";
    public static final String TOPIC_TITLE = "title";
    public static final String TOPIC_PREVIEW = "Testing out";
    public static final String TOPIC_ID = null;
    public static final Object TOPIC_THUMB_URL = null;

    ListView list;
    TopicListAdapter adapter;
    UserFunctions userf;
    JSONParser jpa;
    ArrayList<HashMap<String, String>> topicList = new ArrayList<HashMap<String,String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.topic_page);

        list = (ListView) findViewById(R.id.topic_list);
        adapter = new TopicListAdapter(this, topicList);
        list.setAdapter(adapter);
        list.setOnItemClickListener(this);

        try{
            new ServiceSync().execute("mash");
        }catch(Exception e){
            String message = e.getMessage();
        }

    }

    private class ServiceSync extends AsyncTask<String, JSONObject, JSONObject>{

        @Override
        protected JSONObject doInBackground(String... params) {
            // TODO Auto-generated method stub
            String uname = params[0];
            userf = new UserFunctions();
            JSONObject jp = userf.getTopic(uname);
            return jp;
        }

        @Override
        protected void onPostExecute(JSONObject result) {
            // TODO Auto-generated method stub
            //super.onPostExecute(result);
            try {
                JSONArray obj = result.getJSONArray("data");

                for (int i = 0; i < result.length(); i++) {
                    JSONObject data = obj.getJSONObject(i);
                    HashMap<String, String> map = new HashMap<String, String>();

                    String title = data.getString(TOPIC_TITLE);
                    String time = data.getString("time");
                    String tid = data.getString("tid");

                    map.put(TOPIC_TITLE, title);
                    map.put("Date", time);

                    topicList.add(map);
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Problem with loading the feeds", Toast.LENGTH_LONG).show();
            }
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub

    }
}

So what could be the problem the JSON result is not being processed..

2 Answers 2

2

If by not working you mean you cannot see the parsed data in the list , then for that you need to call adapter.notifyDataSetChanged() method whenever the underlying list is modified after setting the list to the adapter

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

2 Comments

Just tried putting it in the ServiceSync class and for some some reason the class can't see the adapter variable but it sees other variables form outside the class.
after your for loop when you have finished adding everything to topicList call adapter.notifyDataSetChanged() and onDestroy() of activity cancel the asyncTask then onPostExecute() is not called
1

Your error toast should be showing, i think this is your error:

for (int i = 0; i < result.length(); i++)

should be

for (int i = 0; i < obj.length(); i++)

... use more descriptive variable names like

JSONArray feedArray = result.getJSONArray("data");

5 Comments

oh yeah...my bad..just fixed that and it stil displays the same error... i am thinking it's because of adapter.notifyDataSetChanged() in which i tried putting in the servicessync class..but it seems the class doesn't see the variable adapter but it sees all other external variables
it's true you should call adapter.notifyDataSetChanged(), however not calling it wouldn't result in an error. Try clean/rebuild your project, adapter should definately show up.
on another side note, your app will crash when onPostExecute and adapter.notifyDataSetChanged gets called after you left/closed the activity
how am i to fix this please..just give a me run through
see Ravis comment in the answer, that should fix it. (I'd put it in onStop() though this depends on the way you're planning navigation within your app)

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.