我正在使用 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; }};@Overrideprotected 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(); }
2 回答

收到一只叮咚
TA貢獻1821條經驗 獲得超5個贊
由于您使用的是默認值ArrayAdapter
,因此您正在渲染toString()
您的dataDetail
班級。您至少有以下選擇:
定義自定義
toString()
以dataDetail
呈現您需要的信息。就像是:@Override public String toString() { return "Name:" + carName; }
為了顯示汽車名稱。
定義并使用自定義適配器,您可以在其中使用自定義視圖持有者來呈現所需的數據。如果您使用的是
RecyclerView
.你也可以擴展
ArrayAdapter
。有關更多詳細信息,請參閱此答案。
我建議將 aRecyclerView
與自定義適配器一起使用,而不是 ,與此處ListView
介紹的類似。通過這種方式,您可以獲得很大的靈活性,并且您的應用程序將能夠以有效的方式呈現大量元素。在這里看到兩者之間的一個很好的比較。

精慕HU
TA貢獻1845條經驗 獲得超8個贊
dataDetail tData = new dataDetail(customerName, carName, appointmentDate, email, issueDescribe, timeForJob, costForJob, reliableOnCar, distanceJob, finalScore); allDetail.add(tData.getCustomerName());
arrya 適配器僅使用字符串類型的數組列表,因此它將 tdata 作為字符串放入 allDetails
添加回答
舉報
0/150
提交
取消