logo
down
shadow

remove item onlongclick from listview


remove item onlongclick from listview

Content Index :

remove item onlongclick from listview
Tag : android , By : taviso
Date : November 23 2020, 04:01 AM

it should still fix some issue I want when long pressing an item of my list to give an option delete and delete the item if it pressed. , Add this in your onContextItemSelected :
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); // init the info to get the position from
items.remove(info.position); // remove the item from the list 
adapter.notifyDataSetChanged(); //updating the adapter

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

How to add/remove item from listview in android when click button in item listview


Tag : android , By : Manik
Date : March 29 2020, 07:55 AM
Does that help I'm a newbie android.I have some problem about my mini app. You can see figure below: http://i481.photobucket.com/albums/rr175/viethungit/android.png
public class AndroidCustomListViewActivity extends Activity {
    private ListView myList;
    private MyAdapter myAdapter;
    private ImageView myImage;
    public static  String upload= " ";
    public static String GalleryImage;
    public ArrayList<ListItem> myItems = new ArrayList<ListItem>();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listandimage);

        myList = (ListView) findViewById(R.id.MyList);
        myImage= (ImageView)findViewById(R.id.image1);

        myList.setItemsCanFocus(true);
        myAdapter = new MyAdapter();

        ListItem listItem = new ListItem();
        listItem.textdata="@";
        listItem.caption = "";
        myItems.add(listItem);

        myList.setAdapter(myAdapter);
        myAdapter.notifyDataSetChanged();


    }


    public class MyAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public MyAdapter() {
            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

        public int getCount() {
            return myItems.size();
        }

        public ListItem getItem(int position) {
            return myItems.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.item, null);
                holder.text=(TextView )convertView.findViewById(R.id.textView1); 
                holder.captionEditText = (EditText) convertView.findViewById(R.id.ItemCaption);

                holder.addOrDeleteButton = (Button) convertView.findViewById(R.id.buttonAdd);

                holder.captionEditText.setFocusable(true);
                holder.captionEditText.requestFocus();

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

            // Fill EditText with the value you have in data source
            // holder.captionEditText.setId(position);
            holder.text.setTag(position);
            holder.captionEditText.setTag(position);
            holder.captionEditText.setText(getItem(position).caption);

            holder.addOrDeleteButton.setTag(position);

            // / this updates tag of
            // the button view as we
            // scroll ///

            holder.addOrDeleteButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    // TODO Auto-generated method stub
                    int tag = (Integer) view.getTag();
                    if (tag != (myItems.size() - 1)) {
                        myItems.remove(tag);
                        Log.d("GCM", "Item removed from " + tag);
                        myAdapter.notifyDataSetChanged();
                    } else {
                        ListItem listItem = new ListItem();
                        listItem.textdata="@";
                        listItem.caption = "";
                        myItems.add(listItem);


                        /*
                         * Log.d("GCM", holder.captionEditText.getText()
                         * .toString()); myItems.get((Integer)
                         * view.getTag()).caption = holder.captionEditText
                         * .getText().toString();
                         */
                        myAdapter.notifyDataSetChanged();
                        myList.setSelection(myAdapter.getCount() - 1);
                        // holder.captionEditText.setFocusable(true);
                        // holder.captionEditText.requestFocus();

                    }
                }
            });

            holder.captionEditText.addTextChangedListener(new TextWatcher() {

                public void onTextChanged(CharSequence s, int start,
                        int before, int count) {
                    // TODO Auto-generated method stub

                }

                public void beforeTextChanged(CharSequence s, int start,
                        int count, int after) {
                    // TODO Auto-generated method stub

                }

                public void afterTextChanged(Editable s) {
                    // if(position < myItems.size())
                    // getItem(position).caption = s.toString();

                    myItems.get((Integer) holder.captionEditText.getTag()).caption = holder.captionEditText
                            .getText().toString();

                }
            });

            if (position != (myItems.size() - 1)) {
                holder.addOrDeleteButton.setBackgroundResource(R.drawable.fruttarecloseicon);
            } else {
                holder.addOrDeleteButton.setBackgroundResource(R.drawable.fruttareaddicon);
                holder.text.setFocusable(true);
                holder.captionEditText.setFocusable(true);
                holder.text.requestFocus();
                holder.captionEditText.requestFocus();

            }

            return convertView;
        }

    }

    class ViewHolder {

        TextView text;
        EditText captionEditText;
        Button addOrDeleteButton;
    }

    class ListItem {
        String textdata;
        String caption;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
     >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="15dp"
        android:layout_height="50dp"
        android:text=" @"
       />

    <EditText
        android:id="@+id/ItemCaption"
        android:layout_width="270dp"
        android:layout_height="50dp"
        android:layout_margin="3dip"
        android:imeOptions="actionDone|flagNoExtractUi"
        android:inputType="textNoSuggestions"
        android:singleLine="true" >
    </EditText>

    <Button
        android:id="@+id/buttonAdd"
        android:layout_width="75dp"
        android:layout_height="50dp"
        android:layout_margin="3dip"
        />

</LinearLayout>


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

    <ListView
        android:id="@+id/MyList"
        android:layout_width="370dp"
        android:layout_height="160dp"
        android:layout_gravity="center|top"
        android:layout_marginLeft="150dp"
        android:descendantFocusability="beforeDescendants" 
             >
    </ListView>

    <ImageView
        android:id="@+id/image1"
        android:layout_width="80dp"
        android:layout_height="80dp"

         />

</LinearLayout>

ListView OnLongClick strange behaviour


Tag : java , By : user160048
Date : March 29 2020, 07:55 AM
I hope this helps . ListViews recycle Views, so you only have a few views for all of your items. You're directly changing one of these view instances to switch between the info|brief. What you need is to save the status of the info|brief flag for the affected position somewhere else (e.g. a list of positions that should be "briefs" in the adapter). That way when you come back into getView() you can display the right one.

How to use onLongClick method in ListView?


Tag : java , By : AJacques
Date : March 29 2020, 07:55 AM
will be helpful for those in need Hello guys I'am totally new in android and I have an assignment that will output an option when a user long clicks the item that is in list view. Can you help me about this? thanks in advance. I get this code in the internet and try to tweak it but I don't know where to put the method onlongclick. please help me guys , you havent initialize your listview with xml listview..
ListView lst=null;
lst = (ListView) findViewById(R.id.xmllistviewid);

How to change ListView Item's background when onLongClick if adapter extends CursorAdapter


Tag : android , By : Sinisa Ruzin
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I have ListView which gets data from SQLite and a Cursor Adapter. , You should do something like
if (mActionMode != null)
            {
                view.setBackgroundColor(Color.TRANSPARENT);
                mActionMode.finish();
                return false;
            }else
            {
                mActionMode = Tctivity.this.startActionMode(TActivity.this);
                view.setSelected(true);
                view.setBackgroundColor(Color.parseColor("#b8dbd3"));
            }

Listview onlongclick delete


Tag : android , By : cautionsign
Date : March 29 2020, 07:55 AM
this will help As you are deleting data from your database but the list attached to adapter still holds the data, you have to remove from that position and notify your adapter.
Try this code:
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
        myDB.deleteName(theList.get(i));
        theList.remove(i); 
        listAdapter.notifyDataSetChanged();
        Toast.makeText(ListDataActivity.this, "item removed", Toast.LENGTH_SHORT).show();
        return true;
    }
});
public void deleteName(String name){
    SQLiteDatabase db=this.getWritableDatabase();
    String query= "DELETE FROM "+ TABLE_NAME +" WHERE " + COL2 + "= '" + name + "'";
    db.execSQL(query);

}
myDB.deleteName(theList.get(i));
Related Posts Related QUESTIONS :
  • Is displayMetrix xdpi and ydpi accurate?
  • How to detect doubletap on a View?
  • how to play an encrypted file in Android
  • Android: keytool error on the command line when locating debug.keystore
  • Android paint/canvas issue; drawing smooth curves
  • Problems with starting an activity in onStart
  • Performance of map overlay in conjunction with ItemizedOverlay is very poor
  • How to change android indeterminate ProgressBar color?
  • How do I change the Mobile Country Code (MCC) in the Android Emulator?
  • Stop Progressbar manual scrolling in Android
  • Android: download large file
  • Android : uploaded .apk file to server but cant download that .apk file
  • How do I distribute Android app to beta tester without using Market
  • Android Unable to launch the Inbox from a notification
  • Android: Ringer mode changed to silent doesn't stop the phone vibrating
  • Change the side the text appears on a radio button
  • Check the memory usage anp cpu and power of an application in android
  • What is the String 'volumeName' argument of MediaStore.Audio.Playlists.Members.getContentUri referring to?
  • Playing Multiple sounds at the same time in Android
  • Android: how to change the time in emulator?
  • close the soft key pad when i click on other views in android
  • Setting the position within a spinner
  • How to play ringtone/alarm sound in Android
  • How to adjust text font size to fit textview
  • "Android 2.x" vs "Google APIs" for Android AVD Setup
  • How to center buttons on screen horizontally and vertically plus equidistant apart?
  • Discrete seekbar in Android app?
  • Gradle Build Fails - Illegal exception table range in class file com/google/wireless/android/sdk/stats/GradleBuildMemory
  • register has type Long (High Half) but expected Long (Low Half)
  • Is it good idea to use Dispatcher.Main with delay( )?
  • WebView and Advanced View is Not loading partiucalr URL
  • How to show three dots at the end of text view
  • Is it possible to run macros within Android Studio project?
  • Adding a button with default appearance but changed background color
  • What is best way to show notification at a certain time in Android?
  • Jetifier not working when library uses android component and app uses androidx component
  • How can I write the contents of an image in a filesystem
  • Retrieve a list of images from the Database and display them using a ListView?
  • java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
  • Timer/hourglass wont stop
  • How can I make AndroidX the default library for new Android projects?
  • Is it okay to delete buildToolsVersion android studio because of a warning in the Asyncstorage library?
  • Can I call a class that doesn't have an activity withIntent?
  • Android TextView fontFamily not working when set via TextAppearance
  • Unable to find nearby places
  • Is there any way to launch my application right away without 'Ok Google"?
  • Places api set location biased based on current location
  • Layout Preview is not displaying layout when Material Design is added
  • How do I resolve this diamond problem in Kotlin?
  • How to update the custom value in a view model object
  • How to stop refreshing/re-creating activity when updating Firebase data using setValue?
  • Manifest Permissions vs App Setttings Permissions
  • NumberFormatException in Try-Catch while converting String to Int
  • How to pass observable fields through include tag?
  • Next button on Android keyboard not submitting form
  • Does Firestore support automatic functions for updates?
  • Why app crashing because of a missing dimension (for google maps) only when I apply changes?
  • How to make a grouping, ordering and sorting: Kotlin collection
  • how to use augmented reality to change the color of a qr code once it is detected using with arcore and ml kit firebase
  • how to resolve Firebase projects adding quota problem?
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com