我想将我的数据库的所有列放在listview中,广告我想使用Hashmap来制作列,但是我不知道在哪里放置一个循环来制作地图的新对象把它放在一个列表视图中,当我运行我的应用程序时,只显示第一列,似乎还没有创建其他地图!请你帮我吗?谢谢提前!
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#86868a"
>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/CodeFont"
>
</ListView>
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/DeleteSelectedGoodsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/DeleteSelectedGoods"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"/>
<Button
android:id="@+id/ConfirmDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ConfirmDeleteButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
<Button
android:id="@+id/CancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/CancelButton"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
</LinearLayout>
grids.xml
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:orientation="horizontal"
>
<TextView
android:id="@+id/GOOD_ID_CELL"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="@+id/CART_ID_CELL"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="@+id/GOOD_NAME_CELL"
android:layout_width="60dip"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="@+id/GOOD_UNITPRICE_CELL"
android:layout_width="60dip"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="@+id/QUANTITY_CELL"
android:layout_width="60dip"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
提供SQLHelper:
package com.example.nfc;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class ExternalDbOpenHelper extends SQLiteOpenHelper {
//-----------------------------------------------------
public static String DB_PATH;
//-----------------------------------------------------
public static String DB_NAME;
public SQLiteDatabase database;
public final Context context;
/////Adapted From spinner
/** A constant, stores the the table name */
private static final String TABLE_NAME = "Tbl_Goods";
private static final String GOOD_ID = "Good_ID";
private static final String CART_ID = "Cart_ID";
private static final String GOOD_NAME = "Good_Name";
private static final String GOOD_UNITPRICE = "Good_UnitPrice";
private static final String QUANTITY = "Quantity";
////---------------------------
public SQLiteDatabase getDb() {
return database;
}
public ExternalDbOpenHelper(Context context, String databaseName) {
super(context, databaseName, null, 1);
this.context = context;
//-------------------------------------------------
String packageName = context.getPackageName();
DB_PATH = String.format("//data//data//%s//databases//", packageName);
DB_NAME = databaseName;
openDataBase();
}
//--------------------------------------------------------------------------------
public void createDataBase() {
boolean dbExist = checkDataBase();
if (!dbExist) {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
Log.e(this.getClass().toString(), "Copying error");
throw new Error("Error copying database!");
}
} else {
Log.i(this.getClass().toString(), "Database already exists");
}
}
//--------------------------------------------------------------
private boolean checkDataBase() {
SQLiteDatabase checkDb = null;
try {
String path = DB_PATH + DB_NAME;
checkDb = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLException e) {
Log.e(this.getClass().toString(), "Error while checking db");
}
//---------------------------------------------------
if (checkDb != null) {
checkDb.close();
}
return checkDb != null;
}
//-----------------------------------------------------
private void copyDataBase() throws IOException {
// ----------------------------------------------
//------------------- assets----------------------
InputStream externalDbStream = context.getAssets().open(DB_NAME);
// ----------------------------------------------------------
String outFileName = DB_PATH + DB_NAME;
// -------------------------------------------------
OutputStream localDbStream = new FileOutputStream(outFileName);
//------------------------------------
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = externalDbStream.read(buffer)) > 0) {
localDbStream.write(buffer, 0, bytesRead);
}
// -----------------------------
localDbStream.close();
externalDbStream.close();
}
public SQLiteDatabase openDataBase() throws SQLException {
String path = DB_PATH + DB_NAME;
if (database == null) {
createDataBase();
database = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READWRITE);
}
return database;
}
@Override
public synchronized void close() {
if (database != null) {
database.close();
}
super.close();
}
////-----------------------Adapted From SQLSpinner
/** Inserts a new contact to the table contacts */
public long insert(ContentValues contentValues){
long rowID = database.insert(TABLE_NAME, null, contentValues);
return rowID;
}
/** Updates a contact */
public int update(ContentValues contentValues,String contactID){
int cnt = database.update(TABLE_NAME, contentValues, "_id=" + contactID, null);
return cnt;
}
/** Deletes a contact from the table */
public int del(String contactID){
int cnt = database.delete(TABLE_NAME, "_id="+contactID, null);
return cnt;
}
/** Returns all the contacts in the table */
public Cursor getAllContacts(){
return database.query(TABLE_NAME, new String[] { GOOD_ID, CART_ID , GOOD_NAME,GOOD_UNITPRICE,QUANTITY } , null, null, null, null, GOOD_NAME + " asc ");
}
/** Returns a contact by passing its id */
public Cursor getContactByID(String contactID){
return database.query(TABLE_NAME, new String[] { GOOD_ID, CART_ID , GOOD_NAME,GOOD_UNITPRICE,QUANTITY} , "_ID="+contactID, null, null, null, GOOD_NAME + " asc ");
}
////-----------------------------------
@Override
public void onCreate(SQLiteDatabase db) {}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
}
Main.java:
package com.example.nfc;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class PrepopSqliteDbActivity extends ListActivity {
private static final String DB_NAME = "NFC.sqlite";
//------------------------------------------------------------
private static final String TABLE_NAME = "Tbl_Goods";
private static final String GOOD_ID = "Good_ID";
private static final String CART_ID = "Cart_ID";
private static final String GOOD_NAME = "Good_Name";
private static final String GOOD_UNITPRICE = "Good_UnitPrice";
private static final String QUANTITY = "Quantity";
private SQLiteDatabase database;
private ListView listView;
//private ArrayList<String> goods;
///Adapted from MultiLevel
ArrayList<HashMap<String, String>> goods=new ArrayList<HashMap<String,String>>();
HashMap<String, String> map;
//////-----------------Adapted From SQLSpinner
ArrayAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table_row);
//-------------------------------------------------------
ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
database = dbOpenHelper.openDataBase();
//--------------------------------------
fillgoods();
setUpList();
//////Cancel Button
Button btnCancel=(Button)findViewById(R.id.CancelButton);
btnCancel.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
finish();
}
});
}
private void setUpList() {
///////////////////////////////////Multi
//SimpleAdapter mSchedule=new SimpleAdapter(this, goods, R.layout.grids,
//new String[]{"train","from","to"},
//new int[]{R.id.TRAIN_CELL,R.id.FROM_CELL,R.id.TO_CELL});
setListAdapter(new SimpleAdapter(this, goods, R.layout.grids,
new String[]{GOOD_ID,CART_ID,GOOD_NAME,GOOD_UNITPRICE,QUANTITY},
new int[]{R.id.GOOD_ID_CELL,R.id.CART_ID_CELL,R.id.GOOD_NAME_CELL,R.id.GOOD_UNITPRICE_CELL,
R.id.QUANTITY_CELL}));
//---------------------------------layout------------------------------------
//setListAdapter(new ArrayAdapter<HashMap<String, String>>(this,
// android.R.layout.simple_list_item_single_choice, goods));
listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
//---------------------------------------------------------------------------
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position,long id) {
Toast.makeText(getApplicationContext(),
((TextView) view).getText().toString(),
Toast.LENGTH_SHORT).show();
}
});
}
//--------------------------------------------------------------------
private void fillgoods() {
goods = new ArrayList<HashMap<String, String>>();
Cursor goodCursor = database.query(TABLE_NAME,
new String[]
{GOOD_ID,CART_ID,GOOD_NAME,GOOD_UNITPRICE,QUANTITY},
null, null, null, null
, GOOD_NAME);
String[] g=new String[]
{GOOD_ID,CART_ID,GOOD_NAME,GOOD_UNITPRICE,QUANTITY};
goodCursor.moveToFirst();
if(!goodCursor.isAfterLast()) {
do {
int i=0;
String name1 = goodCursor.getString(i+1);
map=new HashMap<String, String>();
map.put(g[i],name1);
goods.add(map);
i=i+1;
} while (goodCursor.moveToNext());
}
//goods.add(map);
goodCursor.close();
}
//////////////Home And Back Button
@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
BackToMainIntent();
else if(keyCode==KeyEvent.KEYCODE_BACK)
{
BackToMainIntent();
}
return false;
}
public void BackToMainIntent()
{
Intent intent = new Intent(this, Main.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
答案 0 :(得分:0)
如果您只是在循环中遇到问题,请将旧代码更改为此代码。
在do-while循环之前声明“i”以获取所有行。您已在do-while循环中声明它,这就是它仅返回第一行的原因。
if(!goodCursor.isAfterLast()) {
int i=0; //declare "i" here.
do {
//int i=0;
String name1 = goodCursor.getString(i+1);
map=new HashMap<String, String>();
map.put(g[i],name1);
goods.add(map);
i=i+1;
} while (goodCursor.moveToNext());
}
希望这会给你一些关于如何循环记录的提示。