如何在 Android Studio 中创建自定义阵列适配器以禁用对列表视图项的单击



当前的大学学生和我正在尝试创建一个自定义数组适配器类来扩展 ArrayAdapter 类,以便我可以禁用对列表视图中选定项目的单击。

我不确定如何实现这个新类 这就是我到目前为止所拥有的

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);
}
}

我也不确定如何编写 isEnabled 函数:

这就是我打算如何使用此功能

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);
}

因此,当用户单击警报中的"删除"按钮时,我想将他们在列表视图中单击的项目变灰(我设法实现了此目的),但我也希望一旦删除它就无法单击它

以及关于如何实现这一目标的建议?

不要使用常规ListView使用自定义:

1) 创建一个CustomListView类:

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. 将您创建的CustomListView添加到 XML 布局中,而不是ListView

  2. 取代:

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

    由:

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

2) 从CustomAdapter类中删除这些方法:

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

3)像这样处理项目点击:

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();
}
});

如果使用上述实现,则当您单击警报对话框中的删除按钮时,该项目下次将不会收到单击事件。

这是一个示例,它将完全帮助您的情况,并且您将知道如何使用模型,以防您需要在列表项上执行任何操作。

自定义适配器

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;
}
}

数据模型

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;
}
}

主活动

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>

最新更新