Java 我的ListView不显示我的数据,而是显示我的包名

Java 我的ListView不显示我的数据,而是显示我的包名,java,android,listview,Java,Android,Listview,我使用ListView来显示从数据库检索到的数据,它以前假设可以工作,但突然不工作,并显示一些东西。通常它应该显示如下内容(其他项目的屏幕截图): 编辑,发布我的全部代码: public class MainActivity extends AppCompatActivity { private TextView mTextMessage; String[] dataDetail; ArrayList<dataDetail> allDetail = new ArrayList&

我使用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() : "";
}}
我认为这行dataDetail[I]=“Name:”+customerName+“\n”+“约会日期:”+appointmentDate+“\n”+“紧急”;将显示我想在ListView中显示的数据,但它显示的数据如下:

当我试图打印数据详细信息时,在logcat中,它给了我以下信息:

I/System.out:customername=Josh,分数=0 调试:[Ljava.lang.String;@e883626customername=Wolski,分数=1 I/System.out:Debug:[Ljava.lang.String;@e883626customername=Cardinal,Score=2
I/System.out:Debug:[Ljava.lang.String;@e883626customername=Pang,Score=3

因为您使用的是默认的
ArrayAdapter
您正在呈现
dataDetail
类的
toString()

  • dataDetail
    中定义自定义
    toString()
    ,以显示所需的信息。类似于:

    @凌驾 公共字符串toString(){ return“Name:+carName; }

    以呈现汽车名称

  • 定义并使用自定义适配器,您可以在其中使用自定义视图保持架来呈现所需的数据。有关使用
    RecyclerView
    的更多详细信息,请参阅

  • 您还可以扩展
    ArrayAdapter
    。有关更多详细信息,请参阅此

  • 我建议使用带有自定义适配器的
    RecyclerView
    ,而不是类似于所显示内容的
    ListView
    。这样您可以获得很大的灵活性,并且您的应用程序将能够以高效的方式显示大量元素。请参阅两者之间的良好比较

     dataDetail tData = new dataDetail(customerName, carName, appointmentDate, email, issueDescribe, timeForJob, costForJob, reliableOnCar, distanceJob, finalScore);
                allDetail.add(tData.getCustomerName());
    

    arrya adapter只使用字符串类型数组列表,因此它将tdata作为字符串放入allDetails

    您使用的是
    toString()
    方法,而没有覆盖它。由
    jsonArray.getJSONObject(i.get(“…”)返回的对象。toString()
    似乎是默认值。您不能只保留
    .toString()吗
    并且仅应用
    jsonArray.getJSONObject(i).get(“…”)
    ?它将显示所需的错误:Java.lang.String,找到的:Java.lang。object@DarshanKachhadiyaOP正在使用ArrayAdapter,请参见附件列表中的methodadd(dataDteail)无法应用于(java.lang.String)我在Arraylist中得到了errorallDetail Arraylist,所以我只需要使用RecyclerView而不是ListView?不,你也可以使用
    ListView
    。最简单的解决方法是使用我提到的第一个方法,只需在
    dataDetail
    类中添加一个
    toString
    方法,其中包含需要显示的字段。它将使用
    ListView
    也可以。如果有很多元素要显示,建议使用
    RecyclerView
    方法。但是,您可以用类似的方式将自定义适配器与
    ListView
    一起使用。@RickyPang Correct。如果使用第一种方法,只需返回一个包含要显示的数据的字符串即可每一行上都有d。Emmm..它起作用了,但现在我的列表显示的是DataDetail[customerName=Josh,carName=Toyota Aygo,appointmentDate=2019年3月1日,电子邮件=Josh@gmail.com,IssueDescripte=发动机停机且无法启动,timeForJob=3,costForJob=200,reliableOnCar=9,distance=7]@RickyPang这是因为您自动生成了
    toString
    方法。请使用所需内容自定义生成方法的主体。
     dataDetail tData = new dataDetail(customerName, carName, appointmentDate, email, issueDescribe, timeForJob, costForJob, reliableOnCar, distanceJob, finalScore);
                allDetail.add(tData.getCustomerName());