Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@chihebnabil
Created March 4, 2018 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chihebnabil/4294f5164f586cb48a25ab56abe8ceff to your computer and use it in GitHub Desktop.
Save chihebnabil/4294f5164f586cb48a25ab56abe8ceff to your computer and use it in GitHub Desktop.
package net.simplifiedlearning.customlistviewandroid;
/**
* Created by Belal on 9/14/2017.
*/
public class Hero {
int image;
String name, team;
public Hero(int image, String name, String team) {
this.image = image;
this.name = name;
this.team = team;
}
public int getImage() {
return image;
}
public String getName() {
return name;
}
public String getTeam() {
return team;
}
}
package net.simplifiedlearning.customlistviewandroid;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//a List of type hero for holding list items
List<Hero> heroList;
//the listview
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initializing objects
heroList = new ArrayList<>();
listView = (ListView) findViewById(R.id.listView);
//adding some values to our list
heroList.add(new Hero(R.drawable.spiderman, "Spiderman", "Avengers"));
heroList.add(new Hero(R.drawable.joker, "Joker", "Injustice Gang"));
heroList.add(new Hero(R.drawable.ironman, "Iron Man", "Avengers"));
heroList.add(new Hero(R.drawable.doctorstrange, "Doctor Strange", "Avengers"));
heroList.add(new Hero(R.drawable.captainamerica, "Captain America", "Avengers"));
heroList.add(new Hero(R.drawable.batman, "Batman", "Justice League"));
//creating the adapter
MyListAdapter adapter = new MyListAdapter(this, R.layout.my_custom_list, heroList);
//attaching adapter to the listview
listView.setAdapter(adapter);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="15dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textViewName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/imageView"
android:layout_toRightOf="@+id/imageView"
android:paddingBottom="10dp"
android:text="Spiderman"
android:textAlignment="center"
android:textSize="35dp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView"
android:layout_width="150dp"
android:layout_height="150dp" />
<TextView
android:id="@+id/textViewTeam"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewName"
android:layout_toEndOf="@+id/imageView"
android:layout_toRightOf="@+id/imageView"
android:paddingTop="10dp"
android:text="Spiderman"
android:textAlignment="center"
android:textSize="25dp"
android:textStyle="bold" />
<Button
android:id="@+id/buttonDelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView"
android:layout_toEndOf="@+id/imageView"
android:layout_toRightOf="@+id/imageView"
android:background="#FF0000"
android:text="Delete" />
</RelativeLayout>
package net.simplifiedlearning.customlistviewandroid;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by Belal on 9/14/2017.
*/
//we need to extend the ArrayAdapter class as we are building an adapter
public class MyListAdapter extends ArrayAdapter<Hero> {
//the list values in the List of type hero
List<Hero> heroList;
//activity context
Context context;
//the layout resource file for the list items
int resource;
//constructor initializing the values
public MyListAdapter(Context context, int resource, List<Hero> heroList) {
super(context, resource, heroList);
this.context = context;
this.resource = resource;
this.heroList = heroList;
}
//this will return the ListView Item as a View
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
//we need to get the view of the xml for our list item
//And for this we need a layoutinflater
LayoutInflater layoutInflater = LayoutInflater.from(context);
//getting the view
View view = layoutInflater.inflate(resource, null, false);
//getting the view elements of the list from the view
ImageView imageView = view.findViewById(R.id.imageView);
TextView textViewName = view.findViewById(R.id.textViewName);
TextView textViewTeam = view.findViewById(R.id.textViewTeam);
Button buttonDelete = view.findViewById(R.id.buttonDelete);
//getting the hero of the specified position
Hero hero = heroList.get(position);
//adding values to the list item
imageView.setImageDrawable(context.getResources().getDrawable(hero.getImage()));
textViewName.setText(hero.getName());
textViewTeam.setText(hero.getTeam());
//adding a click listener to the button to remove item from the list
buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//we will call this method to remove the selected value from the list
//we are passing the position which is to be removed in the method
removeHero(position);
}
});
//finally returning the view
return view;
}
//this method will remove the item from the list
private void removeHero(final int position) {
//Creating an alert dialog to confirm the deletion
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Are you sure you want to delete this?");
//if the response is positive in the alert
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//removing the item
heroList.remove(position);
//reloading the list
notifyDataSetChanged();
}
});
//if response is negative nothing is being done
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
//creating and displaying the alert dialog
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment