1

i'm trying to delete custom listview's item on button click , delete function is working correctly, but prblem is when i click on button then item donot delete on the spot, when i reload

@Override
public View getView(final int paramInt, View paramView, ViewGroup paramViewGroup) {
    // TODO Auto-generated method stub

    LayoutInflater inflator = activity.getLayoutInflater();
    if (paramView == null) {
        view = new ViewHolder();
        paramView = inflator.inflate(R.layout.listview_row, null);

        view.header = (TextView) paramView.findViewById(R.id.tvHeader);
        view.from = (TextView) paramView.findViewById(R.id.tvfrom);
        view.to = (TextView) paramView.findViewById(R.id.tvto);
        view.value = (EditText) paramView.findViewById(R.id.etValue);
        view.imgViewFlag = (ImageView) paramView.findViewById(R.id.ibclose);
        view.result = (TextView) paramView.findViewById(R.id.tvResult);

        paramView.setTag(view);

    } else {
        view = (ViewHolder) paramView.getTag();
    }

    view.header.setText(Header.get(paramInt));
    view.from.setText(From.get(paramInt));
    view.to.setText(To.get(paramInt));
    view.value.setText(Value.get(paramInt));
    view.imgViewFlag.setImageResource(close.get(paramInt));
    view.value.setFocusableInTouchMode(false);
    view.value.setFocusable(false);
    view.imgViewFlag.setFocusableInTouchMode(false);
    view.imgViewFlag.setFocusable(false);
    view.imgViewFlag.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int i=paramInt+1;
            File f1 = new File("/data/data/com.example.converter/shared_prefs/"+i+".xml");



            if(f1.exists()){
                f1.delete();
                 Header.remove(paramInt);
                 From.remove(paramInt);
                 close.remove(paramInt);
                 To.remove(paramInt);
                 Value.remove(paramInt);
            }
            else{
                for(int l = i;i<6;){
                    File f2 = new File("/data/data/com.example.converter/shared_prefs/"+l+".xml");
                    if(f2.exists()){
                        f2.delete();
                         Header.remove(paramInt);
                         From.remove(paramInt);
                         close.remove(paramInt);
                         To.remove(paramInt);
                         Value.remove(paramInt);
                        break;
                    }
                    else{
                        l++;
                    }
                }

            }

        }
    });

    return paramView;

please help me ,I'm very confused how i do it, i want to delete that item when i click on button and it does not delete when i click on button that does not delete on that time........

1
  • Did you call notifyDataSetChanged() on adapter, after updating the listview? Commented Feb 7, 2013 at 11:46

1 Answer 1

4

I am posting code for deleting item from listview which is working correctly in my code.

You forgot to call notifysetchanged thats it.

    @Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    View row = null;
    LayoutInflater inflater = getLayoutInflater();

    row = inflater.inflate(R.layout.one_result_details_row, parent, false);

    // inflate other items here : 
    Button deleteButton = (Button) row.findViewById(R.id.Details_Button01);
     deleteButton.setTag(position);

    deleteButton.setOnClickListener(
        new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer index = (Integer) view.getTag();
                items.remove(index.intValue());  
                notifyDataSetChanged();
            }
        }
    );

plz have a look at the following answers

1) Remove ListView items in Android

2) Remove selected item from ListView

Let me know if you are still facing any issue...

Thanks

Sign up to request clarification or add additional context in comments.

7 Comments

brother i want to delete row from custom list adapter
and i want to delete that item on button click , and that button is from custom listview
which code u given that is only for simple adapter , but i need code which will work for custom list adapter
@Rohit for custom list adapter,,see the following code... stackoverflow.com/questions/10311431/…
brother, i donot have any adapter then how i use this example
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.