0

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):

enter image description here

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:

enter image description here

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

RickyPang
  • 55
  • 1
  • 8
  • 1
    You are using the `toString()` method without having overridden it. The object returned by `jsonArray.getJSONObject(i).get("...").toString()` seems to be the default one. Can't you just leave the `.toString()` and only apply `jsonArray.getJSONObject(i).get("...")`? – deHaar Feb 22 '19 at 12:14
  • It will show error Required: Java.lang.String, Found: java.lang.object – RickyPang Feb 22 '19 at 12:16
  • @DarshanKachhadiya The OP is using the ArrayAdapter, see `onPostExecute` method – dan Feb 22 '19 at 12:21

2 Answers2

1

Since you are using the default ArrayAdapter you are rendering the toString() of your dataDetail class. You have at least the following choices:

  1. Define a custom toString() in dataDetail to present the information that you need. Something like:

    @Override public String toString() { return "Name: " + carName; }

    In order to present the car names.

  2. Define and use a custom adapter where you can use a custom view holder to render the desired data. See here for more details if you are using a RecyclerView.

  3. You can also extend the ArrayAdapter too. See this answer for more details.

I would recommend using a RecyclerView with a custom adapter, instead of the ListView, one similar to what is presented here. This way you gain a lot of flexibility and your app will be able to present a lot of elements in an efficient way. See here a good comparison between the two.

dan
  • 13,132
  • 3
  • 38
  • 49
  • So I just need to use RecyclerView rather than ListView? – RickyPang Feb 22 '19 at 12:26
  • No, you can use the `ListView` too. The simplest fix is to use the first method that I mentioned, just add a `toString` method in your `dataDetail` class with the fields needed to be displayed. It will work with your `ListView` too. The `RecyclerView` is the recommended way if you have a lot of elements to display. But you can use a custom adapter with the `ListView` in a similar way. – dan Feb 22 '19 at 12:27
  • @RickyPang Correct. If you are using the first method, just return a string containing the data that you would like to be presented on each row. – dan Feb 22 '19 at 12:41
  • Emmm..it works but now my list is showing things like DataDetail [customerName=Josh, carName=Toyota Aygo, appointmentDate=01/03/2019, email=Josh@gmail.com, issueDescribe=The engine is down and can not initiate, timeForJob=3, costForJob=200, reliableOnCar=9, distance=7] – RickyPang Feb 22 '19 at 12:41
  • @RickyPang This is because you autogenerated your `toString` method. Customize the body of the generated method with the needed content. – dan Feb 22 '19 at 12:42
  • It works, but my code is working without toString() before, it just suddenly become like this, why is that? Cause this line of the code should showing the data in the list as well dataDetail [i] = "Name: " + customerName + "\n" + "Appointment Date: " + appointmentDate + "\n" + "Urgent"; – RickyPang Feb 22 '19 at 13:00
  • I got this output in Logcat if I try to print out the dataDetail: [Ljava.lang.String;@e883626customername = Wolski, Score = 4 – RickyPang Feb 22 '19 at 13:03
  • @RickyPang The string presented on the last image that you added seems to be from the default toString autogenerated by your IDE. If you do not have a toString, than I think you should post all the code, eg. from your `dataDetail` class too, since it is not clear how the class is used. – dan Feb 22 '19 at 13:07
  • @RickyPang On console you are seeing the output from printing `allDetail` object, but on `ListView` you are displaying the default `toString` from `Object` of the `dataDetail` list. That is why you are seeing the differences. Like I mentioned in my answer in order to establish the output presented in the list just use one of the listed methods. – dan Feb 22 '19 at 13:30
0
 dataDetail tData = new dataDetail(customerName, carName, appointmentDate, email, issueDescribe, timeForJob, costForJob, reliableOnCar, distanceJob, finalScore);
            allDetail.add(tData.getCustomerName());

arrya adapter use only string type array list so it put tdata as string in allDetails