listview没有使用notifydataSetChanged进行更新

时间:2015-04-01 17:32:15

标签: android listview notifydatasetchanged

我的列表视图没有使用在前20个项目之后检索的数据进行更新。

我多次检查过数据是否未通过json传递,但是,只有listview没有使用添加的数据自行更新。

这是我的代码部分:

@Override
public void onGetData(JSONObject obj) {
    Serie[] seriesList;
    if (obj != null) {
        if (pageNumber == 1) {
            series = Serie.serieListFromJSON(this, JsonHandling
                    .getInstance().getJSONArraySerieFromUrlTMDB(obj));
            seriesList = series.toArray(new Serie[series.size()]);
            adapter = new SearchCustomAdapter(this, seriesList);
            lvSearch = (ListView) findViewById(R.id.lvSearch);
            lvSearch.setAdapter(adapter);
            lvSearch.setOnScrollListener(this);
        } else { 
            try {
                Toast.makeText(this, "obj is ?"+obj.toString(1), Toast.LENGTH_LONG).show();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //adapter.clear(); getting an unsupportedmethod exeption
            series.addAll(Serie.serieListFromJSON(this, JsonHandling
                    .getInstance().getJSONArraySerieFromUrlTMDB(obj)));
            seriesList = series.toArray(new Serie[series.size()]);
            adapter.notifyDataSetChanged();
        }
    }
    if (obj == null) {
        Toast.makeText(this, "obj is null", Toast.LENGTH_LONG).show();
        Serie serie = new Serie("not found", "");
        seriesList = new Serie[series.size()];
        seriesList[0] = serie;
        adapter = new SearchCustomAdapter(this, seriesList);
        lvSearch = (ListView) findViewById(R.id.lvSearch);
        lvSearch.setAdapter(adapter);
    }
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
        this.currentFirstVisibleItem = firstVisibleItem;
        this.currentVisibleItemCount = visibleItemCount;
        this.currentTotalItemcount=totalItemCount;  
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    this.currentScrollState = scrollState;

    if(( currentFirstVisibleItem+currentVisibleItemCount) == currentTotalItemcount){
        this.isScrollCompleted();
        adapter.notifyDataSetChanged();
    }

 }

private void isScrollCompleted() {
    if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) {
        pageNumber++;
        JsonHandling.getInstance().getJSONFromUrl(tvTitel.getText().toString(), tvYear.getText().toString(), iof, pageNumber);
        adapter.notifyDataSetChanged();
    }

}

所以有人可以告诉我我做错了什么或错过了吗?

编辑: 在onscrollStateChange中添加了代码调整。仍然不确定为什么它应该存在而不是获取数据。

这里也是按要求提供的searchcustumAdapter:

public class SearchCustomAdapter extends ArrayAdapter<Serie> {

private final Context context;
private final Serie[] values;

public SearchCustomAdapter(Context context, Serie[] values) {
    super(context, R.layout.search_row_layout, values);
    this.context = context;
    this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView==null)
    {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.search_row_layout,
            parent, false);

    TextView textViewTitle = (TextView) rowView
            .findViewById(R.id.SearchRowTitle);
    TextView textViewYear = (TextView) rowView
            .findViewById(R.id.SearchRowYear);
    textViewTitle.setText(values[position].getTitle());
    textViewYear.setText(values[position].getYear());


    return rowView;  
    }
    else
    {
        TextView textViewTitle = (TextView) convertView
                .findViewById(R.id.SearchRowTitle);
        TextView textViewYear = (TextView) convertView
                .findViewById(R.id.SearchRowYear);

        textViewTitle.setText(values[position].getTitle());
        textViewYear.setText(values[position].getYear());

        return convertView;
    }
}

}

更新2: 更改了onScrollCompleted上的代码以获得这部分代码:

            pageNumber++;
        JsonHandling.getInstance().getJSONFromUrl(tvTitel.getText().toString(), tvYear.getText().toString(), iof, pageNumber);
        series.addAll(Serie.serieListFromJSON(this, JsonHandling.getInstance().getJSONArraySerieFromUrlTMDB(obj)));                 
        seriesList = series.toArray(new Serie[series.size()]);        
        lvSearch.setAdapter(new SearchCustomAdapter(this, seriesList));

这确实有效,但这不应该是......对吗?

2 个答案:

答案 0 :(得分:0)

onScrollStateChanged method you are notifying list view but not changing its data

所以试着打电话

onGetData(JSONObject obj) in onscroll it may help you.

答案 1 :(得分:0)

我不了解JSon,所以你可能需要调整下面的代码。在方法isScrollCompleted上,需要添加类似于的代码:

series.addAll(JsonHandling.getInstance().getJSONFromUrl(tvTitel.getText().toString(), tvYear.getText().toString(), iof, pageNumber));                 
seriesList = series.toArray(new Serie[series.size()]);
adapter.notifyDataSetChanged();

注意:Array对象seriesList需要更高级别的范围,而不仅仅是onGetData()