Harsh
Harsh

Reputation: 487

Delete a list element from a ListView

I have a ListView which is populated using a web service. I want to delete a particular entry from the ListView For that I need to get an ID (a texview field), one of the elements from the ListView and pass it to the database. I have an ImageView (a red cross) for every list element which needs to be clicked in order to delete that entry. All I want is to get the value of the text field when I click on the delete image of the same list element. I used an android:onClick xml attribute to call a function deleteEntry. But I'm not sure how to get a particular value of the ID which is in the same list element as the delete image. How can I do that?

EDIT

This is my adapter:

public class MyCustomAdapterWorkEntry extends ArrayAdapter<ViewWorkEntryBean> {


Context context;
int layoutResourceId;
ViewWorkEntryBean currentMRB;
Vector<ViewWorkEntryBean> data;

public MyCustomAdapterWorkEntry(Context context, int layoutResourceId, Vector<ViewWorkEntryBean> data) 
{
    super(context,layoutResourceId,data);
    this.layoutResourceId = layoutResourceId;
    this.context=context;
    this.data = data;
}

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

    View row = convertView;
    MyStringReaderHolder holder;


    if(row==null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent,false);

        holder= new MyStringReaderHolder();

        holder.workLogID = (TextView)row.findViewById(R.id.worklog_id);

        holder.delete = (ImageView) row.findViewById(R.id.delete);

        row.setTag(holder);
    }
    else
    {
        holder=(MyStringReaderHolder) row.getTag();
    }


    ViewWorkEntryBean mrb = data.elementAt(position);

    holder.workLogID.setText(mrb.workLogID);

    holder.delete.setTag(mrb.workLogID);
    return row;
}

static class MyStringReaderHolder
{
TextView workLogID;
ImageView delete;

}
}

And this is the delete function:

public void DeleteWorkEntryClick(View v) {


    String workLogID = null;        

    workLogID = (String) v.getTag();


    deleteWorkEntry(workLogID);

}

It still doesn't work! workLogID is null.

Upvotes: 0

Views: 780

Answers (3)

gwvatieri
gwvatieri

Reputation: 5173

I assume that you are using an adapter for building your ListView, either standard or custom, it does not really matter.

That said, removing a list item from a ListView is in charge of the adapter, basically your adapter has to have a removeItem method which is gonna remove the item from the list and notify that the data set has changed.

I don't know the structure of your code but it should be something like this:

public class YourAdapter extends BaseAdapter {

...

List<YourDataClass> list;

...

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

...

public void removeItem(YourDataClass instance) {
    list.remove(instance);
    notifyDataSetChanged();
}

...
}

public class YourActivity extends Activity {

...

private ListView listView;
private YourAdapter yourAdapter;

...

private final OnItemClickListener listItemOnClickListener = new OnItemClickListener()     {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            YourDataClass itemToRemove = yourAdapter.getItem(position);
            yourAdapter.removeItem(itemToRemove);
    }
};

...

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    list.setOnItemClickListener(listItemOnClickListener);
    ...
}

...

}

The key is that yourDataClass has to override:

public YourDataClass {

    ...

    @Override
    public boolean equals(Object obj) {...}

    @Override
    public int hashCode() {...}

    ...
}

Without knowing your code I assumed that your structure your code in a certain way, hope it will help!

ps. I built the example using the item click listener, if you want to have a sub view of your list item being clickable and perform the delition then you need to move the logic to the getView method in your adapter.

Upvotes: 1

devunwired
devunwired

Reputation: 63293

Set the specific ID value you need to pass to the function as the tag value of each "X" button in the list using setTag(). That way, when the view clicked is passed to your onClick() method, you can call view.getTag() to obtain that number again and pass it to your DB function.

Upvotes: 1

Sam
Sam

Reputation: 86948

All I want is to get the value of the text field when I click on the delete image of the same list element.

Posting some relevant code would be helpful, but here's a generic approach.

Let's assume that your row layout looks like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

We can set up an OnClickListener for the Button that reads the TextView text like so:

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
    public void onClick(View view) {
        LinearLayout parent = (LinearLayout) view.getParent();
        TextView textView = (TextView) parent.getChildAt(0);
        String text = textView.getText().toString();
        // text has what you want
    }
});

If you want a more future-proof, but labor intensive approach use:

TextView text = (TextView) parent.findViewById(R.id.text);

Upvotes: 1

Related Questions