Alvinrightback
Alvinrightback

Reputation: 215

Android SetText not working on TextVIew but the value is there

Guys I am trying to set the Textview but it's not changing, the value from the database is there. i hope someone can help

here's the java code

public class Announcement_Details extends AppCompatActivity{

    private static final String GET_URL = "http://XXX";
    private ProgressDialog pDialog;
    TextView id,title,content,date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lv_view);
        id = (TextView)findViewById(R.id.tv_id);
        title = (TextView)findViewById(R.id.tv_title);
        content = (TextView)findViewById(R.id.tv_content);
        date = (TextView)findViewById(R.id.tv_date);

        getAnnouncementDetails();


    }

    @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_main, 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.refresh) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void getAnnouncementDetails() {
        StringRequest postRequest = new StringRequest(Request.Method.POST, GET_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            JSONArray data = jsonResponse.getJSONArray("announcement_data");
                            Log.d("Announcement Data", ""+data);
                            id.setText(data.getString(0));
                            title.setText(data.getString(1));
                            content.setText(data.getString(2));
                            date.setText(data.getString(3));

                        }catch (Exception e) {
                            e.printStackTrace();
                        }
                        pDialog.dismiss();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }

        ) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                // the POST parameters:
                params.put("id", getIntent().getStringExtra("id"));
                return params;
            }
        };

        pDialog = new ProgressDialog(Announcement_Details.this);
        pDialog.setMessage("Getting Announcements Details.");
        pDialog.show();

        Volley.newRequestQueue(getApplication()).add(postRequest);


    }
}

here's the value from logcat

02-05 01:28:35.861    4004-4004/com.example.wackyroad.internannouncement D/Announcement Data﹕ [{"announcement_title":"Sample Title Here","announcement_content":"Sample Content","announcement_date":"2016-02-04"}]

Upvotes: 1

Views: 1302

Answers (5)

Lovepreet Singh
Lovepreet Singh

Reputation: 1

your json data is like this as i given below:-

[{"announcement_title":"Sample Title Here","announcement_content":"Sample Content","announcement_date":"2016-02-04"}]

There is no Id in your Json data:-

//comment Id settext this line because no Id in your Json data

//id.setText(data.getString(0));

So you have to change your index value as give below in code:-

title.setText(data.getString(0));

content.setText(data.getString(1));

date.setText(data.getString(2));

Upvotes: 0

Uttam Kumar Nayak
Uttam Kumar Nayak

Reputation: 31

[{"announcement_title":"Sample Title Here",  //  0 pos
"announcement_content":"Sample Content",  //  1 pos
"announcement_date":"2016-02-04"}]  //  2 pos

Your json array Size is 3 but you are retrieving 4 items.

 id.setText(data.getString(0));
 title.setText(data.getString(1));
 content.setText(data.getString(2));
 date.setText(data.getString(3)); //remove this one

Let me know if this works.

Upvotes: 1

Saritha G
Saritha G

Reputation: 2608

There is no Id in your Json data.

Parse data from you JsonArray for given response and get the JsonObject from the array. Then parse the details from the JsonObject.

 String title =  data.getJSONObject(0).getString("announcement_title");
 String content = data.getJSONObject(0).getString("announcement_content");
 String date = data.getJSONObject(0).getString("announcement_date");

Set the json data to TextView's:

 title.setText(title); 
 content.setText(content);                                                                
 date.setText(date);

Upvotes: 1

Shoeb Siddique
Shoeb Siddique

Reputation: 2825

Error in parsing Data - Please see this -

title.setText(data.getJSONObject(0).getString("announcement_title"));//announcement_title
        content.setText(data.getJSONObject(0).getString("announcement_content"));//announcement_content
        date.setText(data.getJSONObject(0).getString("announcement_date"));//announcement_date

Here is Very Important link of parsing data - LINK

Upvotes: 1

Mohit Suthar
Mohit Suthar

Reputation: 9395

I think you are not proper parsing data, try this snippets will be helpfull

title.setText(data.getJSONObject(0).getString("announcement_title")); 
                      content.setText(data.getJSONObject(0).getString("announcement_content"));  
                     date.setText(data.getJSONObject(0).getString("announcement_date"));

Upvotes: 2

Related Questions