alexis.araya
alexis.araya

Reputation: 115

ListFragment error

I have a list in a "listview" to scroll the list using this mess, grouping and the list has a header with icons.

public class MyCustomAdapter extends BaseAdapter {

private static final String ASSETS_DIR = "images/";
private static final int TYPE_HEAD = -1;
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;


private Context ctx;

private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;

private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

public MyCustomAdapter(Context context) {
    this.ctx = context;
    mInflater = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public void addItem(final String item) {
    mData.add(item);
    notifyDataSetChanged();
}

public void addSeparatorItem(final String item) {
    mData.add(item);
    mSeparatorsSet.add(mData.size() - 1);
    notifyDataSetChanged();
}

public void addHeadItem(){
    mData.add("");
    mSeparatorsSet.add(0);
    notifyDataSetChanged();
}

@Override
public int getCount() {

    return mData.size();
    //return equipos.size();
}

@Override
public String getItem(int position) {

    return mData.get(position) ;
}

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

@Override
public int getItemViewType(int position) {
    if (position==0)
        return TYPE_HEAD;

    return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}

@Override
public int getViewTypeCount() {
    return TYPE_MAX_COUNT;
}

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

    ViewHolder holder = null;
    int type = getItemViewType(position);
    System.out.println("getView " + position + " " + convertView
            + " type = " + type);
    if (convertView == null) {
        holder = new ViewHolder();
        switch (type) {
        case TYPE_ITEM:
            convertView = mInflater.inflate(R.layout.list_item, null);

            holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
            holder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
            holder.textView3 = (TextView) convertView.findViewById(R.id.textView3);
            holder.imageView1 = (ImageView) convertView.findViewById(R.id.imageView1);

            String[] datos = mData.get(position).split("-");

            holder.textView1.setText(String.format(" %s - %s", datos[0],datos[1]));
            holder.textView2.setText(datos[2]);
            holder.textView3.setText(datos[3]);

            String sel_bandera =  datos[4].trim() ;

            String imgFilePath = "";

            if (sel_bandera.equals("verde")){
                 imgFilePath = ASSETS_DIR + "circle_green.png" ;
            }else if (sel_bandera.equals("amarilla")){
                 imgFilePath = ASSETS_DIR + "circle_yellow.png";
            }else {
                 imgFilePath = ASSETS_DIR + "circle_red.png";
            }
            try {
                Bitmap bitmap = BitmapFactory.decodeStream(this.ctx.getResources().getAssets().open(imgFilePath));
                holder.imageView1.setImageBitmap(bitmap);
                //bandera.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }


            break;
        case TYPE_SEPARATOR:
            convertView = mInflater.inflate(R.layout.list_group, null);
            holder.textView1 = (TextView) convertView.findViewById(R.id.textSeparator);
            holder.textView1.setText(mData.get(position));
            break;

        case TYPE_HEAD:
            convertView = mInflater.inflate(R.layout.list_head, null);

            break;
        }
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    return convertView;

}


public static class ViewHolder {

    public TextView textView1;
    public TextView textView2;
    public TextView textView3;
    public ImageView imageView1;

    public TextView getTextView1() {
        return textView1;
    }
    public void setTextView1(TextView textView1) {
        this.textView1 = textView1;
    }
    public TextView getTextView2() {
        return textView2;
    }
    public void setTextView2(TextView textView2) {
        this.textView2 = textView2;
    }
    public TextView getTextView3() {
        return textView3;
    }
    public void setTextView3(TextView textView3) {
        this.textView3 = textView3;
    }
    public ImageView getImageView1() {
        return imageView1;
    }
    public void setImageView1(ImageView imageView1) {
        this.imageView1 = imageView1;
    }

}

}

public class EquiposActivity extends ListFragment implements OnTouchListener {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mAdapter = new MyCustomAdapter(getActivity());

            if (lista.length > 0) {
                String[] datos = lista[0].split("-");
                cabecera_grupo = datos[4];
            }

            mAdapter.addHeadItem();

            for (int i = 0; i < lista.length; i++) {

                String[] datos = lista[i].split("-");
                String grupo = datos[4];


                if (i == 0) {
                    mAdapter.addSeparatorItem(grupo.replace("_", " "));

                }

                if (!grupo.equals(cabecera_grupo)) {

                    mAdapter.addSeparatorItem(grupo.replace("_", " "));
                    cabecera_grupo = grupo;
                }

                mAdapter.addItem(String.format("%s - %s - %s - %s - %s",
                        datos[0], datos[1], datos[2], datos[3], datos[5]));

            }

            setListAdapter(mAdapter);

return super.onCreateView(inflater, container, savedInstanceState); }

Upvotes: 0

Views: 219

Answers (1)

Michał Z.
Michał Z.

Reputation: 4119

I suggest you to rewrite your getView() method, because I think that you are wrongly using ViewHolder pattern. Read this: http://www.jmanzano.es/blog/?p=166 Or just get rid of ViewHolder and code getView() without it.

Upvotes: 0

Related Questions