2

Here in my previous question How to Print Message when Json Response has no fileds? Toast is working fine but if my response is showing no fileds with array and what if I want to use textview instead of Toast? can anyone help me?enter image description here

public class MessageSent extends ListActivity{

    private ProgressDialog pDialog;
    JSONArray msg=null;
    private TextView nomsg;

    private ListView listview;

    private ArrayList<HashMap<String,String>> aList;
    private static String MESSAGE_URL = "";
    private static final String MESSAGE_ALL="msg";
    private static final String MESSAGEUSER_ID="msg_user_id";
    private static final String MESSAGE_NAME="name";
    private static final String MESSAGE_PROFILE="profile_id";
    private static final String MESSAGE_IMAGE="image";
    private static final String MESSAGE_CAST="cast";
    private static final String MESSAGE_AGE="age";
    private static final String MESSAGE_LOCATION="location";
    private CustomAdapterMessage adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view_msgsent);
        nomsg=(TextView)findViewById(R.id.no_message);
        String strtexts = getIntent().getStringExtra("id");
        System.out.println("<<<<<<<< id : " + strtexts);
        MESSAGE_URL = "xxxxx"+strtexts;

        // listview=(ListView)findViewById(R.id.list);

        //ListView listview = this.getListView();

        ListView listview = (ListView)findViewById(android.R.id.list);

        new LoadAlbums().execute();


            }
        });
    }

    class LoadAlbums extends AsyncTask>> {

        /**
         * Before starting background thread Show Progress Dialog
         * */

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MessageSent.this);
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected ArrayList<HashMap<String,String>> doInBackground(String... args) {
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String, String>>();
            String jsonStr = sh.makeServiceCall(MESSAGE_URL, ServiceHandler.GET);

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) 
            {
                try 
                {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    msg = jsonObj.getJSONArray(MESSAGE_ALL);

                    // looping through All Contacts
                    for (int i = 0; i < msg.length(); i++) 
                    {
                        JSONObject c = msg.getJSONObject(i);

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(MESSAGEUSER_ID ,c.getString(MESSAGEUSER_ID));
                        map.put(MESSAGE_NAME,c.getString(MESSAGE_NAME));
                        map.put(MESSAGE_PROFILE, c.getString(MESSAGE_PROFILE));
                        map.put(MESSAGE_IMAGE, c.getString(MESSAGE_IMAGE));
                        map.put(MESSAGE_CAST, c.getString(MESSAGE_CAST));
                        map.put(MESSAGE_AGE, c.getString(MESSAGE_AGE)+" years");
                        map.put(MESSAGE_LOCATION, c.getString(MESSAGE_LOCATION));

                        // adding HashList to ArrayList
                        data.add(map);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return data;
        }

        protected void onPostExecute(ArrayList<HashMap<String,String>> result) {
            super.onPostExecute(result);

            // dismiss the dialog after getting all albums
            if (pDialog.isShowing())
                pDialog.dismiss();

            if(msg == null || msg.length() == 0) { 
                //Toast.makeText(getApplicationContext(), "No response", Toast.LENGTH_LONG).show
                nomsg.setText("No Message Found");
                //nomsg.setBackgroundDrawable(R.drawable.borders);
            }

            if(aList == null) {
                aList = new ArrayList<HashMap<String, String>>();
                aList.addAll(result);
                adapter = new CustomAdapterMessage(getBaseContext(), result);
                setListAdapter(adapter);
            } else {
                aList.addAll(result);
                adapter.notifyDataSetChanged();
            }
        }

    }
}
Community
  • 1
  • 1

2 Answers2

0

Try this way :

protected void onPostExecute(Void result) {
    super.onPostExecute(result);

    if (pDialog.isShowing())
        pDialog.dismiss();

    if(contacts == null || contacts.length() <= 0){
        yourTextView.setText("No Data");   
    }
 }
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

As it seems you have confusions in setting up the app. Let me explain few things and also provide you some sample code.

The use of an AsynTask?

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

But there are some operations that you would have to do in the UI while working in the background thread. In that case you make use of the,

1. onPreExecute(), invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.

2. onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

In your specific case as you want to set the values after the operation you have to make use of the latter mathod of the AsynTask.

The same code referring to the example you follow,

//MainActivity.java
public class MainActivity extends ListActivity {

    TextView yourTextView;

     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        .
        .
        //change the ID in this line from what you are using
        yourTextView = (TextView) findViewByID(R.id.id_in_activity_main);
        .
        .

        // Calling async task to get json
        new GetContacts().execute();
    }

    /**
     * Async task class to get json by making HTTP call
     * */
    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            .
            .
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            .
            .
        }

        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            if (pDialog.isShowing())
                pDialog.dismiss();

            if(contacts == null || contacts.length() <= 0){
                yourTextView.setText("No Data");   
            }
         }
     }
 }
codePG
  • 1,754
  • 1
  • 12
  • 19
  • i appriciate this answer and i am doing same as you say but my app crash –  Nov 22 '14 at 11:51
  • could you please update the question with your code and logcat. That would help identifying the problem easily – codePG Nov 22 '14 at 12:00
  • If all the above were correct, then select you project `Project -> Clean` and then try running your project. Sometimes there could be problems with linking. – codePG Nov 22 '14 at 12:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65417/discussion-between-codepg-and-aditya-vyas). – codePG Nov 22 '14 at 12:44