Open top menu







Explain-: How to Remove Index row from listView  OnItemClick?


Activity_main.xml.
……………………………………………………………………………………………………………………………………
xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.project.example.listviewremoveindex.MainActivity">
    <ListView

        android:id="@+id/list_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
……………………………………………………………...........

MainActivity.Java.
……………………………………………………………………
package com.project.example.listviewremoveindex;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    ListView list;
    Button btnAdd, btnRemove;
    ArrayList listarray=new ArrayList<>();

    @Override
   
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Add Values
       
for (int i=0;i<10;i++){
            listarray.add("Item "+i);
        }
        list = (ListView) findViewById(R.id.list_item);
        final ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1, listarray);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
           
public void onItemClick(AdapterView parent, View view, int position, long id) {




                String slelctitem=listarray.get(position);
                Toast.makeText(getApplicationContext(),slelctitem+" deleted",Toast.LENGTH_SHORT).show();
                listarray.remove(position);
                adapter.notifyDataSetChanged();

            }
        });
    }



}




0 comments