从intent转换为mainactivity后更新游标

时间:2016-11-13 16:04:38

标签: android indexoutofboundsexception android-cursoradapter

所以我在MainActivity中有这个listview,每次主要活动开始或恢复时都会更新。 代码是

@Override public void onResume() { super.onResume(); Cursor array_list_patients = mydb.getAllPatientsDetails(); PatientAdapter arrayAdapter = new n PatientAdapter(this,array_list_patients); obj = (ListView)findViewById(R.id.listView1); obj.setAdapter(arrayAdapter); }

现在对于 MainActivity 中的列表视图,我有一个 setOnItemClickListener ,如下所示,它基本上显示另一个活动“ ModifyPatient ”中的数据。代码是:

    Cursor array_list_patients = mydb.getAllPatientsDetails();
    final PatientAdapter arrayAdapter = new  PatientAdapter(this,array_list_patients);
    obj = (ListView)findViewById(R.id.listView1);
    obj.setAdapter(arrayAdapter);
    obj.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
            Cursor cur = (Cursor) arrayAdapter.getItem(arg2);
            cur.moveToPosition(arg2);

            int id_To_Search = cur.getInt(cur.getColumnIndexOrThrow("id"));
            Bundle dataBundle = new Bundle();
            dataBundle.putInt("id", id_To_Search);

            Intent intent = new Intent(getApplicationContext(),ModifyPatient.class);

            intent.putExtras(dataBundle);
            startActivity(intent);
        }
    });

现在在 ModifyPatient 类中..我可以对数据库条目执行 Upate / Delete 操作。现在的问题是,当我执行删除操作并返回 MainActivity ,然后点击 listview 中的任何项目时,我会得到 OutOfBounds 光标异常。 ModifyPatient 类中当前的删除按钮代码如下:

        deleteButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            AlertDialog.Builder builder = new AlertDialog.Builder(ModifyPatient.this);
            builder.setMessage(R.string.deleteContact)
                    .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            mydb.deletePatient(id_To_Update);
                            Toast.makeText(getApplicationContext(), "Deleted Successfully",
                                    Toast.LENGTH_SHORT).show();
                            ModifyPatient.this.finish();
                        }
                    })
                    .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // User cancelled the dialog
                        }
                    });

            AlertDialog d = builder.create();
            d.setTitle("Are you sure");
            d.show();
        }

    });

这将删除该条目并返回到mainactivity,但是单击listview中的任何项目都会引发我之前提到的错误。 我已经做了一个小的解决方法,我已经替换了以下行:

ModifyPatient.this.finish();

Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);

但问题是,原始活动仍然在后台,并且分叉了新的MainActivity。因此,按下后退按钮,返回原始列表视图的原始活动。

如果有人可以帮我解决,如何更新游标,因为我无法在我的情况下实现swapcursor。我不确定我做错了什么。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

我设置我的变量&#34; arrayAdapter &#34;出现在主要活动中的 onResume onItemClickListener ,从本地全球,以及这就是诀窍。 新代码,其中 arrayAdapter 是全局的:

 public void onResume()
{
    super.onResume();
    Cursor array_list_patients = mydb.getAllPatientsDetails();
    arrayAdapter = new PatientAdapter(this,array_list_patients);
    obj = (ListView)findViewById(R.id.listView1);
    obj.setAdapter(arrayAdapter);
}

    arrayAdapter = new PatientAdapter(this,array_list_patients);
    obj = (ListView)findViewById(R.id.listView1);
    obj.setAdapter(arrayAdapter);
    obj.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
            Cursor cur = (Cursor) arrayAdapter.getItem(arg2);
            cur.moveToPosition(arg2);

            int id_To_Search = cur.getInt(cur.getColumnIndexOrThrow("id"));
            Bundle dataBundle = new Bundle();
            dataBundle.putInt("id", id_To_Search);

            Intent intent = new Intent(getApplicationContext(),ModifyPatient.class);
            intent.putExtras(dataBundle);
            startActivity(intent);
            arrayAdapter.notifyDataSetChanged();
        }
    });
相关问题