0

I have retrieved data from database and added to Arraylist using a class called AddDetails. In AddDetails class I am assigning all values. Then I have sent that ArrayList to activity class where I need to display that data in ListView. Now while copying data from ArrayList to Array I am getting error like class not found or while displaying on list view it is just showing some garbage value.

The code is as follows:

 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
  • you can use the arraylist to display data in listview why do you need to convert arraylist to array? Commented Jun 13, 2013 at 7:19
  • what are you convert into array..?? just use that arraylist for listview.. Commented Jun 13, 2013 at 7:20
  • "it is just showing some garbage value." Did you override toString method in the AddDetails class ? Commented Jun 13, 2013 at 7:21
  • when I try to display directly from Arraylist (See my code in Blackbelt's answer) it is showing com.example.realestate.AddDetails@40e253d8 data details are coming from AddDetails class. Commented Jun 13, 2013 at 9:11

3 Answers 3

1

change

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

with

   arrStr = alSearchResult.toArray(new String[0]);
Sign up to request clarification or add additional context in comments.

3 Comments

this lsSearchResult.setAdapter(adapter) in doInbackground should be moved to onpostExecute()
No use blackbelt, getting java.lang.ArrayStoreException: source[0] of type com.example.realestate.AddDetails cannot be stored in destination array of type java.lang.String[]
In some example I saw that, so just tried to convert into String array. Now tried to assign arraylist to list ArrayAdapter<AddDetails> adapter = new ArrayAdapter<AddDetails>(SearchActivity.this, android.R.layout.simple_list_item_1,alSearchResult); lsSearchResult.setAdapter(adapter); ********** It is displaying com.example.realestate.AddDetails@40e253d8 in list view
1

Please do not access your listView lsSearchResult in background thread. Pass your result to onPostExecute().

Try to use following Code :

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); 
}

2 Comments

The value is coming but not able to display
are you getting some type of error or it is just not showing anything ?
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);

this is helpful for arraylist to array conversion also you can use arraylist directly with the arrayadapter no need to convert it to array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.