警报对话框中出错

时间:2016-06-08 14:25:52

标签: android

'返回视图'在listview.setOnItemLongClickListener方法。该错误表示无法从具有void结果类型的方法返回值。我正在尝试创建一个警告对话框,它会通知用户是否要删除。

package com.example.user.swen;


import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;


import java.util.ArrayList;
import java.util.List;



import com.example.user.swen.DB.RecordsDataSource;
import com.example.user.swen.Model.Records;

public class Record extends ListActivity {

    public Button NewButton;
    public RecordsDataSource Recordsdatasource;
    ArrayAdapter<Records> RecordAdapter;
    List<Records> records;
    ListView listview;
    Records selectedRecord;


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


        //Referencing the Database
        Recordsdatasource = new RecordsDataSource(this);
        Recordsdatasource.open();

        LayoutInflater inflater = LayoutInflater.from(Record.this);
        View view = inflater.inflate(R.layout.activity_record, null);

        //set the listView to use the custom List Adapter
        records = (List<Records>) Recordsdatasource.getAll();
        RecordAdapter = new RecordAdapter(this, 0, (ArrayList<Records>) records);
        listview = (ListView) findViewById(android.R.id.list);
        listview.setAdapter(RecordAdapter);


        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectedRecord = records.get(position);
                AlertDialog.Builder a_builder = new AlertDialog.Builder(Record.this);
                a_builder.setMessage("Duty: " + selectedRecord.getType())
                        .setCancelable(false)
                        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                AlertDialog alert = a_builder.create();
                alert.setTitle(Html.fromHtml("<font color='#08ae9e'>Information</font>"));
                alert.show();


            }
        });


        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                selectedRecord = records.get(position);
                AlertDialog.Builder a_builder = new AlertDialog.Builder(Record.this);
                a_builder.setMessage("Are you sure you want to delete?")
                        .setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                deleteItem(selectedRecord);
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });
                AlertDialog alert = a_builder.create();
                alert.setTitle(Html.fromHtml("<font color='#08ae9e'>Alert!!</font>"));
                alert.show();
                return true;
            }
        });
        return view;
    }


    public void Addrecords() {
        NewButton = (Button) findViewById(R.id.NewButton);
        NewButton.setOnClickListener(new View.OnClickListener() {

           @Override
            public void onClick(View v) {
                Intent addrecords = new Intent(Record.this, NewRecord.class);
                startActivity(addrecords);
            }
        });
    }


    public static void showToast(Context context, String text) {
        Toast.makeText(context, text, Toast.LENGTH_LONG).show();
    }

    private void deleteItem(Records selectedRecord) {
        Recordsdatasource.removeRecords(selectedRecord);
        showToast(this, "Usage deleted");
        RecordAdapter.notifyDataSetChanged();
        RecordAdapter.notifyDataSetInvalidated();
        refreshDisplay();
    }


    private void refreshDisplay() {
        records = Recordsdatasource.getAll();
        RecordAdapter.clear();
        RecordAdapter.addAll(records);
    }




}

2 个答案:

答案 0 :(得分:1)

您正尝试从活动的onCreate()方法返回视图。

@Override
protected void onCreate(Bundle savedInstanceState) {

    LayoutInflater inflater = LayoutInflater.from(Record.this);
    View view = inflater.inflate(R.layout.activity_record, null);

    //...

    return view;
}

如您所见,Activity中的onCreate()具有void返回类型,这意味着您不应该在此方法中返回任何内容。

从代码的外观来看,您试图将此布局用作Activity的布局,但是您将View通胀代码与Fragment的onCreateView()方法中使用的代码混淆,确实需要您返回一个观点。

在活动中,您需要调用setContentView()来设置布局。您可以通过setContentView(R.layout.activity_record)使用资源ID设置布局,或者如果您想自己夸大视图,可以致电setContentView(view)

答案 1 :(得分:0)

来电return view实际上位于onCreate。声明函数onCreate不返回任何内容(void)。因此,您无法返回View的实例。您只需删除该行或将其替换为return;此外,onItemLongClick必须返回boolean。因此,您无法移动return view