Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8715541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:52:15+00:00 2026-06-13T05:52:15+00:00

I’m new to android. Started learning recently. I have created a LinearLayout dynamically. I

  • 0

I’m new to android. Started learning recently.

I have created a LinearLayout dynamically. I have the following fields in LinearLayout:
1. Customer name
2. Edit Button
3. Delete Button.

Orientation is horizontal.

Now I want to delete a single record when I click on delete button. How can I get row id in this case and later I can pass to database to delete the record. Here is the code.

user_list_view_item_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="horizontal"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:background="@drawable/bg_main"
   >


    <TextView android:id="@+id/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:textStyle="bold"
        android:textSize="50dp"
        android:textColor="#FFFFFF"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />


        <Button
            android:id="@+id/userListEditButton"
            style="@style/SpecialText"
            android:layout_width="50dp"
            android:layout_height="50dp"  
            android:typeface="monospace"          
            android:text="@string/save" 
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp" />

        <Button
            android:id="@+id/userListDeleteButton"
            style="@style/SpecialText"
            android:layout_width="50dp"
            android:layout_height="50dp"  
            android:typeface="monospace"          
            android:text="@string/reset" 
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp" />
    </LinearLayout>


UserAdapter.java

package com.rad.mobileshop.activities;

import java.sql.SQLException;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.rad.mobileshop.common.entities.User;
import com.rad.mobileshop.db.DBAdapter;

public class UserAdapter extends ArrayAdapter<User> {

    Context context;
    int layoutResourceId;
    User data[] = null;
    private DBAdapter mdb;

    public UserAdapter(Context context, int layoutResourceId, User[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
        mdb = new DBAdapter(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        UserHolder holder = null;

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

            holder = new UserHolder();
            holder.txtTitle = (TextView) row.findViewById(R.id.txtTitle);
            holder.editButton = (Button) row
                    .findViewById(R.id.userListEditButton);
            holder.deleteButton = (Button) row
                    .findViewById(R.id.userListDeleteButton);

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

        User user = data[position];     
        holder.txtTitle.setText(user.getName());
        holder.editButton.setText("Edit");
        holder.deleteButton.setText("Delete");

        holder.editButton.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                System.out.println("hello Edit");
            }
        });

        holder.deleteButton.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                System.out.println("hello delete ");
            }
        });     

        return row;
    }

    private void deleteUserDetails(int userId) {
        try {

            mdb.open();
            mdb.deleteUserDetails(userId);

        } catch (SQLException e1) {
            e1.printStackTrace();
        }
    }

    static class UserHolder {
        TextView txtTitle;
        Button editButton;
        Button deleteButton;
    }
}

User.java

package com.rad.mobileshop.common.entities;

public class User {
    private int id;
    private String name;
    private String type;

    public User() {
        super();
    }

    public User(int id, String name, String type) {
        super();
        this.id = id;
        this.name = name;
        this.type = type;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return this.name ;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:52:16+00:00Added an answer on June 13, 2026 at 5:52 am

    First you need to change your User[] array into an ArrayList, because you cannot dynamically remove an element for a primitive array. (At least not as easily and ArrayAdapter is already designed to work with ArrayLists in this circumstance.)

    Next let’s save position in deleteButton in getView() for easy access:

    holder.deleteButton.setTag(position);
    

    Last add this to your delete OnClickListener:

    public void onClick(View view) {
        User user = getItem((Integer) view.getTag()));
        remove(user);
        deleteUserDetails(user.getId());
    }
    

    There are an couple minor adjustments that I didn’t cover, like changing UserAdapter’s constructor and data[position] in getView(), but they are easy fixes.


    Optional advice:

    • You should create only one LayoutInflater in your constructor and simply re-use it in getView()
    • You don’t need to store a copy of data in UserAdapter, it is already stored in ArrayAdapter, simply access it with getItem(position).
    • You can streamline getView() by moving every command that doesn’t change into the if(row == null) block.

    All that said, I’m not sure why you extended ArrayAdapter if you are working with a database. I suggest switching to a custom CursorAdapter, but this isn’t required.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a small JavaScript validation script that validates inputs based on Regex. I
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.