我想将Sqlite
数据显示在我的ListView
中,但我不能这样做。
现在它可以显示Toast
但是如何将其运行到我的ListView
?
谢谢。
public class fehrest extends Activity {
public String fonts="BNazanin.ttf";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// DATABASE START
DBAdapter db = new DBAdapter(this);
try {
String destPath = "/data/data/" + getPackageName() + "/databases";
File f = new File(destPath);
if (!f.exists()) {
f.mkdirs();
f.createNewFile();
//---copy the db from the assets folder into
// the databases folder---
CopyDB(getBaseContext().getAssets().open("mydb"),
new FileOutputStream(destPath + "/MyDB"));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//---get all contacts---
db.open();
Cursor c = db.getAllContacts();
if (c.moveToFirst()) {
do {
DisplayContact(c);
} while (c.moveToNext());
}
db.close();
//DATABASE END
setFace();
//Tab2 contents
}
//DATABASE COPY FILES
public void CopyDB(InputStream inputStream,
OutputStream outputStream) throws IOException {
//---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
// DATABASE COPY FILES END
// DATABASE SHOW INFO
public void DisplayContact(Cursor c) {
Toast.makeText(this,
"id: " + c.getString(0) + "\n" +
"Name: " + c.getString(1) + "\n" +
"Email: " + c.getString(2),
Toast.LENGTH_LONG).show();
}
// DATABASE SHOW INFO END
答案 0 :(得分:0)
您可以执行以下操作。您还可以使用CustomAdapter。对于示例,我使用了SimpleAdapter
。
http://developer.android.com/reference/android/widget/SimpleAdapter.html
ListView lv= (ListView)findViewById(R.id.listview);
//listview in main.xml
String[] from = new String[] {"id", "name", "email"};
int[] to = new int[] { R.id.textView1, R.id.textView2, R.id.textView3};
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
db.open();
Cursor c = db.getAllContacts();
if(c.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
String id = c.getString(0);
String name= c.getString(1);
String email = c.getString(2);
map.put("id",id);
map.put("name",name);
map.put("email",email);
fillMaps.add(map);
} while (c.moveToNext());
}
db.close();
}
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.mylayout, from, to);
lv.setAdapter(adapter);
mylayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="50dp"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_marginRight="23dp"
android:text="TextView" />
</RelativeLayout>
您也可以使用光标适配器,在这种情况下可能更合适
String[] from = new String[] {"id", "name", "email"}; // columns
int[] to = new int[] { R.id.textView1, R.id.textView2, R.id.textView3};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.mylayout, c, from, to);
注意:
SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
在API级别11中不推荐使用此构造函数。不建议使用此选项,因为它会导致在应用程序的UI线程上执行Cursor查询,从而导致响应能力较差甚至应用程序无响应错误。作为替代方案,请将LoaderManager
与CursorLoader
一起使用。