我是Android的初学者。请帮我解决这个简单的问题:从SQLite中检索一个数据。
案例1:
dbhelper.java
// Getting single part
public lispar Look(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor kr = db.query(TABLE_PART, new String[]{ID,
KODE, NAMA}, ID + "=?",
new String[]{String.valueOf(id)}, null, null, null, null);
if (kr != null)
kr.moveToFirst();
lispar contact = new lispar(Integer.parseInt(kr.getString(0)),
kr.getString(1), kr.getString(2));
// return contact
kr.close();
db.close();
return contact;
}
main.java
kodcari = (Button) findViewById(R.id.carikod);
kodcari.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
ckodin = kodin.getText().toString(); //wrong string?
parin.getText().toString();
//what should i do?
}
});
partnam = (EditText) findViewById(R.id.namepart);
dbHandle = new DbHandle(this);
}
案例2:
main.java ()
kodin = (EditText) findViewById(R.id.inkod);
kodcari = (Button) findViewById(R.id.carikod);
kodcari.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = mDbHelper.getReadableDatabase();
String[] projection = {
FeedEntry._ID,
FeedEntry.KODE,
FeedEntry.NAMPART,
FeedEntry.BELANG
};
// Filter results WHERE "kode" = 'kode part'
String selection = FeedEntry.KODE + " = ?";
String[] selectionArgs = parin.getText().toString(), belangin.getText().toString();//{ "KODE_PART" }; //
// How you want the results sorted in the resulting Cursor
String sortOrder = FeedEntry.KODE + " DESC";
Cursor kr = db.query(
FeedEntry.KODE, // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
if (kr != null)
kr.moveToFirst();
//String parin = kr.getString(
//kr.getColumnIndexOrThrow(FeedEntry._ID)
//);
}
});
main.xml中
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Looking by Code"
android:textSize="10pt"
android:id="@+id/txkod"
android:layout_marginTop="63dp"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="insert code"
android:textAlignment="center"
android:layout_marginTop="25dp"
android:id="@+id/inkod"
android:layout_below="@+id/txkod"
android:layout_alignLeft="@+id/txkod"
android:layout_alignStart="@+id/txkod"
android:layout_alignRight="@+id/txkod"
android:layout_alignEnd="@+id/txkod" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Look"
android:id="@+id/carikod"
android:layout_below="@+id/inkod"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/codepar"
android:hint="code part"
android:layout_below="@+id/namkod"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nampar"
android:hint="name part"
android:layout_below="@+id/nampar"/>
</LinearLayout>
插入数据是成功的,但是读取数据不是。 (基准意味着数据*)
答案 0 :(得分:0)
我不确定以下内容会让你感动。但似乎您没有获得 kodin 和 parin 视图的ID。所以也许尝试以下方法: -
kodin = (EditText) findViewById(R.id.inkod);
parin = (EditText) findViewById(R.id.nampar);
更详细的代码(未经测试和即时编写,因此可能有错误)
如果您正在使用Android Studio或IDE进行调试,那么使用它可能非常有用。
kodcari = (Button) findViewById(R.id.carikod);
// Adding the following lines may get the correct string
kodin = (EditText) findViewById(R.id.inkod); //????
parin = (EditText) findViewById(R.id.nampar); //????
kodcari.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
ckodin = kodin.getText().toString(); //wrong string?
parin.getText().toString();
//what should i do? Perhaps the following :-
lipsar myContact = Look(ckodin !!!!CONVERTED TO INT FROM STRING);
// whatever else you need to do with the mycontact .....
}
});
partnam = (EditText) findViewById(R.id.namepart);
dbHandle = new DbHandle(this);
}