我无法从 android 的片段中读取活动中的数据库信息
Posted
技术标签:
【中文标题】我无法从 android 的片段中读取活动中的数据库信息【英文标题】:I can't read database information in activity from fragment in android 【发布时间】:2018-03-18 05:31:57 【问题描述】:我是 android 编程的初学者,但我很难从片段中读取活动中的 sqlite
数据库信息。
这是我的片段活动:
package cm.mavis.easylife.les_fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import cm.mavis.easylife.ChoixRecherche;
import cm.mavis.easylife.DataHelper;
import cm.mavis.easylife.LectureInfos;
import cm.mavis.easylife.R;
public class FragmentRecherche extends Fragment
String[] liste;
protected Cursor cursor;
DataHelper dbcenter;
ListView listview;
public static Context fragmentRecherche;
public static FragmentRecherche newInstance()
FragmentRecherche fragment = new FragmentRecherche();
return fragment;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.fragment_recherche, container, false);
FloatingActionButton floatingActionButton = rootView.findViewById(R.id.fab);
floatingActionButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Intent intent = new Intent(getContext(), ChoixRecherche.class);
startActivity(intent);
);
listview = rootView.findViewById(R.id.liste_recherche);
fragmentRecherche = getActivity();
dbcenter = new DataHelper(getActivity());
RefreshList();
return rootView;
public void RefreshList()
SQLiteDatabase database = dbcenter.getReadableDatabase();
cursor = database.rawQuery("select * from cni_perdu", null);
liste = new String[cursor.getCount()];
cursor.moveToFirst();
for (int cc = 0; cc < cursor.getCount(); cc++)
cursor.moveToPosition(cc);
liste[cc] = cursor.getString(1);
listview.setAdapter(new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, liste));
listview.setSelected(true);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3)
final String selection = liste[arg2];
final CharSequence[] dialogitem = "Voir", "Suprimer";
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Option");
builder.setIcon(R.drawable.mes_voyage);
builder.setItems(dialogitem, new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int item)
switch (item)
case 0:
Intent intent = new Intent(getActivity(), LectureInfos.class);
intent.putExtra("", selection);
startActivity(intent);
RefreshList();
break;
case 1:
SQLiteDatabase database = dbcenter.getWritableDatabase();
database.execSQL("delete from cni_perdu where prenom = '" + selection + "'");
dbcenter.close();
RefreshList();
break;
);
builder.create().show();
);
((ArrayAdapter)listview.getAdapter()).notifyDataSetInvalidated();
@Override
int id = item.getItemId();
if (id == R.id.action_settings)
return super.onOptionsItemSelected(item);
这就是我的活动:
package cm.mavis.easylife;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class LectureInfos extends AppCompatActivity
DataHelper dataHelper;
protected Cursor cursor;
TextView textView1, textView2, textView3, textView4;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lecture_infos);
dataHelper = new DataHelper(this);
textView1 = (TextView) findViewById(R.id.textview1);
textView2 = (TextView) findViewById(R.id.textview2);
textView3 = (TextView) findViewById(R.id.textview3);
textView4 = (TextView) findViewById(R.id.textview4);
SQLiteDatabase database = dataHelper.getReadableDatabase();
cursor = database.rawQuery("SELECT * FROM cni_perdu where nom = '" +
getIntent().getStringExtra("nom") + "'", null);
cursor.moveToFirst();
if (cursor.getCount() > 0)
cursor.moveToPosition(0);
textView1.setText(cursor.getString(1));
textView2.setText(cursor.getString(2));
textView3.setText(cursor.getString(3));
textView4.setText(cursor.getString(4));
当我执行我的应用程序时,我得到了这个结果
但我想得到这个结果:
请帮帮我!
编辑:如果我重复了一个主题,但经过多次搜索后我没有找到积极的结果,请原谅。如果我的英语很差,也请原谅我是说法语的;
谨此!
更新
这是我数据库的代码
package cm.mavis.easylife;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataHelper extends SQLiteOpenHelper
private static final String DATABASE_NAME = "piece_perdu.db";
private static final int DATABASE_VERSION = 3;
public DataHelper(Context context)
super(context, DATABASE_NAME,null,DATABASE_VERSION);
public void onCreate(SQLiteDatabase database)
String sql = "create table if not exit cni_perdu(nom text null, prenom text null, ville text null, quartier text null);";
Log.d("Data", "onCreate: "+sql);
database.execSQL(sql);
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2)
【问题讨论】:
是 nom, ville 是你表的列名吗? 代替 cursor.moveToPosition(0);使用 cursor.moveToFirst(); 我有同样的结果。 【参考方案1】:在 FragmentRecherche 类中将您的 intent.putExtra("", selection);
更改为 intent.putExtra("nom", selection);
记住
intent.putExtra()
始终采用键值对的形式。如果你想从intent
获取数据,你必须在getIntent().getStringExtra()
中使用与intent.putExtra()
相同的key
例如-:
从活动一中发送带有意图的附加内容
Intent intent = new Intent(Main.this, Second.class);
intent.putExtra("foo", message);
startActivity(intent);
在第二个活动中从意图中获取额外内容
String msg = getIntent().getStringExtra("foo");
现在,您的代码必须是您的 FragmentRecherche
Intent intent = new Intent(getActivity(), LectureInfos.class);
intent.putExtra("nom", selection);
startActivity(intent);
RefreshList();
break;
【讨论】:
当我改变这个时我有同样的结果。 检查您是否使用日志从数据库中获取数据。我认为问题出在您的数据库代码中 我在另一个活动中使用这个数据库并且它工作。我认为问题出在我的片段活动中。【参考方案2】: Intent intent = new Intent(getActivity(), LectureInfos.class);
intent.putExtra("nom", selection);
startActivity(intent);
RefreshList();
break;
您没有传递 putExtra 参数名称。
试试
intent.putExtra("nom", selection);
在你的 DBHelper 类中
public void onCreate(SQLiteDatabase database)
String sql = "create table if not exists cni_perdu(nom text null, prenom text null, ville text null, quartier text null);";
Log.d("Data", "onCreate: "+sql);
database.execSQL(sql);
你的语法错误。你写的是“exit”而不是“exists”
【讨论】:
以上是关于我无法从 android 的片段中读取活动中的数据库信息的主要内容,如果未能解决你的问题,请参考以下文章
设置onclick侦听器在android中无法从一个片段移动到另一个活动[重复]