Android-在Listview中显示来自适配器的数据

冯德伦

我目前有一个应用程序,可从mysql数据库中提取数据并以原始JSON格式显示。我目前正在将这些数据推送到String变量中,并将其显示在特定活动的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;
}

}

这是单击“解析JSON”按钮时显示的内容。

listView填充后为空

为什么不显示任何帮助或建议,将不胜感激!

提前致谢!

尤内斯·泽布季

您的问题似乎在这里:

while(count<jsonObject.length()){

您不是在循环使用数组元素的数目,而是在使用映射的key:value对象的数目(即“ server_response”)为1的循环,您必须将此行更改为:

while(count<jsonArray.length()){

您只有第一个显示的元素,因为只有一个元素jsonObject.length()会返回1

从doc,JSONObject,length()方法中获取:

返回此对象中名称/值映射的数量。

在您的情况下,您只映射了一个名称/值("server_response":[array items...]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Android ArrayList适配器不显示ListView

如何使用适配器在Android ListView中显示Firestore集合

Android Studio ListView将不会显示适配器中的项目

Android中的ListView和适配器,是否属于适配器设计模式?

使用自定义适配器时如何从Android中的ListView获取数据

从Android中的ListView适配器获取TextView

在Android中刷新ListView适配器的正确方法

AlertDialog中的Android ListView-设置适配器

Android ListView中适配器和模板视图的组合

Android:从viewPager中的Fragment调用ListView的适配器的getview()

我的Android Listview适配器中的IndexOutOfBoundsException

如何在其适配器中刷新android listview

Android:ListView适配器中的业务逻辑?

我如何从适配器获取数据并在Android的“活动”中显示

到recyclerview的Firebase数据显示错误-RecyclerView:android中未连接适配器

适配器中的Android RecyclerView显示问题

Android - 在数据绑定适配器中传递视图

RecyclerView适配器中的数据绑定-Android

如何在Android的适配器中设置数据

如何在Android适配器中传递Json数据

Android ListView适配器未过滤

Android ListView适配器...不重绘

Android-Recyclerview适配器显示来自外部存储的图像

Android 设置单击更新和删除来自适配器的 ImageButton SQLite 数据不起作用

Android,适配器的getView中的NullPointerException

带有自定义适配器的 Android ListView 仅显示最后一项

来自 url 的 setRingtone 和适配器类中的 RecyclerView (Android)

ListView 中的数据来自 SQLite // 适配器错误

Android Stop recycler-查看已显示项目的适配器绑定数据