我正在使用ListView来显示从我的数据库中检索到的数据,它本来可以工作,但是突然不显示任何东西。通常,它应该显示类似以下内容(其他项目的屏幕截图):
编辑后,发布我的所有代码:
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
String[] dataDetail;
ArrayList<dataDetail> allDetail = new ArrayList<>();
ListView detailList;
final Context context = this;
final int min = 0;
final int max = 10;
final int random = new Random().nextInt((max - min) + 1) + min;
int score = 0;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
detailList = findViewById(R.id.dataView);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
new GetData().execute();
}
private class GetData extends AsyncTask<Void, Void, ArrayList<dataDetail>> {
protected ArrayList<dataDetail> doInBackground(Void... voids) {
HttpURLConnection urlConnection;
InputStream in = null;
try {
URL url = new URL("http://10.0.2.2:8080/projectServer/DataDetailDB?getdata=true");
urlConnection = (HttpURLConnection) url.openConnection();
in = new BufferedInputStream(urlConnection.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
String response = convertStreamToString(in);
System.out.println("Server response = " + response);
try {
JSONArray jsonArray = new JSONArray(response);
dataDetail = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
final String customerName = jsonArray.getJSONObject(i).get("customerName").toString();
final String carName = jsonArray.getJSONObject(i).get("carName").toString();
final String appointmentDate = jsonArray.getJSONObject(i).get("appointmentDate").toString();
final String email = jsonArray.getJSONObject(i).get("email").toString();
final String issueDescribe = jsonArray.getJSONObject(i).get("issueDescribe").toString();
final String timeForJob = jsonArray.getJSONObject(i).get("timeForJob").toString();
final String costForJob = jsonArray.getJSONObject(i).get("costForJob").toString();
final String reliableOnCar = jsonArray.getJSONObject(i).get("reliableOnCar").toString();
final String distanceJob = jsonArray.getJSONObject(i).get("distance").toString();
final int finalScore = score;
if (random * Float.parseFloat(timeForJob) < 15) {
score += 1;
} if (random * Float.parseFloat(reliableOnCar) > 15) {
score += 1;
} if (random * Float.parseFloat(distanceJob) > 10) {
score -=1;
} if (random * Float.parseFloat(costForJob) > 40) {
score -=1;
} if(random * Float.parseFloat(timeForJob) + random * Float.parseFloat(reliableOnCar) +
random * Float.parseFloat(distanceJob) + random * Float.parseFloat(costForJob) > 500) {
score += 5;
} else {
score += 0;
}
dataDetail tData = new dataDetail(customerName, carName, appointmentDate, email, issueDescribe, timeForJob, costForJob, reliableOnCar, distanceJob, finalScore);
allDetail.add(tData);
System.out.println("customername = " + customerName + ", Score = " + finalScore);
if (score >= 5) {
dataDetail[i] = "Name: " + customerName + "\n" + "Appointment Date: " + appointmentDate + "\n" + "Urgent";
} else if (score >= 3) {
dataDetail[i] = "Name: " + customerName + "\n" + "Appointment Date: " + appointmentDate + "\n" + "Second urgent";
} else {
dataDetail[i] = "Name: " + customerName + "\n" + "Appointment Date: " + appointmentDate + "\n" + "Normal";
}
System.out.print("Debug: " + dataDetail);
Collections.sort(allDetail, new Comparator<advprog.mmu.ac.uk.schedulerapp.dataDetail>() {
@Override
public int compare(dataDetail o1, dataDetail o2) {
return Integer.compare(o1.getFinalScore(), o2.getFinalScore());
}
});
Collections.reverse(allDetail);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(ArrayList<dataDetail> dataDetailArrayList) {
super.onPostExecute(dataDetailArrayList);
ArrayAdapter List = new ArrayAdapter(context, android.R.layout.simple_list_item_1, allDetail);
detailList.setAdapter(List);
}
}
public String convertStreamToString(InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}}
dataDetial类:
公共类dataDetail {
private String customerName;
private String carName;
private String appointmentDate;
private String email;
private String issueDescribe;
private String timeForJob;
private String costForJob;
private String reliableOnCar;
private String distanceJob;
private int finalScore;
public dataDetail(String customerName, String carName, String appointmentDate, String email, String issueDescribe, String timeForJob, String costForJob, String reliableOnCar, String distanceJob, int finalScore) {
this.customerName = customerName;
this.carName = carName;
this.appointmentDate = appointmentDate;
this.email = email;
this.issueDescribe = issueDescribe;
this.timeForJob = timeForJob;
this.costForJob = costForJob;
this.reliableOnCar = reliableOnCar;
this.distanceJob = distanceJob;
this.finalScore = finalScore;
}
public int getFinalScore() {
return finalScore;
}
public void setFinalScore(int finalScore) {
this.finalScore = finalScore;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public String getAppointmentDate() {
return appointmentDate;
}
public void setAppointmentDate(String appointmentDate) {
this.appointmentDate = appointmentDate;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getIssueDescribe() {
return issueDescribe;
}
public void setIssueDescribe(String issueDescribe) {
this.issueDescribe = issueDescribe;
}
public String getTimeForJob() {
return timeForJob;
}
public void setTimeForJob(String timeForJob) {
this.timeForJob = timeForJob;
}
public String getCostForJob() {
return costForJob;
}
public void setCostForJob(String costForJob) {
this.costForJob = costForJob;
}
public String getReliableOnCar() {
return reliableOnCar;
}
public void setReliableOnCar(String reliableOnCar) {
this.reliableOnCar = reliableOnCar;
}
public String getDistanceJob() {
return distanceJob;
}
public void setDistanceJob(String distanceJob) {
this.distanceJob = distanceJob;
} }
我认为这行dataDetail [i] =“名称:” +客户名称+“ \ n” +“约会日期:” +约会日期+“ \ n” +“紧急”;将显示我想在ListView中显示的数据,但它显示的数据是这样的:
当我尝试打印出dataDetail时,在logcat中它给出了以下信息:
I / System.out:customername = Josh,分数= 0 调试:[Ljava.lang.String; @ e883626customername = Wolski,分数= 1 I / System.out:调试:[Ljava.lang.String; @ e883626customername = Cardinal,Score = 2 I / System.out:调试:[Ljava.lang.String; @ e883626customername = Pang,Score = 3
答案 0 :(得分:1)
由于您使用的是默认的ArrayAdapter
,因此正在呈现toString()
类的dataDetail
。您至少有以下选择:
在toString()
中定义自定义dataDetail
,以显示所需的信息。像这样:
@Override 公共字符串toString(){ 返回“名称:” + carName; }
为了显示汽车名称。
定义并使用自定义适配器,您可以在其中使用自定义视图持有者来呈现所需的数据。如果您使用的是RecyclerView
,请参见here。
您也可以扩展ArrayAdapter
。有关更多详细信息,请参见此answer。
我建议您使用带有自定义适配器的RecyclerView
,而不是ListView
,它类似于here。这样,您将获得很大的灵活性,并且您的应用程序将能够高效地呈现很多元素。参见here,两者之间有一个很好的比较。
答案 1 :(得分:0)
dataDetail tData = new dataDetail(customerName, carName, appointmentDate, email, issueDescribe, timeForJob, costForJob, reliableOnCar, distanceJob, finalScore);
allDetail.add(tData.getCustomerName());
arrya适配器仅使用字符串类型数组列表,因此它将tdata作为字符串放入allDetails中