【问题标题】:List view Not Displaying Data列表视图不显示数据
【发布时间】:2016-03-23 21:40:11
【问题描述】:

最近我遇到了一个问题,我无法从我的 android 应用程序中的在线数据库中获取多个记录来显示。我使用的是文本视图,但被建议使用列表视图。在这里的一些好心人的帮助下,我已经指出了正确的方向,但由于列表视图没有显示任何内容,我仍然遇到了麻烦。我将附上我为这篇文章注释的代码。

作为参考,我使用 volley 和 php,通过 url 访问数据时,例如:http://example.url/data.php/$id=1

public class GetExerciseList extends AppCompatActivity implements View.OnClickListener {


private EditText editTextId;
private Button buttonGet;
private TextView textViewResult;
ListView listView ;
ArrayAdapter adapter;
private ArrayList<String> dataList = new ArrayList<>();
private ProgressDialog loading;

初始化视图

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

    editTextId = (EditText) findViewById(R.id.editTextId);
    buttonGet = (Button) findViewById(R.id.buttonGet);
    textViewResult = (TextView) findViewById(R.id.textViewResult);

    ListView listView = (ListView) findViewById(R.id.list);
    adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, dataList);
    listView.setAdapter(adapter);

    buttonGet.setOnClickListener(this);
}

声明适配器和视图

private void getData() {
    String id = editTextId.getText().toString().trim();
    if (id.equals("")) {
        Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
        return;
    }
    loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
    String url = Config.DATA_URL+editTextId.getText().toString().trim();
    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            loading.dismiss();
            showJSON(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {                       Toast.makeText(GetExerciseList.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

根据匹配 ID 获取数据

private void showJSON(String response){
    String musclegroup="";
    String exercise="";

    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
        JSONObject collegeData;
       for(int i = 0; i<result.length(); i++) {
            collegeData = result.getJSONObject(i);
            musclegroup = collegeData.getString(Config.KEY_MUSCLEGROUP);
            exercise = collegeData.getString(Config.KEY_EXERCISE);

           adapter.notifyDataSetChanged();
           dataList.add(musclegroup + " " + exercise + " ");
       }

FOR 循环遍历 JSON 数据,并将所有记录添加到数据列表

    } catch (JSONException e) {
        e.printStackTrace();
    }

异常捕捉器

   textViewResult.setText("Target Muscle:\t" + musclegroup + "\nLift:\t" + exercise);
  //  this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, dataList));

    ListView lv = (ListView)findViewById(R.id.list);
    lv.setAdapter(adapter);
    ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, dataList);

}

TEXTVIEW 离开只是为了检查数据。列表视图当前没有输出 LISTVIEW 适配器声明并设置,数据集为“datalist”

@Override
public void onClick(View v) {
    getData();
}
}

我已将其分解以使其易于阅读,感谢大家的帮助,一如既往地非常感谢。

【问题讨论】:

  • interchange adapter.notifyDataSetChanged(); dataList.add(musclegroup + " " + exercise + " "); 先加入arraylist 然后通知适配器
  • 您收到回复了吗?您是否检查过 dataList arraylist 是否有数据。它的大小是多少?
  • 嘿,我设法让它显示,但我现在又回到它只显示 1 条记录的问题,当它应该显示多个时

标签: android listview


【解决方案1】:

首先,添加数据并通知adatper。

private void showJSON(String response){
    String musclegroup="";
    String exercise="";
try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
        JSONObject collegeData;
       for(int i = 0; i<result.length(); i++) {
            collegeData = result.getJSONObject(i);
            musclegroup = collegeData.getString(Config.KEY_MUSCLEGROUP);
            exercise = collegeData.getString(Config.KEY_EXERCISE);
           dataList.add(musclegroup + " " + exercise + " ");
           adapter.notifyDataSetChanged();

       }

【讨论】:

  • 已应用更改。仍然没有收到列表视图输出。感谢您的帮助
  • 我现在已经设法产生了一个输出,但它没有显示多个记录
【解决方案2】:

更好的解决方案是创建您的适配器并在循环完成后将其设置为ListView,而不是每次在循环中调用adapter.notifyDataSetChanged();,直到迭代您的响应。

private void showJSON(String response){
    String musclegroup="";
    String exercise="";
   try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
        JSONObject collegeData;
        for(int i = 0; i<result.length(); i++) {
            collegeData = result.getJSONObject(i);
            musclegroup = collegeData.getString(Config.KEY_MUSCLEGROUP);
            exercise = collegeData.getString(Config.KEY_EXERCISE);
           dataList.add(musclegroup + " " + exercise + " ");
       }

    adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, dataList);
    listView.setAdapter(adapter);
    }

【讨论】:

  • 谢谢你,这确实看起来更精致,即使代码通过 notifydatasetchanged 进行迭代也是如此。我仍然没有收到来自列表视图的输出。我只能在屏幕上看到文本视图
  • 可能是您没有响应数据,但是发生了异常。你检查了吗?
  • 我现在已经设法显示列表视图。唯一的问题是我只能看到 1 条记录,不确定我的问题是什么,列表视图现在看起来很好,问题出在我的 java 循环某处
  • hm... 尝试记录您的 JSONArray 长度:Log.d("JSONArray length ",result.length() + ""); 并告诉我它是怎么回事。
  • 它以“长度 1”的形式返回。什么时候应该是 3 条记录匹配
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-24
  • 2020-07-13
  • 2014-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多