簡體   English   中英

如何進行確認對話框?

[英]how can i make a confirm dialog?

我在 android 工作室中創建了一個待辦事項應用程序。 當您單擊任務項列表時,它會自行刪除。 但我想在那之前顯示一個確認對話框。我正在使用兩個 java 類。 此待辦事項應用程序的 MainActivity 和 Filehelper。

所以我使用此代碼刪除了該項目並且它正在工作:

    @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    items.remove(position);
    adapter.notifyDataSetChanged();
    Toast.makeText(this, "Deleted", Toast.LENGTH_SHORT).show();
}

現在我正在使用此代碼和確認對話框:

    @Override  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Confirm dialog demo !");
    builder.setMessage("You are about to delete all records of database. Do you really want to proceed ?");
    builder.setCancelable(false);
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface parent, int position) {
            items.remove(position);
        }
    });

    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "You've changed your mind to delete tasks", Toast.LENGTH_SHORT).show();
        }
    });

    builder.show();
}

但是當我在我的 android 上運行它時。 我看到了對話框。 但是當我按下是時它會自行崩潰。 所以你能給我正確的代碼嗎? 並告訴我我的代碼的問題?

主要活動代碼:

public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

private EditText ItemET;
private Button btn;
private ListView itemList;

private ArrayList<String> items;
private ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ItemET = findViewById(R.id.item_edit_text);
    btn = findViewById(R.id.add_btn);
    itemList = findViewById(R.id.item_list);

    items = FileHelper.readData(this);

    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
    itemList.setAdapter(adapter);

    btn.setOnClickListener(this);
    itemList.setOnItemClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.add_btn:
            String ItemEntered = ItemET.getText().toString();
            if (ItemEntered.trim().isEmpty()) {
                Toast.makeText(getApplicationContext(), "Please Enter some detail", Toast.LENGTH_LONG).show();
            } else {
                adapter.add(ItemEntered);
                ItemET.setText("");
                FileHelper.writeData(items, this);
                Toast.makeText(this, "item Added", Toast.LENGTH_SHORT).show();
            }
            break;
    }
}


// Confirm box
@Override  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Confirm dialog demo !");
    builder.setMessage("You are about to delete all records of database. Do you really want to proceed ?");
    builder.setCancelable(false);
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface parent, int position) {
            items.remove(position);
        }
    });

    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "You've changed your mind to delete all records", Toast.LENGTH_SHORT).show();
        }
    });

    builder.show();
}

}

文件助手代碼:

公共 class FileHelper {

public static final String FILENAME = "listinfo.dat";

public static void writeData(ArrayList<String> items, Context context){

    try {
        FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(items);
        oos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

    public static ArrayList<String> readData(Context context) {

        ArrayList<String> itemList = null;
        try {
            FileInputStream fis = context.openFileInput(FILENAME);
            ObjectInputStream ois = new ObjectInputStream(fis);
            itemList = (ArrayList<String>) ois.readObject();
        } catch (FileNotFoundException e) {

            itemList = new ArrayList<>();


            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return itemList;

    }

}

這也是我的 MainActivity.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout

    android:layout_width="match_parent"
    android:layout_marginTop="12dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginBottom="12dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <EditText

        android:id="@+id/item_edit_text"
        android:hint="Enter Item"
        android:layout_width="0dp"
        android:layout_weight="1.9"
        android:layout_marginRight="20dp"
        android:layout_height="wrap_content" />
    <Button
        android:background="@drawable/layout_bg"
        android:id="@+id/add_btn"
        android:text="add"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"/>


</LinearLayout>

<ListView
    android:background="@drawable/layout_bg"
    android:id="@+id/item_list"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:textAlignment="center"
    android:layout_marginBottom="24dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

您可以創建一個方法並在需要時調用它

void confirmDialog(){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Discard Data!");// Dialog header
    builder.setMessage("Are you sure you want to discard this data?");  //disclaimer text

// yes button
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            edit_text.getText().clear();  //clear EditText

        }
    });

//No button
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

        }
    });
    builder.create().show();
}

你的代碼沒問題。 你能分享你的崩潰日志嗎? 您要從中刪除的列表中似乎有問題。

如果可能的話,分享你的整個 class。

請發送 logcat 圖片。 然后將更容易識別問題。 還要檢查

builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface parent, int position) {
        Toast.makeText(getApplicationContext(), "Check if toast is working or crush. ", Toast.LENGTH_SHORT).show();
    }
});

當“是”按鈕被按下或壓碎時,檢查吐司是否正常工作。 然后你會更加確定問題所在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM