我有这个数据库,我需要填充listview,并且由于某种原因,它确实被填充,但不是来自数据库的实际值,它只返回我在数据库中的数据引用,我认为它是回到我的内存位置,我发布了我的DBHelper.java,我的ShirtsActivity.java将显示它和所需的xml文件。那么有谁知道为什么我没有得到实际值?
package ankitkaushal.app.healthysizing;
public class Item {
private String brand;
//private String item;
private String price;
private String store;
public Item() {
}
//public Item(String brand, String item, String price, String store) {
// super();
// this.brand = brand;
// this.item = item;
// this.price = price;
// this.store = store;
// }
public void setBrand(String brand) {
this.brand = brand;
}
//public void setItem(String item) {
// this.item = item;
//}
public void setPrice(String price) {
this.price = price;
}
public void setStore(String store) {
this.store = store;
}
public String getStore() {
return store;
}
public String getPrice() {
return price;
}
//public String getItem() {
// return item;
//}
public String getBrand() {
return brand;
}
}
package ankitkaushal.app.healthysizing;
import android.app.ActionBar;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.SearchView;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.*;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.logging.Handler;
public class shirtsActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shirts);
final DatabaseHelper dbhelper;
final ListView listView;
final ListAdapter shirtsAdapter;
dbhelper = new DatabaseHelper(getApplicationContext());
try {
dbhelper.createDataBase();
} catch (IOException e) {
e.printStackTrace();
}
listView = (ListView) findViewById(R.id.listViewShirts);
List<Item> shirtsList = dbhelper.getAllShirts();
if(shirtsList != null){
shirtsAdapter = new ArrayAdapter<Item>(getApplicationContext(),
android.R.layout.simple_list_item_1, android.R.id.text1,
shirtsList);
listView.setAdapter(shirtsAdapter);
}
}
}
package ankitkaushal.app.healthysizing;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class DatabaseHelper extends SQLiteOpenHelper {
public static String DB_PATH = "/data/data/ankitkaushal.app.healthysizing/databases/";
public static String DB_NAME = "HealthySizing";
public static final int DB_VERSION = 1;
public static final String TB_USER = "Shirts";
private SQLiteDatabase myDB;
private Context context;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
@Override
public synchronized void close(){
if(myDB!=null){
myDB.close();
}
super.close();
}
public List<String> getAllUsers() {
List<String> listUsers = new ArrayList<String>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor c;
try {
c = db.rawQuery("SELECT * FROM " + TB_USER , null);
if(c == null) return null;
String name;
c.moveToFirst();
do {
name = c.getString(5);
listUsers.add(name);
} while (c.moveToNext());
c.close();
} catch (Exception e) {
Log.e("tle99", e.getMessage());
}
db.close();
return listUsers;
}
public List<Item> getAllShirts() {
List<Item> shirtList = new ArrayList<Item>();
String query = "SELECT * FROM " + TB_USER; //query to get all the shirts
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
String price, brand, store;
if (cursor.moveToFirst()){
do {
Item item = new Item();
price = cursor.getString(2);
item.setPrice(price);
item.setPrice(cursor.getString(2));
item.setBrand(cursor.getString(1));
item.setStore(cursor.getString(3));
shirtList.add(item);
} while (cursor.moveToNext());
}
return shirtList;
}
public Cursor getShirtsData() {
String SQLQuery = "SELECT * FROM" + TB_USER;
return myDB.rawQuery(SQLQuery, null);
}
/***
* Open database
* @throws android.database.SQLException
*/
public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
myDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
/***
* Copy database from source code assets to device
* @throws java.io.IOException
*/
public void copyDataBase() throws IOException {
try {
InputStream myInput = context.getAssets().open(DB_NAME);
String outputFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outputFileName);
byte[] buffer = new byte[1024];
int length;
while((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
} catch (Exception e) {
Log.e("tle99 - copyDatabase", e.getMessage());
}
}
/***
* Check if the database doesn't exist on device, create new one
* @throws IOException
*/
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
Log.e("tle99 - create", e.getMessage());
}
}
}
// ---------------------------------------------
// PRIVATE METHODS
// ---------------------------------------------
/***
* Check if the database is exist on device or not
* @return
*/
private boolean checkDataBase() {
SQLiteDatabase tempDB = null;
try {
String myPath = DB_PATH + DB_NAME;
tempDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
Log.e("tle99 - check", e.getMessage());
}
if (tempDB != null)
tempDB.close();
return tempDB != null ? true : false;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#29A9D2"
android:weightSum="1"
android:id="@+id/shirt"
android:onClick="onClickSearch">
<SearchView
android:layout_width="352dp"
android:layout_height="wrap_content"
android:id="@+id/searchView3"
android:background="#ffffffff" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listViewShirts"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/shirtPrice"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/shirtBrand"
android:layout_below="@+id/shirtPrice"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/shirtStore"
android:layout_below="@+id/shirtBrand"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:1)
您未展开ArrayAdapter
,因此getView
的默认操作是在数组中的项目上调用toString()
。由于您对toString()
没有Item
方法,因此它会调用Object.toString()
来显示您正在查看的内存位置。
最终,您必须通过扩展ArrayAdapter
并覆盖getView()
来编写自定义适配器,但只是为了让您看到它有效,请覆盖您的toString()
Item
类并再次运行它。你应该看到一个改进。