I'm using ListView to showing the data retrieved from my database, it suppose work before, but it suddenly doesn't and show things. Normally it should showing things like this (Screenshot from other project):
Edited, post my all code:
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 Class:
public class 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;
} }
I think this line dataDetail[i] = "Name: " + customerName + "\n" + "Appointment Date: " + appointmentDate + "\n" + "Urgent"; will be showing the data I want to show in the ListView, but it shows the data like this:
And when I trying to print out the dataDetail, in logcat it gives me this:
I/System.out: customername = Josh, Score = 0 Debug: [Ljava.lang.String;@e883626customername = Wolski, Score = 1 I/System.out: Debug: [Ljava.lang.String;@e883626customername = Cardinal, Score = 2 I/System.out: Debug: [Ljava.lang.String;@e883626customername = Pang, Score = 3