Android - 在Listview中显示来自Adapter的数据

时间:2016-07-21 13:40:58

标签: android json listview adapter android-studio-2.1

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

以下是MainActivity代码片段:

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

这是我的Test.java的代码

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

}

以下是我的DataAdapter的代码:

@Injectable()
export class CommunicationService {
    private _broadcast = new Subject<EventParam>();

    broadcast$ = this._broadcast.asObservable();

    public sendEvent(eventParameters: EventParam): void {
        this._broadcast.next(eventParameters); 
    }
}

}

以下是点击&#34; Parse JSON&#34;按钮。

listView empty after population

任何帮助或建议为什么它不显示将非常感谢!

提前致谢!

2 个答案:

答案 0 :(得分:0)

签入Test.java。我想你是在将数据添加到listview之前将适配器设置为listview

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
    }

答案 1 :(得分:0)

你的问题似乎在这里:

while(count<jsonObject.length()){

你没有使用数组元素的数量进行循环,而是使用映射键的数量:value对象是一个(“server_response”),你必须将这一行更改为:

while(count<jsonArray.length()){

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

来自doc,JSONObject,length()方法:

  

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

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