android适配器getView方法不起作用

时间:2014-09-19 15:56:37

标签: android listview android-listview

在android应用程序上工作,我有一个listview,需要适配器...我的问题,当我发送上下文arraylist参数到适配器... getView方法不起作用,

这里是我在主要活动中调用适配器的地方,其中arraylist已满

adapter = new ArticlesAdapter(getApplicationContext(), arraylist);
        content.setAdapter(adapter);

这里是完整的适配器代码

public class ArticlesAdapter extends BaseAdapter{

    private ArrayList<ArticlesBean> arr;
    private LayoutInflater inflater;
    private ImageLoader mImageLoader;

    public ArticlesAdapter(Context context, ArrayList<ArticlesBean> arraylist){
        this.arr = arraylist;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mImageLoader = new ImageLoader(context);



    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return arr.size();

    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return arr.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

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

        Log.e("get view","in get view");

        ViewHolder holder = null;
        if (v == null) {
            v = inflater.inflate(R.layout.customfavlist, null);
            holder = new ViewHolder();


            holder.title = (TextView) v.findViewById(R.id.ltitle);
            holder.ldesc = (TextView) v.findViewById(R.id.ldesc);
            v.setTag(holder);

        } else {
            holder = (ViewHolder) v.getTag();

        }

        holder.title = (TextView) v.findViewById(R.id.ltitle);
        holder.ldesc = (TextView) v.findViewById(R.id.ldesc);

        holder.title.setGravity(Gravity.RIGHT);
        holder.title.setText(arr.get(position).getTitle());
        holder.ldesc.setText(arr.get(position).getBody().substring(0, 45).replace("\n", " "));




        return v;
    }

    static class ViewHolder {

        private TextView title;
        private TextView ldesc;
    }

    public void refresh(ArrayList<ArticlesBean> items) {
        // TODO Auto-generated method stub
        arr.addAll(items);
        notifyDataSetChanged();
        // notifyDataSetChanged();
    }

}

1 个答案:

答案 0 :(得分:0)

变化:

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

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

adapter = new ArticlesAdapter(getApplicationContext(), arraylist);

adapter = new ArticlesAdapter(MainActivity.this, arraylist);