我正在尝试从我的SQLite数据库中获取信息,并在我的android活动中将其显示为列表视图。我可以在一个活动的文本视图中看到条目,但是在一个单独的活动的列表视图中需要它,它创建一个空格,每个条目都是,但它不显示文本。当我创建一个新条目时,它会在列表中创建一个新空间,但仍然没有文本。我正在关注教程,但在评论中似乎没有其他人有这个问题。我得到一个错误,说“SPAN_EXCLUSIVE_EXCLUSIVE跨度不能有一个零长度”这是导致问题的原因吗?
这是我创建数据库的类文件
package com.example.medicalpp;
import java.util.ArrayList;
import java.util.HashMap;
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;
public class Drugs {
// set up how the database will look
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "_drug_name";
public static final String KEY_DOSE = "_drug_dose";
public static final String KEY_TIME = "_drug_time";
public static final String KEY_INFO = "_drug_info";
private static final String DATABASE_NAME = "DrugList";
private static final String DATABASE_TABLE = "drugsTable";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// Create the database
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
+ " TEXT NOT NULL, " + KEY_DOSE + " TEXT NOT NULL, "
+ KEY_TIME + " TEXT NOT NULL, " + KEY_INFO
+ " TEXT NOT NULL)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop table if updated
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public Drugs(Context c) {
ourContext = c;
}
public Drugs open() throws SQLException {
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
public ArrayList<HashMap<String, String>> getAllDrugs(){
ArrayList<HashMap<String, String>> drugArrayList = new ArrayList<HashMap<String, String>>();
String [] columne = new String[] { KEY_ROWID, KEY_NAME, KEY_DOSE };
Cursor cursor = ourDatabase.query(DATABASE_TABLE, columne, null, null, null,
null, null);
if(cursor.moveToFirst()){
do{
HashMap<String, String> drugMap = new HashMap<String, String>();
drugMap.put("KEY_ROWID", cursor.getString(0));
drugMap.put("KEY_NAME", cursor.getString(1));
drugMap.put("KEY_DOSE", cursor.getString(2));
drugArrayList.add(drugMap);
} while(cursor.moveToNext());
}
return drugArrayList;
}
}
这是我的java类文件
package com.example.medicalpp;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class AddNew extends ListActivity {
Intent intent;
TextView tvDrugID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new);
Drugs drugName = new Drugs(this);
drugName.open();
ArrayList<HashMap<String, String>> drugList = drugName.getAllDrugs();
if(drugList.size() != 0){
ListView listview = getListView();
listview.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int arg2, long arg3) {
tvDrugID = (TextView) view.findViewById(R.id.tvDrugID);
String drugIDValue = tvDrugID.getText().toString();
Intent theIntent = new Intent("com.example.medicalpp.EDITDRUGS");
theIntent.putExtra("tvDrugID", drugIDValue);
startActivity(theIntent);
}
});
ListAdapter adapter = new SimpleAdapter(
AddNew.this, drugList, R.layout.item_layout,
new String[] {"tvDrugID", "tvDrugName", "tvDrugDose"},
new int[] {R.id.tvDrugID, R.id.tvDrugName, R.id.tvDrugDose});
setListAdapter(adapter);
}
drugName.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add_new, menu);
return true;
}
}
这是活动xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".AddNew" >
<TextView
android:id="@+id/drugName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/choose" />
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
我的列表视图的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvDrugID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="16dp"
android:visibility="gone"
/>
<TextView
android:id="@+id/tvDrugName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="16dp"
android:text="@string/name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tvDrugDose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvDrugName"
android:layout_alignBottom="@+id/tvDrugName"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/tvDrugName"
android:text="@string/dose"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
答案 0 :(得分:0)
在getAllDrugs()
,你的意思不是:
HashMap<String, String> drugMap = new HashMap<String, String>();
drugMap.put("tvDrugID", cursor.getString(0));
drugMap.put("tvDrugName", cursor.getString(1));
drugMap.put("tvDrugDose", cursor.getString(2));