列表视图不显示数据

时间:2016-03-23 13:24:50

标签: android listview

所以最近我有一个问题,我无法在我的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 + " ");
       }

循环通过JSON数据循环,并将所有记录添加到数据库

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

EXCEPTION CATCHER

   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 ADAPTER声明并设置,数据设置为“datalist”

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

我打破了这个以使其易于阅读,谢谢大家的帮助,因为我们非常感激。

2 个答案:

答案 0 :(得分:2)

首先,添加数据并通知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();

       }

答案 1 :(得分:0)

更好的解决方案是创建适配器并在循环结束后将其设置为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);
    }