单击列表视图项时显示警告对话框时出错

时间:2015-03-07 04:46:27

标签: android sqlite android-alertdialog

单击列表视图项时显示警告对话框时出错。

代码如下所示..这是在android sqlite中完成的程序。检索到的数据显示在列表视图中,但警报对话框无法正常编辑和删除。

请帮我解释一下代码......

import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;

public class Display11 extends Activity {
    ListView listview;
    PopupWindow popup;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display1);

        listview=(ListView)findViewById(R.id.listView1);
        Intent in=getIntent();
        ArrayList<String> a=in.getStringArrayListExtra("Key");
        ArrayAdapter<String> dis=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,a);
        listview.setAdapter(dis);
        listview.setTextFilterEnabled(true);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,long rowId) {
                // TODO Auto-generated method stub

        ///public void OnItemClick(AdapterView<?> parent, View view, int position,long rowId){
                int itemposition = position;
                String itemValue = (String)listview.getItemAtPosition(position); 
                // TODO Auto-generated method stub



                AlertDialog.Builder alert  = new AlertDialog.Builder(getApplicationContext());
                /*LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
                View view1 =inflater.inflate(R.layout.alert_dialog,null);
                alert.setView(view1);*/
                //ad.setView(inflater.inflate(R.layout.alert_dialog, null));
                popup = new PopupWindow(getBaseContext());
                //popup.setContentView(alert_)
                alert.setMessage("Database Manager");

                alert.setIcon(R.drawable.ic_launcher);
                alert.setTitle("Terms of Service");

                alert.setPositiveButton("edit",new DialogInterface.OnClickListener(){

                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub
                        Intent positiveActivity=new Intent(getApplicationContext(),com.cerebtec.contactdb.PositiveActivity.class);
                    startActivity(positiveActivity);
                    }});

                alert.setNegativeButton("delete",new DialogInterface.OnClickListener(){

                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Intent negativeActivity=new Intent(getApplicationContext(),com.cerebtec.contactdb.NegativeActivity.class);
                    startActivity(negativeActivity);
                    }});
                //ad.setNeutralButton("Cancel",null);
                //ad.setCancelable(false);


                //AlertDialog alert=ad.create();
                //alert.setView(view1);
                alert.create();
                alert.show();
            }});
        }}

1 个答案:

答案 0 :(得分:1)

您的问题出在以下代码中:

AlertDialog.Builder alert = new AlertDialog.Builder(getApplicationContext());

应用程序是UI内容的错误上下文,您需要该活动。你需要使用它:

AlertDialog.Builder alert = new AlertDialog.Builder(Display11.this);