我想用我数据库中的数据填充我的用户界面元素
读取数据库发生在我的数据库类(AbezoeAdapter)
上填充我的用户界面是由我的主类(Bezoekrapporten)
完成的现在我正在努力使用以下代码来实现我的想法
package com.example.deceunincktechniekers;
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.content.res.*;
import android.database.Cursor;
@SuppressLint("SimpleDateFormat") public class bezoekrapporten extends Activity
{
TextView controlelijn;
EditText scanzonedata;
String scanzonestring;
String sScan;
String Bezoeknummer;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.bezoekrapporten);
//////////////////////// here is some code missing///////////////////////////
private void vulbezoekrapportboxin(String bezoeknummer) {
ABezoeAdapter bezoe = new ABezoeAdapter(this);
TextView bezoekrapportnummer;
bezoekrapportnummer = (TextView) findViewById(R.id.boxbrBrnum);
TextView servicenummer;
servicenummer = (TextView) findViewById(R.id.boxbrServicenum);
TextView datum;
datum = (TextView) findViewById(R.id.boxbrBrdatum);
TextView klantnaam;
klantnaam = (TextView) findViewById(R.id.boxbrKlant);
TextView straatnaam;
straatnaam = (TextView) findViewById(R.id.boxbrAdres);
TextView gemeente;
gemeente = (TextView) findViewById(R.id.boxbrGemeente);
TextView machinenummer;
machinenummer = (TextView) findViewById(R.id.boxbrMachinenr);
TextView merk;
merk = (TextView) findViewById(R.id.boxbrMerk);
TextView serienummer;
serienummer = (TextView) findViewById(R.id.boxbrSerial);
if(bezoeknummer == null)
{
bezoekrapportnummer.setText("-----");
}
else
{
Cursor c =bezoe.leesgegevens(bezoeknummer);
if (c.moveToFirst()){
while(!c.isAfterLast()){
String data = c.getString(c.getColumnIndex("bezoekrapportdatum"));
controlelijn.setText(data);
c.moveToNext();
}
}
c.close();
bezoekrapportnummer.setText(bezoeknummer);
}
return;
}
和AbezoeAdapter的代码片段
package com.example.deceunincktechniekers;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.*;
import android.util.Log;
public class ABezoeAdapter extends ADbAdapter {
public static final String recordnummer = "RECNUM";
public static final String bezoekrapportnummer = "Z01";
public static final String bezoekrapportdatum = "Z02";
public static final String herstellingsoort = "Z03";
//////////////////here is some code missing///////////////////////////:
public static final String basisservicenummer = "Z27";
public static final String verzonden = "Z28";
public static final String[] allekolommen = new String[] {bezoekrapportnummer + " AS _id", bezoekrapportdatum,
herstellingsoort, totaleduur, servicenummer, ONBEKEND, klantnaam, adres, machinenummer, omschrijving,
duur, postcode, gemeente, merk, model, serienummer, opmerkingen, werkgereed, extratijd, urenstand, gecontroleerdbureel,
onderhoudsfiche, uitsplitsen, opmerkingbureel, ONBEKEND2, orderverwerkdatum, ordernummer, basisservicenummer, verzonden};
//////////////////////////////here is some code missing/////////////////////////////////////////////
public Cursor leesgegevens(String bezoeknummer) {
open();
Cursor c = onzedatabase.query(true, databasetabel, allekolommen,
"_id" + "=?", new String[] { bezoeknummer }, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
Log.i("cursor leesgegevens", c.toString());
sluit();
return c;
/////////////////////////////////////here is some code missing//////////////////
任何人都可以通过最佳方式告诉我将数据从数据库传输到我的用户界面吗?
如果我已经能够编译我的代码,我的logcat中会出现如下错误:
11-24 15:58:34.089: E/AndroidRuntime(785): FATAL EXCEPTION: main
11-24 15:58:34.089: E/AndroidRuntime(785): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.deceunincktechniekers/com.example.deceunincktechniekers.bezoekrapporten}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
答案 0 :(得分:0)
这是我如何做我的一个适配器。首先,这是执行查询的代码:
private void query (long uuid)
{
// Fill list using adapter
String cols[] = { Instances.TITLE }; // dummy field; we will set all fields together in ViewBinder
int fields[] = { R.id.node_text };
cursorAdapter = new CursorAdapter (this,
R.layout.row_view,
null, // no cursor yet
cols,
fields,
Adapter.NO_SELECTION);
cursorAdapter.setViewBinder (new MyViewBinder(this));
// Setup the adapter filter - this does the query work.
cursorAdapter.setFilterQueryProvider (new FilterQueryProvider()
{
@Override
public Cursor runQuery (CharSequence constraint)
{
Cursor cursor = getContentResolver().query (...your params...);
return cursor;
}
});
listView.setAdapter (cursorAdapter);
listView.setTextFilterEnabled (true);
// Force initial query - notice we setup an adapter with a null cursor.
cursorAdapter.getFilter().filter (null);
}
注意我没有子类化适配器。相反,我将ViewBinder子类化。通常,每次调用ViewBinder都会一次设置一个视图。我认为一起完成所有观点会更有效率。我是这样做的:
private class MyViewBinder implements ViewBinder
{
@Override
public boolean setViewValue (View view, Cursor cursor, int column)
{
// Do all columns in one pass.
if (column != SelectAndroidEvent.instancesTitleCol)
throw new IllegalStateException ("viewbinder should only be called for title; " +
"col: " + column);
// Find encompassing layout.
ViewGroup parentView = (ViewGroup)view.getParent();
// Get fields from parentView.
TextView titleView = (TextView)view;
TextView field2 = (TextView)parentView.findViewById (R.id.field2);
...
// set all fields together - easier.
view.setText (cursor.getString (column);
field2.setText (cursor.getString (FIELD2_COL);
...
return true;
}
}
希望这有帮助。