Android的 - 移植ListView与JSONArray数据 [英] android - populating ListView with data from JSONArray

查看:126
本文介绍了Android的 - 移植ListView与JSONArray数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:我是从服务器获取一个的JSONObject 包含用户的播放列表。我想显示在的ListView 这个数据,也有我的测试情况下,288的音频文件。该JSON是正确分析,大小等于288.我创建了一个帮助类,并与它的对象参数化的的ArrayList 。问题:有是正好288项在我的的ListView 但正确的条目艺术家 - 标题上去指数13(不知道为什么),然后他们得到的重复的。所以,我没有在列表中288不同的条目,但我只有14如此反复,直到列表的末尾项。

更新:我删除if语句,它解决了这个问题,但RAM的消费却增加了3MB。优化有什么建议?

 公共类AudioList扩展ListActivity {私人的ListView LV;
私人的JSONObject usersPlaylist,singleJSONItem;
私人JSONArray responseJSONArray;
私人共享preferences preFS;
私人PlaylistItem audioList;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_audio_list);
    在里面();    ArrayList的< PlaylistItem>播放列表=新的ArrayList< PlaylistItem>();
    尝试{
        usersPlaylist = Utils.retrieveJsonObjectFromUrl(新URL(
                APP_CONSTANTS.REQUEST_AUDIO_LIST(preFS)),这一点);
        responseJSONArray = usersPlaylist.getJSONArray(回应);        的for(int i = 0; I< responseJSONArray.length();我++){
            singleJSONItem = responseJSONArray.getJSONObject(ⅰ);
            audioList =新PlaylistItem(singleJSONItem);
            playlist.add(audioList);
        }        Toast.makeText(getApplicationContext(),
                Integer.toString(playlist.size()),Toast.LENGTH_LONG)
                。显示(); //有288项,JSONArray正确解析    }赶上(MalformedURLException的E){        e.printStackTrace();
    }赶上(JSONException E){        e.printStackTrace();
    }赶上(IOException异常五){        e.printStackTrace();
    }    AudioListAdapter适配器=新AudioListAdapter(这一点,
            R.layout.playlist_item,播放列表);
    lv.setAdapter(适配器);}私人无效的init(){
    LV = getListView();
    lv.setTranscriptMode(00000000);
    lv.setDividerHeight(1);
    lv.setSmoothScrollbarEnabled(真);
    preFS = preferenceManager.getDefaultShared preferences(本);}

该PlayListItem类:

 公共类PlaylistItem {私人字符串艺术家,标题;
私人的JSONObject OBJ;公共PlaylistItem(OBJ的JSONObject){
    this.obj = OBJ;
}
公共PlaylistItem(){}公共字符串getArtist(){
    尝试{
        艺术家= obj.getString(艺术家);
    }赶上(JSONException E){        e.printStackTrace();
    }
    返回的艺术家;}公共字符串的getTitle(){
    尝试{
        标题= obj.getString(标题);
    }赶上(JSONException E){        e.printStackTrace();
    }
    返回称号;
}
    }

适配器:

 公共类AudioListAdapter扩展ArrayAdapter< PlaylistItem> {私人上下文的背景下;
私人诠释layoutResourceId;
私人PlaylistItem澳元;
私人的ArrayList< PlaylistItem>数据= NULL;公共AudioListAdapter(上下文的背景下,INT layoutResourceId,
        ArrayList的< PlaylistItem>数据){
    超级(上下文,layoutResourceId,数据);
    this.layoutResourceId = layoutResourceId;
    this.context =背景;
    this.data =数据;}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    查看排= convertView;
    ViewHolder持有人=新ViewHolder();
    AUD = data.get(位置);
            //它给一个正确的位置上,如果我硬code中的指数,像data.get(99);
    如果(行== NULL){        LayoutInflater充气=((活动)上下文).getLayoutInflater();
        行= inflater.inflate(layoutResourceId,父母,假);
        holder.play =(按钮)row.findViewById(R.id.btn_list_play);
        holder.imgSaved =(ImageView的)行
                .findViewById(R.id.img_list_audio_saved);
        holder.tvArtist =(TextView中)一行
                .findViewById(R.id.tvListItemArtist);
        holder.tvTitle =(TextView中)row.findViewById(R.id.tvListItemSong);        holder.tvArtist.setText(aud.getArtist());
        holder.tvTitle.setText(aud.getTitle());    }    返回行;
}静态类ViewHolder {
    按钮播放;
    ImageView的imgSaved;
    TextView的tvArtist,tvTitle;}    }


解决方案

我已经在你的getView method.Replace做了一些修改,并检查它。

  @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup){
            查看排= convertView;
            ViewHolder持有人;
            AUD = data.get(位置);
                    //它给一个正确的位置上,如果我硬code中的指数,像data.get(99);
            如果(行== NULL){
                持有人=新ViewHolder();
                LayoutInflater充气=((活动)上下文).getLayoutInflater();
                行= inflater.inflate(layoutResourceId,父母,假);
                holder.play =(按钮)row.findViewById(R.id.btn_list_play);
                holder.imgSaved =(ImageView的)row.findViewById(R.id.img_list_audio_saved);
                holder.tvArtist =(TextView中)一行.findViewById(R.id.tvListItemArtist);
                holder.tvTitle =(TextView中)row.findViewById(R.id.tvListItemSong);
                row.setTag(保持器);
        }
    其他
    {
                    支架=(ViewHolder)row.getTag();    }
                holder.tvArtist.setText(aud.getArtist());
                holder.tvTitle.setText(aud.getTitle());            返回行;
        }

Situation: I'm getting a JSONObject containing user's playlist from the server. I want to display this data in a ListView, there are 288 audio files in my test case. The JSON is parsed correctly, the size equals to 288. I created a help class and parametrized the ArrayList with its Objects. The problem: there're exactly 288 items in my ListView BUT the correct entries Artist - Title go up to index 13 (don't know why) and then they get repeated. So I don't have 288 different entries in the List, but I have only 14 entries which are repeated until the end of the List.

UPDATE: I removed the if statement and it solved the issue but RAM consumption has increased by 3MB. Any suggestions for optimizing?

   public class AudioList extends ListActivity {

private ListView lv;
private JSONObject usersPlaylist, singleJSONItem;
private JSONArray responseJSONArray;
private SharedPreferences prefs;
private PlaylistItem audioList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_audio_list);
    init();

    ArrayList<PlaylistItem> playlist = new ArrayList<PlaylistItem>();
    try {
        usersPlaylist = Utils.retrieveJsonObjectFromUrl(new URL(
                APP_CONSTANTS.REQUEST_AUDIO_LIST(prefs)), this);
        responseJSONArray = usersPlaylist.getJSONArray("response");

        for (int i = 0; i < responseJSONArray.length(); i++) {
            singleJSONItem = responseJSONArray.getJSONObject(i);
            audioList = new PlaylistItem(singleJSONItem);
            playlist.add(audioList); 
        }

        Toast.makeText(getApplicationContext(),
                Integer.toString(playlist.size()), Toast.LENGTH_LONG)
                .show(); //there are 288 entries, JSONArray parsed correctly

    } catch (MalformedURLException e) {

        e.printStackTrace();
    } catch (JSONException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }

    AudioListAdapter adapter = new AudioListAdapter(this,
            R.layout.playlist_item, playlist);
    lv.setAdapter(adapter);

}

private void init() {
    lv = getListView();
    lv.setTranscriptMode(0x00000000);
    lv.setDividerHeight(1);
    lv.setSmoothScrollbarEnabled(true);
    prefs = PreferenceManager.getDefaultSharedPreferences(this);

}

The PlayListItem class:

     public class PlaylistItem {

private String artist, title;
private JSONObject obj;

public PlaylistItem(JSONObject obj) {
    this.obj = obj;
}
public PlaylistItem(){

}

public String getArtist() {
    try {
        artist = obj.getString("artist");
    } catch (JSONException e) {

        e.printStackTrace();
    }
    return artist;

}

public String getTitle() {
    try {
        title = obj.getString("title");
    } catch (JSONException e) {

        e.printStackTrace();
    }
    return title;
}
    }

The adapter:

     public class AudioListAdapter extends ArrayAdapter<PlaylistItem> {

private Context context;
private int layoutResourceId;
private PlaylistItem aud;
private ArrayList<PlaylistItem> data = null;

public AudioListAdapter(Context context, int layoutResourceId,
        ArrayList<PlaylistItem> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ViewHolder holder = new ViewHolder();
    aud = data.get(position);
            //it's giving a CORRECT position if I hardcode the index, like data.get(99);
    if (row == null) {

        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder.play = (Button) row.findViewById(R.id.btn_list_play);
        holder.imgSaved = (ImageView) row
                .findViewById(R.id.img_list_audio_saved);
        holder.tvArtist = (TextView) row
                .findViewById(R.id.tvListItemArtist);
        holder.tvTitle = (TextView) row.findViewById(R.id.tvListItemSong);

        holder.tvArtist.setText(aud.getArtist());
        holder.tvTitle.setText(aud.getTitle());

    }

    return row;
}

static class ViewHolder {
    Button play;
    ImageView imgSaved;
    TextView tvArtist, tvTitle;

}

    }

解决方案

I have done some modifications in your getView method.Replace it and check it.

  @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            ViewHolder holder;
            aud = data.get(position);
                    //it's giving a CORRECT position if I hardcode the index, like data.get(99);
            if (row == null) {
                holder = new ViewHolder();
                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                row = inflater.inflate(layoutResourceId, parent, false);
                holder.play = (Button) row.findViewById(R.id.btn_list_play);
                holder.imgSaved = (ImageView) row.findViewById(R.id.img_list_audio_saved);
                holder.tvArtist = (TextView) row .findViewById(R.id.tvListItemArtist);
                holder.tvTitle = (TextView) row.findViewById(R.id.tvListItemSong);
                row.setTag(holder);
        }
    else
    {
                    holder = (ViewHolder ) row.getTag();

    }
                holder.tvArtist.setText(aud.getArtist());
                holder.tvTitle.setText(aud.getTitle());



            return row;
        }

这篇关于Android的 - 移植ListView与JSONArray数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆