Ahmad Y Yousef
Ahmad Y Yousef

Reputation: 21

How can I convert ArrayList to ListView?

I'm tring to get a array from PHP page and show it in ListView.

I tried to put it in ListAdapter but this problem appears:

01-21 09:13:44.223: E/AndroidRuntime(1974): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.e_music/com.example.e_music.ViewArtists}: java.lang.ClassCastException: java.util.ArrayList cannot be cast to android.widget.ListAdapter

Android Code:

@SuppressLint("NewApi") public class ViewArtists extends ListActivity{
    // url to get all products list
    private static String urlArtists= "http://mwssong.esy.es/android/ViewArtists.php";

    // JSON Node names
    public static final String TAG_SUCCESS = "success";
    public static final String TAG_Artist = "Artists";
    public static final String TAG_id = "id";
    public static final String TAG_FName = "FName";

   ArrayList<Artist> getArtists =new ArrayList<Artist>();  

   class Artist {

       public String id;
       public String FName;
       public String LName;
       public String Gender;
       public String Country;      
   }

   static ArrayList<String> ResultRow;
   @TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewartists);

        String result ="";

        try{
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

            StrictMode.setThreadPolicy(policy); 

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://mwssong.esy.es/android/ViewArtists.php");
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            InputStream is = httpEntity.getContent();

            try {

                BufferedReader reader =new BufferedReader(new InputStreamReader(is,"iso-8859-1" ),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                result=sb.toString();
            }catch (Exception e){
                Log.e("log_tag", "error1 " + e.toString());

            }

        }catch (Exception e){
            Log.e("log_tag","error2 " +  e.toString());

        }

        try{
            JSONArray jArray= new JSONArray(result);
            for(int i=0; i<jArray.length();i++){
                JSONObject json_data =jArray.getJSONObject(i);
                //Create new Artist:
                Artist ArtistRow= new Artist();
                //Get attributes:
                ArtistRow.id=json_data.getString("id");
                ArtistRow.FName=json_data.getString("FName");
                ArtistRow.LName=json_data.getString("LName");
                ArtistRow.Gender=json_data.getString("Gender");
                ArtistRow.Country=json_data.getString("Country");

                getArtists.add(ArtistRow);
            }
        }catch (Exception e){
            Log.e("log_tag","error3 " +  e.toString());

        }

        ArrayAdapter<Artist> adapter = new ArrayAdapter<Artist>(this, android.R.layout.simple_list_item_1, getArtists);
        ListView myListView =(ListView)findViewById(R.id.lvArtist);
        myListView.setAdapter(adapter);
 }
}

Upvotes: 1

Views: 1989

Answers (2)

Jameson
Jameson

Reputation: 6659

As the error says, you can't cast an ArrayList to a ListAdapter.

If you have:

ArrayList<String> arrayList = new ArrayList<String>();

You should create an adapter directly:

private ArrayAdapter arrayAdapter = 
    new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);

Then use it with:

listView.setAdapter(arrayAdapter);

Upvotes: 1

Doron Yakovlev Golani
Doron Yakovlev Golani

Reputation: 5470

You are confusing two things - list data & list adapter. The adapter is responsible for creating views and giving them to the list, while the data is well... just data (in your case Strings).

I suggest you use an ArrayAdapter (http://developer.android.com/reference/android/widget/ArrayAdapter.html). Your code should look like this:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getArtists);
ListView myListView =(ListView)findViewById(R.id.lvArtist);
myListView.setAdapter(adapter);

Upvotes: 0

Related Questions