Skip to content Skip to sidebar Skip to footer

Buttons In Listview Each Item Layout

I have a listview and I am populating that list view from a ArrayList of custom objects. This is the custom adapter code. @Override public View getView(int position, View c

Solution 1:

I think the problem is with your fPOJO variable when onClick is called it may have different value because after every getView you're changing it's value.

@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stubfinalint a=position;


    final FileHolder holder;
    Viewrow= convertView;
    if(row==null)
    {
        LayoutInflaterinflater= ((Activity)context).getLayoutInflater();
        row = inflater.inflate(textViewResourceId, parent, false);
        holder=newFileHolder();

        holder.file=files.get(position);
        holder.deleteButton=(ImageButton) row.findViewById(R.id.deleteButton);
        holder.downloadButton=(ImageButton) row.findViewById(R.id.downloadButton);

        holder.deleteButton.setTag(holder.file);
        holder.downloadButton.setTag(holder.file);


        holder.fileName= (TextView) row.findViewById(R.id.fileName);



        holder.fileName.setText(holder.file.getFileName());

    }
    else
    {
        holder=(FileHolder) row.getTag();
    }

    final AsyncCall call=newAsyncCall();

    holder.downloadButton.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
            // TODO Auto-generated method stub
            MODE=0;
            //call.execute(MODE);

        }
    });

    holder.deleteButton.setOnClickListener(newView.OnClickListener() {

        @OverridepublicvoidonClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getContext(), "Delete File "+(String.valueOf(a))+"  "+holder.fileName, Toast.LENGTH_LONG).show();
            MODE=1;
            //call.execute(MODE);

        }
    });


    return row;
}

Just make holder final and get you're file name from it.

Solution 2:

You store your list item in holder, this is your problem, ListView adapter creates as much convertViews as needed to fill the screen so if you have list of 10+ elements, only few will be added to row.

First of all don't store your list element in holder, you gave adapter method called getItem(positoin) to get it.

In your holder view store only references to layout views (TextView, Buttons etc) and (if(row == null) call findViewById on them and setTag.

Then set text, listeners outside the if statement, like so:

@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stubfinalint a=position;


    FileHolder holder=null;
    Viewrow= convertView;
    if(row==null)
    {
        LayoutInflaterinflater= ((Activity)context).getLayoutInflater();
        row = inflater.inflate(textViewResourceId, parent, false);
        holder=newFileHolder();

        holder.deleteButton=(ImageButton) row.findViewById(R.id.deleteButton);
        holder.downloadButton=(ImageButton) row.findViewById(R.id.downloadButton);

        holder.fileName= (TextView) row.findViewById(R.id.fileName);

        row.setTag(holder);

    }
    else
    {
        holder=(FileHolder) row.getTag();
    }

    fPOJO= getItem(position);

    final AsyncCall call=newAsyncCall();

    holder.downloadButton.setOnClickListener(null);
    holder.downloadButton.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
            // TODO Auto-generated method stub
            MODE=0;
            //call.execute(MODE);

        }
    });

    holder.deleteButton.setOnClickListener(null);
    holder.deleteButton.setOnClickListener(newView.OnClickListener() {

        @OverridepublicvoidonClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getContext(), "Delete File "+(String.valueOf(a))+"  "+fPOJO.getFileName(), Toast.LENGTH_LONG).show();
            MODE=1;
            //call.execute(MODE);

        }
    });


    return row;
}

publicstaticclassFileHolder{
    TextView fileName;
    ImageButton downloadButton;
    ImageButton deleteButton;
}

Also you can do some code optimalization, for example don't initialize LayoutInflater in getView() method, you can do it in adapter Constructor

Post a Comment for "Buttons In Listview Each Item Layout"