如何从sqlite获取值并在edittext中显示它

时间:2015-04-11 04:43:01

标签: android sqlite android-edittext cursor

这是我的类DataBaseManager中的mi函数游标

 public Cursor cargarid()
{
    String [] columna=new String[]{columna_ID_producto};

    return db.query(Table_Name_productos,columna,null,null,"desc","LIMIT","1");

}

现在我怎样才能得到结果。 我需要在我的片段中得到结果。

 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

     manager = new DataBaseManager(getActivity());
     cursor=manager.cargarid();
 final EditText clave = (EditText) getActivity().findViewById(R.id.et_clave);e
clave.settext("the value from sqlite??");

}

3 个答案:

答案 0 :(得分:0)

cursor = manager.cargarid();
cursor.moveToFirst();
final EditText clave = (EditText) getActivity().findViewById(R.id.et_clave);
clave.settext(cursor.getString(0));

答案 1 :(得分:0)

使用以下代码从sqlite获取值

 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

 manager = new DataBaseManager(getActivity());
 Cursor cursor=manager.cargarid();
 final EditText clave = (EditText)getActivity().findViewById(R.id.et_clave);

 if(cursor.getCount > 0)
 {
    cursor.moveToFirst();
    clave.settext("sql lite value" +cursor.getString(0));
 }

}

答案 2 :(得分:0)

在您的DatabaseManager类中创建如下的新方法:

public ArrayList getDataFromdatabase(){

    ArrayList array_list = new ArrayList();
    SQLiteDatabase db = this.getReadableDatabase();
    String [] columna=new String[]{columna_ID_producto};
    Cursor c = db.query(Table_Name_productos,columna,null,null,"desc","LIMIT","1");

    c.moveTofirst();

    while(c.isAfterLast() == false){
         array_list.add(c.getString(c.getColumnIndex(CONTACTS_COLUMN_NAME)));
          c.moveToNext();
     }
     return array_list;
  }

要从数据库获取所有数据并显示到列表视图中,您只需从片段中调用此方法并将其添加到自定义适配器中。

Arraylist list= databasemanager.getDataFromdatabase();