Android - 在列表视图中显示来自 Adapter 的数据

Android - Display data from Adapter in Listview

我目前有一个应用程序可以从 mysql 数据库中提取数据并以 JSON 原始格式显示。我目前正在努力将此数据推送到一个字符串变量中,并将其显示在特定 activity 的 Listview 上。 问题是,当试图显示这些数据时,我的 Listview 没有填充;我确定该变量不为空,因为 if 语句会捕获它。

这是 MainActivity 代码片段:

//Methods to grab information from abhandym_DB database
public void getJSON(View view){
new BackgroundTask().execute();
}

public void parseJSON(View view){
if(JSON_String==null){
    Toast.makeText(getApplicationContext(), "First Get Json", Toast.LENGTH_LONG).show();
}else{
    Intent intent = new Intent(this,Test.class);
    intent.putExtra("JSON_Data",JSON_String);
    startActivity(intent);
}
}

class BackgroundTask extends AsyncTask<Void,Void,String>{

    String json_url;

    @Override
    protected void onPreExecute() {
        json_url = "http://abhandyman.x10host.com/json_get_data.php";
    }

    @Override
    protected String doInBackground(Void... params) {
        try {
            URL url = new URL(json_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            InputStream inputSteam = httpURLConnection.getInputStream();
            BufferedReader buffereredReader = new BufferedReader(new InputStreamReader(inputSteam));
            StringBuilder stringBuilder = new StringBuilder();
            while((JSON_String = buffereredReader.readLine())!=null){
            stringBuilder.append(JSON_String+"\n");
            }
            buffereredReader.close();
            inputSteam.close();
            httpURLConnection.disconnect();
            return stringBuilder.toString().trim();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
        TextView textView = (TextView)findViewById(R.id.fragment1_textview_JSONAPPEAR);
        textView.setText(result);
        JSON_String = result;
    }
}

这是我的 Test.java

的代码
public class Test extends AppCompatActivity {
String JSON_String;
JSONObject jsonObject;
JSONArray jsonArray;
DataAdapter dataAdapter;
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_layout);
    listView = (ListView)findViewById(R.id.test_listView);
    dataAdapter = new DataAdapter(this, R.layout.row_layout);
    listView.setAdapter(dataAdapter);
    JSON_String = getIntent().getExtras().getString("JSON_Data");
    try {
        jsonObject = new JSONObject(JSON_String);
        jsonArray = jsonObject.getJSONArray("server_response");
        int count = 0;
        String jobid,problem,resolution;
        while(count<jsonObject.length()){
            JSONObject JO = jsonArray.getJSONObject(count);
            jobid = JO.getString("jobid");
            problem = JO.getString("problem");
            resolution = JO.getString("resolution");
            Data data = new Data(jobid,problem,resolution);
            dataAdapter.add(data);
            count++;
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

}

这是我的 DataAdapter 的代码:

public class DataAdapter extends ArrayAdapter{
List list = new ArrayList();
public DataAdapter(Context context, int resource) {
    super(context, resource);
}

public void add(Data object) {
    super.add(object);
    list.add(object);
}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row;
    row = convertView;
    DataHolder dataHolder;
    if(row == null){
        LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.row_layout,parent,false);
        dataHolder = new DataHolder();
        dataHolder.tx_jobid = (TextView) row.findViewById(R.id.tx_jobid);
        dataHolder.tx_problem = (TextView) row.findViewById(R.id.tx_problem);
        dataHolder.tx_resolution = (TextView) row.findViewById(R.id.tx_resolution);
        row.setTag(dataHolder);
    }else{
    dataHolder = (DataHolder)row.getTag();
    }
    Data data = (Data)this.getItem(position);
    dataHolder.tx_jobid.setText(data.getJobid());
    dataHolder.tx_problem.setText(data.getProblem());
    dataHolder.tx_resolution.setText(data.getResolution());
    return row;
}

static class DataHolder{
TextView tx_jobid,tx_problem,tx_resolution;
}

}

这是单击 "Parse JSON" 按钮时显示的内容。

listView empty after population

如能就其未显示的原因提供任何帮助或建议,我们将不胜感激!

提前致谢!

签到Test.java。我认为您在向列表视图添加数据之前将适配器设置为列表视图

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_layout);
        listView = (ListView)findViewById(R.id.test_listView);
        dataAdapter = new DataAdapter(this, R.layout.row_layout);

        JSON_String = getIntent().getExtras().getString("JSON_Data");
        try {
            jsonObject = new JSONObject(JSON_String);
            jsonArray = jsonObject.getJSONArray("server_response");
            int count = 0;
            String jobid,problem,resolution;
            while(count<jsonObject.length()){
                JSONObject JO = jsonArray.getJSONObject(count);
                jobid = JO.getString("jobid");
                problem = JO.getString("problem");
                resolution = JO.getString("resolution");
                Data data = new Data(jobid,problem,resolution);
                dataAdapter.add(data);
                count++;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
         listView.setAdapter(dataAdapter); //change effected
    }

您的问题似乎出在这里:

while(count<jsonObject.length()){

你不是使用数组元素的数量循环,而是使用映射的 key:value 对象的数量,它是一个("server_response"),你必须将此行更改为:

while(count<jsonArray.length()){

,

您只显示了第一个元素,因为 jsonObject.length() 将 return 1 因为它只有一个元素。

来自文档、JSONObject、length() 方法:

Returns the number of name/value mappings in this object.

在您的情况下,您只有一个 name/value 映射 ("server_response":[array items...])