我是android编程的新手。你可以帮我用光标获取行吗。
使用此代码我可以获取所有行:
public Cursor getAllDataDays() {
SQLiteDatabase db = this.getWritableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_MYBUDGETWITHDAYS;
return db.rawQuery(selectQuery, null);
}
这是我获取特定行的代码:
public Cursor SelectAllDaysIntParent(int id) {
SQLiteDatabase db = this.getWritableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_MYBUDGETWITHDAYS + " WHERE" + KEY_PARENTID + " = " + id;
return db.rawQuery(selectQuery, null);
}
但它没有显示任何行。
请帮忙。谢谢。
Main_BudgetPlannerListViewAdapter.java
package caraga.tripgurufinal.adapter;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
import com.example.budgetplanner.R;
public class Main_BudgetPlannerListViewAdapter extends CursorAdapter{
@SuppressWarnings("deprecation")
public Main_BudgetPlannerListViewAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView planName = (TextView)view.findViewById(R.id.tv_PlanName);
TextView planDate = (TextView)view.findViewById(R.id.tv_PlanDate);
TextView planDays = (TextView)view.findViewById(R.id.tv_NumberOfDays);
TextView CreatedDateTime = (TextView)view.findViewById(R.id.tv_createdDateTime);
planDays.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1)))+ " Days");
planName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2))));
planDate.setText("Travel Date: " + cursor.getString(cursor.getColumnIndex(cursor.getColumnName(3))));
CreatedDateTime.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(4))));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.mybudgetplann_list, parent, false);
return retView;
}
注意:我想要一个包含Cursor
的方法,因为我的listview
自定义适配器需要光标。
答案 0 :(得分:0)
尝试更改此
planDays.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1)))+ " Days");
planName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2))));
planDate.setText("Travel Date: " + cursor.getString(cursor.getColumnIndex(cursor.getColumnName(3))));
CreatedDateTime.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(4))));
到这个
planDays.setText(cursor.getString(0)+ " Days");
planName.setText(cursor.getString(1));
planDate.setText("Travel Date: " + cursor.getString(2));
CreatedDateTime.setText(cursor.getString(3));