我正在创建一个Android应用程序。我有一个TextView和一个ListView。我尝试检索数据并在TextView上显示,它工作。然后我进一步改进想要在listview上显示,但它只显示" null"。我不知道为什么......
public class History extends AppCompatActivity {
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
ListView HistoryList;
String HotelName;
String DoorCode;
String DateIn;
String DateOut;
String RoomNum;
String customerID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
getData();
addData();
HistoryList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position){
case 0:
startActivity(new Intent(History.this, UserApplication.class));
break;
case 1:
startActivity(new Intent(History.this, UserApplication.class));
break;
case 2:
startActivity(new Intent(History.this, UserApplication.class));
break;
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
startActivity(new Intent(History.this, Setting.class));
return true;
}
else if(id == R.id.History){
startActivity(new Intent(History.this, History.class));
return true;
}
return super.onOptionsItemSelected(item);
}
public void addData(){
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
// listItems.add(HotelName[x]+"\n"+(CounterTime[x])+"\n Check In Date: "+(CheckInDate[x])+"\n Check Out Date: "+(CheckOutDate[x])
// +"\n Room Type: "+(RoomTypeName[x])+"\n Electrical Usage: "+(ElecticityTypeName[x]));
listItems.add(HotelName+"\n"+DateIn+"\n"+DateOut+"\n"+RoomNum);
listItems.add(1,"Deal");
listItems.add(2,"Dea2");
listItems.add(3,"Dea3");
listItems.add(4,"Dea4");
listItems.add(5,"Dea5");
adapter.notifyDataSetChanged();
}
protected ListView getListView() {
if (HistoryList == null) {
HistoryList = (ListView) findViewById(R.id.HistoryList);
}
return HistoryList;
}
protected void setListAdapter(ListAdapter adapter) {
getListView().setAdapter(adapter);
}
//---Start getData from Database ---//
private ProgressDialog loading;
private void getData() {
loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
customerID = MainActivity.getID();
TextView txtviewbla;
txtviewbla = (TextView) findViewById(R.id.textView3);
txtviewbla.setText(customerID);
// String abc = LoginActivity.getEmail(); // check back from login activity
String url = "http://vlc2017-001-site1.1tempurl.com/get-purchasehistory.php?CustomerID="+customerID;
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(History.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
public void showJSON(String response){
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray("result");
JSONObject collegeData = result.getJSONObject(0);
HotelName = collegeData.getString("HotelName");
DoorCode = collegeData.getString("DoorCode");
DateIn = collegeData.getString("DateIn");
DateOut = collegeData.getString("DateOut");
RoomNum = collegeData.getString("RoomNum");
// name = collegeData.getString("name");
// address = collegeData.getString("address");
// vc = collegeData.getString("vc");
} catch (JSONException e) {
e.printStackTrace();
}
TextView txtviewbla;
txtviewbla = (TextView) findViewById(R.id.textView3);
txtviewbla.setText(customerID+HotelName+DoorCode+DateIn+DateOut+RoomNum);
}
}
Value可以在textview中显示,是不是我不能直接使用Listview中的变量? ListView仅显示为null