我正在从数据库中检索数据并在简单的textview中显示记录,但我想在listview中显示数据,因为我正在点击listview上执行某些操作。我不知道该怎么做,请帮我如何在listview中显示数据并编辑我的代码。感谢。
检索数据的代码
public class RDatawithID extends Activity{
private Button buttonGet;
private TextView textViewResult;
ListView listCollege1;
ProgressBar proCollageList;
String Resp;
private ProgressDialog loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rdatawith_id);
listCollege1 = (ListView)findViewById(R.id.tasklist);
textViewResult = (TextView) findViewById(R.id.textViewResult);
System.out.println("In Oncreate method");
getData();
}
private void getData() {
String id = String.valueOf(1);
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+id;
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(RDatawithID.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
System.out.println("Response - ---- "+response);
String name="";
String address="";
String vc = "";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
Task1_ = new ArrayList<String>();
for(int x=0;x<result.length();x++) {
JSONObject collegeData = result.getJSONObject(x);
name = collegeData.getString(Config.KEY_NAME);
address = collegeData.getString(Config.KEY_ADDRESS);
vc = collegeData.getString(Config.KEY_VC);
textViewResult.setText(textViewResult.getText() + "\n" + "Name:\t" + name + "\nAddress:\t" + address + "\nVice Chancellor:\t" + vc);
}
} catch (JSONException e) {
e.printStackTrace();
}
//textViewResult.setText("Name:\t"+name+"\nAddress:\t" +address+ "\nVice Chancellor:\t"+ vc);
}
再次感谢。