0

This is may XML class random values in which we make a row that I want to delete

randomvalues.xml

  <LinearLayout
android:id="@+id/linear"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="@+id/addbtn">

<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="30dp"
        android:layout_height="40dp"
        android:src="@drawable/img1"
        />

   <LinearLayout
        android:layout_width="255dp"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Name"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#339966"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/adress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Address"
                android:textColor="#606060" />
        </LinearLayout>
    </LinearLayout>        

    <ImageButton
        android:id="@+id/removebtn"
        android:layout_width="30dp"
        android:layout_height="40dp"
        android:src="@drawable/remove"/>       
</LinearLayout>
</LinearLayout>

this is my activity_main XML in which i used a list view to show a row that I make in random values XML file

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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.example.chaqeel.taskviews.MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/linear">

<ImageButton
    android:id="@+id/addbtn"
    android:layout_width="30dp"
    android:layout_height="40dp"
    android:src="@drawable/add"
    android:layout_marginLeft="280dp"/>
</LinearLayout>


<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/linear"
   >

</ListView>
</RelativeLayout>

This is MainActivity.java in which we used a array to show the values

MainActivity.java

package com.example.chaqeel.taskviews;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;

public class MainActivity extends Activity {

ListView lv;

String[] Names = {"Aqeel", "Ali", "Ansar", "Usama", "Farhad"};
 String[] Address = {"Chakwal", "Rawalpindi", "Islamabad", "Lahore",       
"Multan"};
int[] Images = {R.drawable.img1, R.drawable.img2, R.drawable.img3, 
R.drawable.img4, R.drawable.img5};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ListView) findViewById(R.id.listview);
    lv.setAdapter(new dataListAdapter(Names, Address, Images));
}

class dataListAdapter extends BaseAdapter {
    String[] Name, Addres;
    int[] imge;

    /*dataListAdapter() {
        Name = null;
        Addres = null;
        imge=null;
    }*/

    public dataListAdapter(String[] text, String[] text1, int[] text3) {
        Name = text;
        Addres = text1;
        imge = text3;
    }

    public int getCount() {
        return Name.length;
    }

    public Object getItem(int arg0) {
        return null;
    }

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

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

        LayoutInflater inflater = getLayoutInflater();
        final View row;
        row = inflater.inflate(R.layout.randomvalues, parent, false);
        final TextView Name, Addres;
        ImageView imge;
        Name = (TextView) row.findViewById(R.id.name);
        Addres = (TextView) row.findViewById(R.id.adress);
        imge = (ImageView) row.findViewById(R.id.img);
        Name.setText(Names[position]);
        Addres.setText(Address[position]);
        imge.setImageResource(Images[position]);

      final   ArrayList<String> lvv= new ArrayList<>();
        Collections.addAll(lvv,Names);
     //  Collection.addAll(lvv,Address);

    ImageButton dltbutton = (ImageButton) findViewById(R.id.removebtn);
    dltbutton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            lvv.remove(Names);
            lvv.remove(Address);
            notifyDataSetChanged();
        }
    });

    return (row);
    }    
}
}
swiftBoy
  • 35,607
  • 26
  • 136
  • 135

7 Answers7

1

In the onClickListener try the following code

youradapter.notifyDataSetChanged();

0

In the deletebutton onClickListener try this

Names = ArrayUtils.removeElement(Names,Names[position]);
Address = ArrayUtils.removeElement(Address,Address[position]);
notifyDataSetChanged();

Though it is advised to use OOP concept like a class to hold the Arrays of Names,Address and Images.

Ashik Vetrivelu
  • 1,021
  • 1
  • 9
  • 24
0

Try below code:

dltbutton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {


            Name = ArrayUtils.removeElement(Names, Names[position]);
            Address = ArrayUtils.removeElement(Address, Address[position]);
            imge = ArrayUtils.removeElement(imge, imge[position]);
            notifyDataSetChanged();
        }
    });
Md Sufi Khan
  • 1,751
  • 1
  • 14
  • 19
0

change this:

    ImageButton dltbutton = (ImageButton) findViewById(R.id.removebtn);
    dltbutton.setTag(position);
    dltbutton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

           Integer index = (Integer) view.getTag();
            lvv.remove(index.intValue());  
            notifyDataSetChanged();

    }
});
Damini Mehra
  • 3,257
  • 3
  • 13
  • 24
0

You need to better idea to use Model class as below:-

create a model class MyModel.class

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

public class MyModel {
    String Name, Address;
    int image;


    public MyModel(String name, String address, int image) {
        Name = name;
        Address = address;
        this.image = image;
    }

    public String getName() {
        return Name;
    }

    public String getAddress() {
        return Address;
    }

    public int getImage() {
        return image;
    }
}

and then add with adapter class see below:

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import java.util.List;


public class MainActivity extends Activity {


ListView lv;

String[] Names = {"Aqeel", "Ali", "Ansar", "Usama", "Farhad"};
 String[] Address = {"Chakwal", "Rawalpindi", "Islamabad", "Lahore",       
"Multan"};
int[] Images = {R.drawable.ic_menu_camera, R.drawable.ic_menu_gallery, R.drawable.ic_menu_manage,
R.drawable.ic_menu_send, R.drawable.ic_menu_share};
private List<MyModel> myModel=new ArrayList<>();
private DataListAdapter dataListAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_main);
    lv = (ListView) findViewById(R.id.listview);

    for(int i=0;i<Names.length;i++){
        myModel.add( new MyModel(Names[i],Address[i],Images[i]));
    }
    dataListAdapter=new DataListAdapter(myModel);
    lv.setAdapter(dataListAdapter);


}

class DataListAdapter extends BaseAdapter {
   private List<MyModel> myModel=new ArrayList<>();

    public DataListAdapter(List<MyModel> myModel) {
      this.myModel=myModel;

    }

    public int getCount() {

        return myModel.size();
    }

    public Object getItem(int arg0) {

        return null;
    }

    public long getItemId(int position) {

        return position;
    }

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

        LayoutInflater inflater = getLayoutInflater();
        final View row;
        row = inflater.inflate(R.layout.randomevalue, parent, false);
        final TextView Name, Addres;
        ImageView imge;
        Name = (TextView) row.findViewById(R.id.name);
        Addres = (TextView) row.findViewById(R.id.adress);
        imge = (ImageView) row.findViewById(R.id.img);
        Name.setText(myModel.get(position).getName());
        Addres.setText(myModel.get(position).getAddress());
        imge.setImageResource(myModel.get(position).getImage());

    ImageButton dltbutton = (ImageButton) row.findViewById(R.id.removebtn);
    dltbutton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            myModel.remove(position);
            notifyDataSetChanged();
        }
    });


    return (row);
    }


}

    public void addMore(View v)
    {
        myModel.add( new MyModel("Mahesh","India",R.drawable.ic_menu_share));
        dataListAdapter.notifyDataSetChanged();

    }


}

enter image description here

public void addMore(View v)
    {
        myModel.add( new MyModel("Mahesh","India",R.drawable.ic_menu_share));
        dataListAdapter.notifyDataSetChanged();

    }

add android:onClick="addMore" to your add button

it will fine Happy Coding :)

Neeraj Singh
  • 610
  • 9
  • 15
  • thanku can you also tell me how we get random values form array list on clicking add button that i am show in activity main xml file – Ch Aqeel Aug 31 '16 at 09:05
  • check for latest code updates above and don't forget to click up arrow. – Neeraj Singh Aug 31 '16 at 09:34
  • sorry sir..i get randomly data from inserted data in list view that i store in mymodel class...on clicking add button – Ch Aqeel Aug 31 '16 at 12:23
0

Are you from Chakwal? I am Chakwalian. Again StackOverflow is telling me that my answer does not meet thier quality requirements.

user6657161
  • 361
  • 1
  • 8
-2
@Override
public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    AdapterContextMenuInfo info =(AdapterContextMenuInfo) item.getMenuInfo();
    pos=info.position;
    deleteditem=myList.get(pos);

    if(item.getTitle()=="Delete")
    {   
        String delete = myList.get(pos);
        File f = new File(path + "/"+ delete);

        if (f != null && f.exists()) 
        {
            f.delete();
        }

        myList.remove(pos); 
        adapter. notifyDataSetChanged();

        Toast.makeText(getApplicationContext(), "item has deleted",Toast.LENGTH_LONG).show();
}
Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
R.Devi
  • 1
  • 2