ListView自定义适配器getView没有调用

时间:2015-03-05 11:23:43

标签: android android-listview

  1. 以下是我的代码,我在这里得到回复..

       Fetching Data from server in josn
    
    
           getRequest();
                       list = (ListView) findViewById(R.id.list);
                       list.setAdapter(adapter);
                       list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                           @Override
                           public void onItemClick(AdapterView<?> parent, View view,
                                                   int position, long id) {
    
                           }
                       });
    
       //
    
               private void getRequest(){
                       Thread trd = new Thread(new Runnable() {
                           @Override
                           public void run() {
                               DefaultHttpClient httpClient = new DefaultHttpClient();
                               HttpEntity httpEntity = null;
                               HttpResponse httpResponse = null;
    
       //
    
    
                               try{
                                   Log.d("Get top called","try block");
                                   HttpGet httpGet = new HttpGet(url);
                                   httpResponse = httpClient.execute(httpGet);
                                   httpEntity = httpResponse.getEntity();
                                   response = EntityUtils.toString(httpEntity);
                                   if(response!=null){
                                       try{
                                           HashMap<String, String> defaultSongsDetails;
                                           songs = new JSONArray(response);
                                           JSONObject jsonObject;
                                           for(int i=0;i<songs.length();i++){
    
                                               defaultSongsDetails = new HashMap<String, String>();
    
                                               String title,artist,language,imageUrl,songUrl,vocalUrl,duration=null;
                                               String sdCardPathOfImage,sdCardPathOfOriginalSong,sdCardPathOfVocalSong=null;
    
                                               jsonObject = songs.getJSONObject(i);
    
                                               title = jsonObject.getString(SONG_TITLE);
                                               defaultSongsDetails.put("song_title",title);
    
                                               artist = jsonObject.getString(SONG_ARTIST);
                                               defaultSongsDetails.put("song_artist",artist);
    
    
    
    
                                               language = jsonObject.getString(SONG_LANGUAGE);
                                               defaultSongsDetails.put("song_language",language);
    
                                               duration = jsonObject.getString(SONG_DURATION);
                                               defaultSongsDetails.put("song_duration",duration);
    
                                               imageUrl = jsonObject.getString(SONG_THUMBNAL);
                                               songUrl = jsonObject.getString(SONG_DOWNLOAD_URL);
                                               vocalUrl = jsonObject.getString(SONG_VOCAL_URL);
    
                                               String thumbnail=null;
                                               int cutImg = imageUrl.lastIndexOf('/');
                                               if (cutImg != -1) {
                                                   thumbnail = imageUrl.substring(cutImg + 1);
                                               }
                                               sdCardPathOfImage = basepath + "/songs/" + thumbnail;
                                               defaultSongsDetails.put("song_thumbnail", sdCardPathOfImage);
    String fileName=null;    int cut = songUrl.lastIndexOf('/');    if (cut != -1) {    fileName = songUrl.substring(cut + 1);    }   
    

    sdCardPathOfOriginalSong = basepath +&#34; / songs /&#34; + fileName;                                                defaultSongsDetails.put(&#34; song_original_path&#34 ;,
    sdCardPathOfOriginalSong);     String vocalFileName = null; int cutVocal = songUrl.lastIndexOf(&#39; /&#39;); if(cut!= -1){vocalFileName = songUrl.substring(cutVocal + 1); } sdCardPathOfVocalSong = 基本路径+&#34; /歌曲/&#34; + vocalFileName;                                                defaultSongsDetails.put(&#34; song_vocal_path&#34;,sdCardPathOfVocalSong);
    //将Hashmap添加到ArrayList                                                defaultSongsDetailList.add(defaultSongsDetails);                                            adapter = new CustomList(JsonActivity.this,defaultSongsDetailList);                    } catch(Exception e){e.printStackTrace(); catch(Exception e){e.printStackTrace(); } }); trd.start();                    }

       //CustomList Adapter class
    
    
               public class CustomList extends ArrayAdapter<HashMap<String, String>>{
                   private final Activity context;
    
                   private ArrayList<HashMap<String, String>> songList;
                   public CustomList(Activity context,
                                          ArrayList<HashMap<String, String>> songList) {
                       super(context, R.layout.list_item_view, songList);
                       this.context = context;
                       this.songList = songList;
                   }
                   @Override
                   public View getView(int position, View view, ViewGroup parent) {
                       LayoutInflater inflater = context.getLayoutInflater();
                       View rowView= inflater.inflate(R.layout.list_item_view, null, true);
                       TextView txtTitle = (TextView) rowView.findViewById(R.id.title);
                       ImageView imageView = (ImageView) rowView.findViewById(R.id.list_image);
                       TextView txtArtist = (TextView) rowView.findViewById(R.id.artist);
                       TextView txtDuration = (TextView) rowView.findViewById(R.id.duration);
    
                       System.out.println(" title ** "+songList.get(position).get("song_title")+" duration **   
    

    &#34 + songList.get(位置)获得(&#34; song_duration&#34)                        +&#34; thumb **&#34; + songList.get(position).get(&#34; song_thumbnail&#34;)+&#34;语言** &#34 + songList.get(位置)获得(&#34; song_language&#34));

                       txtArtist.setText(songList.get(position).get("song_title"));
                       txtDuration.setText(songList.get(position).get("song_duration"));
    
                       File file = new File(songList.get(position).get("song_thumbnail"));
                       Uri uri = Uri.fromFile(file);
                  imageView.setImageBitmap(BitmapFactory.decodeFile(defaultFilePath));
                           imageView.setImageURI(uri);
                       txtTitle.setText(songList.get(position).get("song_language"));
    
                       return rowView;
                   }
               }
    
    
       Any help would be appreciated...
    

1 个答案:

答案 0 :(得分:0)

 //Fetching Data from server in josn
        getRequest();

        gAdapter = new BaseAdapter(HomeJsonActivity.this, defaultSongsDetailList);;
        list = (ListView) findViewById(R.id.list);
        gAdapter.notifyDataSetChanged();
        list.setAdapter(gAdapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
            }
        });

private void getRequest() {
        Thread trd = new Thread(new Runnable() {
            @Override
            public void run() {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpEntity httpEntity = null;
                HttpResponse httpResponse = null;

                try {
                    Log.d("Get top called", "try block");
                    HttpGet httpGet = new HttpGet(url);
                    httpResponse = httpClient.execute(httpGet);
                    httpEntity = httpResponse.getEntity();
                    response = EntityUtils.toString(httpEntity);
                    if (response != null) {
                        try {
                            HashMap<String, String> defaultSongsDetails;
                            songs = new JSONArray(response);
                            JSONObject jsonObject;
                                defaultSongsDetails = new HashMap<String, String>();

                                String title, artist, language, imageUrl, songUrl, vocalUrl, duration = null;
                                String sdCardPathOfImage, sdCardPathOfOriginalSong, sdCardPathOfVocalSong = null;

                                jsonObject = songs.getJSONObject(i);

                                title = jsonObject.getString(SONG_TITLE);
                                defaultSongsDetails.put("song_title", title);

                                artist = jsonObject.getString(SONG_ARTIST);
                                defaultSongsDetails.put("song_artist", artist);

                                language = jsonObject.getString(SONG_LANGUAGE);
                                defaultSongsDetails.put("song_language", language);

                                duration = jsonObject.getString(SONG_DURATION);
                                defaultSongsDetails.put("song_duration", duration);

                                imageUrl = jsonObject.getString(SONG_THUMBNAL);
                                songUrl = jsonObject.getString(SONG_DOWNLOAD_URL);
                                vocalUrl = jsonObject.getString(SONG_VOCAL_URL);

                                String thumbnail = null;
                                int cutImg = imageUrl.lastIndexOf('/');
                                if (cutImg != -1) {
                                    thumbnail = imageUrl.substring(cutImg + 1);
                                }
                                sdCardPathOfImage = basepath + "/songs/" + thumbnail;
                                defaultSongsDetails.put("song_thumbnail", sdCardPathOfImage);
                                String fileName = null;
                                int cut = songUrl.lastIndexOf('/');
                                if (cut != -1) {
                                    fileName = songUrl.substring(cut + 1);
                                }
                                sdCardPathOfOriginalSong = basepath + "/songs/" + fileName;
                                defaultSongsDetails.put("song_original_path", sdCardPathOfOriginalSong);

                                String vocalFileName = null;
                                int cutVocal = songUrl.lastIndexOf('/');
                                if (cut != -1) {
                                    vocalFileName = songUrl.substring(cutVocal + 1);
                                }
                                sdCardPathOfVocalSong = basepath + "/songs/" + vocalFileName;
                                defaultSongsDetails.put("song_vocal_path", sdCardPathOfVocalSong);

                                defaultSongsDetailList.add(defaultSongsDetails);
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        trd.start();

    }

//Custom Base Adapter Class

public class BaseAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private ArrayList<HashMap<String, String>> songList;

    public BaseAdapter(Context context, ArrayList<HashMap<String, String>> list) {
        this.mInflater = LayoutInflater.from(context);
        this.songList = list;
        for (HashMap hh : songList) {
            System.out.println("**** hh ***" + hh);
        }
    }

    @Override
    public int getCount() {
        return songList.size();
    }

    @Override
    public Object getItem(int position) {
        return songList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView;
        ViewHolder holder;
        if (convertView == null) {
            rowView = mInflater.inflate(R.layout.list_item_view, parent, false);
            holder = new ViewHolder();
            holder.title = (TextView) rowView.findViewById(R.id.title);
            holder.thumb = (ImageView) rowView.findViewById(R.id.list_image);
            holder.artist = (TextView) rowView.findViewById(R.id.artist);
            holder.duration = (TextView) rowView.findViewById(R.id.duration);

            rowView.setTag(holder);
        } else {
            rowView = convertView;
            holder = (ViewHolder) rowView.getTag();
        }
        holder.title.setText(songList.get(position).get("song_title"));
        File file = new File(songList.get(position).get("song_thumbnail"));
        Uri uri = Uri.fromFile(file);
        holder.thumb.setImageURI(uri);
        holder.artist.setText(songList.get(position).get("song_artist"));
        holder.duration.setText(songList.get(position).get("song_duration"));

        System.out.println(" title ** " + songList.get(position).get("song_title") + " duration ** " + songList.get(position).get("song_duration")
                + " thumb ** " + songList.get(position).get("song_thumbnail") + " language ** " + songList.get(position).get("song_language"));

        return rowView;
    }

    private class ViewHolder {
        public ImageView thumb;
        public TextView title, duration, artist;
    }
}