更新列表视图后,列表元素未显示

时间:2014-11-18 05:31:45

标签: android listview android-listview

我有一个列表碎片显示一些项目,如下面的


enter image description here


现在稍后,在某些事件中,我只想用新项目更新此列表,现在如果新列表中的新项目数小于列表项的旧数量,那么它会显示我的列表


enter image description here


问题是:空列表元素。更新我的列表的代码是

         myListView.invalidate();
         CustomAdapter adapter= new CustomAdapter(getApplicationContext(), rowItems);
         notificationListView.setAdapter(adapter);
         adapter.notifyDataSetChanged();

         //even tried the above piece of code in the runOnUiThread()

适配器类

    import java.util.List;

import com.emaven.pcmhapp.R;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

    public class CustomAdapter extends BaseAdapter {

        Context context;
        List<RowItem> rowItem;

        CustomAdapter(Context context, List<RowItem> rowItem) {
            this.context = context;
            this.rowItem = rowItem;


        }

        @Override
        public int getCount() {

            return rowItem.size();
        }

        @Override
        public Object getItem(int position) {

            return rowItem.get(position);
        }

        @Override
        public long getItemId(int position) {

            return rowItem.indexOf(getItem(position));
        }

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

            if (convertView == null) {
                LayoutInflater mInflater = (LayoutInflater) context
                        .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                convertView = mInflater.inflate(R.layout.list_item, null);
            }

           // ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
            TextView txtTitle = (TextView) convertView.findViewById(R.id.title);

            RowItem row_pos = rowItem.get(position);
            // setting the image resource and title
       //     imgIcon.setImageResource(row_pos.getIcon());
            txtTitle.setText(row_pos.getTitle());

            return convertView;

        }

    }

2 个答案:

答案 0 :(得分:0)

使用Holder类尝试使用以下代码:

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;

            if (convertView == null) {
                holder = new ViewHolder();
                LayoutInflater mInflater = (LayoutInflater) context
                        .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                convertView = mInflater.inflate(R.layout.list_item, null);
                holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }
            RowItem row_pos = rowItem.get(position);

            holder.txtTitle.setText(row_pos.getTitle());

            return convertView;

        }

class ViewHolder {
            TextView txtTitle;
        }

答案 1 :(得分:0)

定义在ListView中添加的适配器的自定义方法:

添加多个项目:

public void addItem(List<RowItem> items){
   rowItem = new ArrayList<RowItem>(items);
   notifyDataSetChanged();
}

注意:根据下面的问题,使用ViewHolder设计模式进行ListView性能。