【问题标题】:Android SetText not working on TextVIew but the value is thereAndroid SetText 在 TextVIew 上不起作用,但值在那里
【发布时间】:2016-02-05 06:32:01
【问题描述】:

伙计们,我正在尝试设置Textview,但它没有改变,数据库中的值在那里。我希望有人可以提供帮助

这是java代码

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);


    }
}

这是来自 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"}]

【问题讨论】:

  • Json 数组包含 JsonObject,因此您需要在将 set 添加到 textview 之前获取它。

标签: java php android sql


【解决方案1】:

我认为你没有正确解析数据,试试这个 sn-ps 会有所帮助

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

【讨论】:

    【解决方案2】:

    解析数据时出错 - 请看这个 -

    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
    

    这是解析数据的非常重要的链接-LINK

    【讨论】:

      【解决方案3】:

      您的 Json 数据中没有 Id。

      从您的 JsonArray 中解析数据以获得给定的响应,并从数组中获取 JsonObject。然后从 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");
      

      将json数据设置为TextView的:

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

      【讨论】:

        【解决方案4】:
        [{"announcement_title":"Sample Title Here",  //  0 pos
        "announcement_content":"Sample Content",  //  1 pos
        "announcement_date":"2016-02-04"}]  //  2 pos
        

        您的 json 数组大小为 3,但您正在检索 4 个项目。

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

        让我知道这是否有效。

        【讨论】:

        • 这根本行不通,没有键我们无法获取值。
        【解决方案5】:

        您的 json 数据如下所示:-

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

        您的 Json 数据中没有 Id:-

        //comment Id settext 这一行因为你的Json数据中没有Id

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

        所以你必须改变你的索引值,如下面的代码:-

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-13
          • 1970-01-01
          相关资源
          最近更新 更多