0

我已从数据库中检索数据并使用名为 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;

}

4

3 回答 3

1

改变

   arrStr = (String[]) alSearchResult.toArray();   

   arrStr = alSearchResult.toArray(new String[0]);
于 2013-06-13T07:23:21.703 回答
1

请不要在后台线程中访问您的 listView lsSearchResult。将结果传递给 onPostExecute()。

尝试使用以下代码:

private class SearchTask extends AsyncTask<Void, Void, ArrayList<String>>{
@Override
   protected ArrayList<String> doInBackground(Void... arg0) {

     DatabaseHandler db = new DatabaseHandler();          

     String Place = etSearch.getText().toString();
     AddDetails details = new AddDetails(Place);
     alSearchResult = db.searchResultByPlace(details);

     return alSearchResult;
}

protected void onPostExecute(ArrayList<String> result) {
     ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(SearchActivity.this,android.R.layout.simple_list_item_1, result);
     lsSearchResult.setAdapter(arrayAdapter); 
}
于 2013-06-13T07:32:22.593 回答
0
ArrayList<String> stock_list = new ArrayList<String>();
    stock_list.add("stock1");
    stock_list.add("stock2");
    String[] stockArr = new String[stock_list.size()];
    stockArr = stock_list.toArray(stockArr);
    for(String s : stockArr)
        System.out.println(s);

这对 arraylist 到数组的转换很有帮助,您也可以直接将 arraylist 与 arrayadapter 一起使用,无需将其转换为数组

于 2013-06-13T07:23:11.577 回答