单击按钮删除ListView条目-Android

时间:2018-12-08 14:18:53

标签: java android

我有具有列表视图和按钮的自定义列表视图。我想删除其按钮被单击的列表视图的条目。但是我的应用程序一直崩溃,因为该按钮从数组中删除了项目,但没有更新视图。数组为空时,应用程序崩溃。 这就是我所拥有的:

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Button;

public class ComplainDetailsAdvisorActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complain_details_advisor);

        final String[] foods = {"Bacon", "Ham", "Tuna", "Candy", "Meatball", "Potato"};


        final ListAdapter customListAdapter = new CustomAdapter(this,foods);// Pass the food array to the constructor.
        ListView customListView = (ListView) findViewById(R.id.complainList);
        customListView.setAdapter(customListAdapter);}}

CustomAdapterClass:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;


class CustomAdapter extends ArrayAdapter<String>{
    ArrayList<String> foods;

    public CustomAdapter(Context context, String[] foods) {
        super(context, R.layout.customrow, foods);
        this.foods=new ArrayList<String>(Arrays.asList(foods));;
    }

    public void refreshFoods(ArrayList<String> foods) {
        this.foods.clear();
        this.foods.addAll(foods);
        notifyDataSetChanged();
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater myCustomInflater = LayoutInflater.from(getContext());
        View customView = myCustomInflater.inflate(R.layout.customrow, parent, false);
        String singleFoodItem = getItem(position);
        TextView itemText = (TextView) customView.findViewById(R.id.adComplain);

        itemText.setText(singleFoodItem);

        Button b2 = (Button) customView.findViewById(R.id.resolveButton);
        b2.setTag(position);
        b2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                ((ViewGroup) arg0.getParent()).removeView(arg0);
                int pos = (int)arg0.getTag();
                foods.remove(pos);
                refreshFoods((ArrayList<String>) foods.clone());

                CustomAdapter.this.notifyDataSetChanged();            }
        });

        return customView;
    }
}

ComplainDetailsAdvisorActivity的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" >


    <ListView
        android:id="@+id/complainList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"

        />


</RelativeLayout>

列表视图中自定义行的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" >

    <Button
        android:id="@+id/resolveButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="3dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:text="@string/resolve_button" />

    <TextView
        android:id="@+id/adComplain"
        android:layout_width="355dp"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/adVotes"
        android:layout_alignParentTop="true"
        android:layout_marginStart="4dp"
        android:layout_marginTop="63dp"
        android:text="TextView"
        android:layout_alignLeft="@+id/adVotes"
        android:layout_marginLeft="4dp" />

</RelativeLayout>

编辑:这是错误日志

2018-12-08 19:27:20.816 719-719/? E/SDAgentPackageStateReceiver: Not going to handle 'com.example.mariaahmed.sas'!
2018-12-08 19:27:22.304 719-719/? E/SDAgentPackageStateReceiver: Not going to handle 'com.example.mariaahmed.sas'!
2018-12-08 19:27:28.503 9745-9745/? E/SPPClientService: [PackageInfoChangeReceiver] [handlePkgRemovedEvent] PackageName : com.example.mariaahmed.sas, true, false
2018-12-08 19:27:29.053 27395-27395/com.example.mariaahmed.sas E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.mariaahmed.sas, PID: 27395
    java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
        at java.util.ArrayList.remove(ArrayList.java:477)
        at com.example.mariaahmed.sas.CustomAdapter$1.onClick(CustomAdapter.java:47)
        at android.view.View.performClick(View.java:6312)
        at android.widget.TextView.performClick(TextView.java:11202)
        at android.view.View$PerformClick.run(View.java:23985)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6816)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
2018-12-08 19:27:31.093 27791-27808/? E/AASAservice-TokenRule: parseToken() : TokenFile is null
2018-12-08 19:27:31.162 27791-27808/? E/AASAservice-IntentThread: AASAIntentThread : myrule is null.

1 个答案:

答案 0 :(得分:-1)

您需要重写get counts方法并返回食物arraylist的大小。