how to create a custom array adapter in android studio to disable clicks on list view item

Viewed 440

current Uni student and i am trying to create a custom array adapter class that extends the ArrayAdapter class so i can disable clicks on selected items in the list view.

I am unsure how to implement this new class this is what i have so far

package com.example.assignment1;

import android.content.Context;
import android.widget.ArrayAdapter;

import androidx.annotation.NonNull;

public class customAdapter extends ArrayAdapter {
    public customAdapter(@NonNull Context context, int resource) {
        super(context, resource);
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
       // return super.isEnabled(position);
    }
}

I am unsure how to write the isEnabled function as well:

this is how i intend to use this function

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                poss = position + 1;
                AlertDialog.Builder builder = new AlertDialog.Builder(viewFriends.this);
                builder.setTitle("Notice");
                builder.setMessage("Please select to to edit, delete a friend or cancel");

                // add the buttons
                builder.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                         //System.out.println(poss);
                        Intent intent = new Intent(getApplicationContext(), editOrDelete.class);
                        ArrayList<String> result1 = mydb.retrieveRow(poss);
                        name = result1.get(1);
                        age = result1.get(2);
                        gender = result1.get(3);
                        address = result1.get(4);
                        code = result1.get(0);
                        intent.putExtra("code", code);
                        intent.putExtra("name", name);
                        intent.putExtra("age", age);
                        intent.putExtra("gender", gender);
                        intent.putExtra("address", address);
                        startActivity(intent);
                    }
                });
                builder.setNeutralButton(" Delete", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        System.out.println(poss);
                        mydb.updateDeleted(poss);
                       if(listView.getChildAt(position).isEnabled())
                        {
                           listView.getChildAt(position).setEnabled(false);
                          // function to disable clicks
                       }
                    }
                });
                builder.setNegativeButton("Cancel", null);
                AlertDialog dialog = builder.create();
                dialog.show();


            }
        });



    }
public void displayFriendList() {
        ArrayList<String> result = mydb.retrieveRows();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, result);
        listView.setAdapter(adapter);
    }

so when the user clicks the delete button from the alert i would like to grey out the item they clicked on in the list view (i managed to achieve this) but i would also like for it not to be able to be clicked on once it has been deleted

and suggestions on how i can achieve this?

2 Answers

Instead of using a regular ListView use a custom one:

1) Create a CustomListView class:

public class CustomListView extends ListView{

//add these three constructors
public CustomListView(Context context){
super(context);
}
public CustomListView(Context context , AttributeSet attrs){
super(context , attrs);
}
public CustomListView(Context context , AttributeSet attrs, int defStyleAttr){
super(context , attrs, defStyleAttr);
}

//handle the item click
@Override
public boolean performItemClick(View view , int position , long id){

if(!view.isEnabled()){
//don't handle the click
return false;
}else{
//handle the click
return super.performItemClick(view, position, id);
}

}

}
  1. add the CustomListView that you created to your xml layout instead of ListView.

  2. replace:

    ListView listView = findViewById(.......);
    

    by:

    CustomListView listView = findViewById(.......);
    

2) remove these methods from your CustomAdapter class:

@Override
public boolean areAllItemsEnabled() {
    return false;
}

@Override
public boolean isEnabled(int position) {
   // return super.isEnabled(position);
}

3) handle the item click like that:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view, final int position, long id) {

poss = position + 1;
AlertDialog.Builder builder = new AlertDialog.Builder(viewFriends.this);
builder.setTitle("Notice");
builder.setMessage("Please select to to edit, delete a friend or cancel");

// add the buttons
builder.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

//.......keep whatever you have the same here

}
});

builder.setNeutralButton(" Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

//disable the view so that you won't receive clicks again
view.setEnabled(false);

}
});

builder.setNegativeButton("Cancel", null);
AlertDialog dialog = builder.create();
dialog.show();

}
});

If you use the above implementation, then when you click delete button in alert dialog the item won't receive the click event the next time.

This is an example which will help your case exactly and you'll come to know how to work with model in case you need to perform anything on the list item.

CustomAdapter

import android.content.Context;
import android.widget.ArrayAdapter;

import androidx.annotation.NonNull;

import java.util.ArrayList;

public class CustomAdapter extends ArrayAdapter {

    private ArrayList<DataModel> dataModels = new ArrayList<>();

    CustomAdapter(@NonNull Context context, int resource, ArrayList<DataModel > data) {
        super(context, resource,data);
       dataModels=data;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        if(dataModels.size()>0) {
            DataModel dataModel = dataModels.get(position);
            return dataModel.isEnabled();
        }
        return true;
    }
}

DataModel

public class DataModel {

    private String data;

    private boolean isEnabled=true;

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public boolean isEnabled() {
        return isEnabled;
    }

    public void setEnabled(boolean enabled) {
        isEnabled = enabled;
    }
}

MainActivity

import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ListView listView;

    CustomAdapter adapter;

    private ArrayList<DataModel> dataModels =new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         listView=findViewById(R.id.listView);
         displayFriendList();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Notice");
                builder.setMessage("Please select to to edit, delete a friend or cancel");

                // add the buttons
                builder.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.setNeutralButton(" Delete", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dataModels.get(position).setEnabled(false);
                        adapter.notifyDataSetChanged();
                    }
                });
                builder.setNegativeButton("Cancel", null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });



    }


    public void displayFriendList() {
        for(int i=0;i<10;i++)
        {
            DataModel dataModel=new DataModel();
            dataModel.setData(i+"data");
            dataModels.add(dataModel);
        }
         adapter = new CustomAdapter(this,
                android.R.layout.simple_list_item_1,dataModels);
              listView.setAdapter(adapter);
    }
}

activity_main

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>
Related