我已从数据库中检索数据并使用名为 AddDetails 的类添加到 Arraylist。在 AddDetails 类中,我正在分配所有值。然后我将该 ArrayList 发送到我需要在 ListView 中显示该数据的活动类。现在,在将数据从 ArrayList 复制到 Array 时,我遇到了诸如找不到类之类的错误,或者在列表视图上显示时它只是显示了一些垃圾值。
代码如下:
private class SearchTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
DatabaseHandler db = new DatabaseHandler();
String Place = etSearch.getText().toString();
AddDetails details = new AddDetails(Place);
alSearchResult = db.searchResultByPlace(details);
arrStr = new String[alSearchResult.size()];
try
{
arrStr = (String[]) alSearchResult.toArray();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(SearchActivity.this, android.R.layout.simple_list_item_multiple_choice, arrStr);
lsSearchResult.setAdapter(adapter);
}
catch(Exception e)
{
Log.e("Error:" + e.toString());
}
return null;
}
protected ArrayList<AddDetails> searchResultByPlace(AddDetails details)
{
ArrayList<AddDetails> alSearchData = null;
try
{
String place = details.getPlace();
Cursor c = mDataBase.rawQuery("SELECT * FROM " + ESTATE_DETAILS + " WHERE " + strSearchType + " = " + "'" + place + "'", null);
c.moveToFirst();
if(c.getCount() > 0)
{
alSearchData = new ArrayList<AddDetails>();
if (c.moveToFirst())
{
do {
details = new AddDetails();
details.setID(Integer.parseInt(c.getString(0)));
details.setPlace(c.getString(1));
details.setArea(c.getString(2));
details.setType(c.getString(3));
details.setPhoneNumber(c.getString(4));
// Adding contact to list
alSearchData.add(details);
} while (c.moveToNext());
}
}
c.close();
}
catch(SQLException e)
{
Log.e("Error:", e.toString());
}
return alSearchData;
}