I am trying to get information from my SQLite database and display it as a list view in my android activity. I can see the entries in a text view in one activity but needed it in a list view in a separate activity, it creates a space where each entry is but it does not display text. When I create a new entry it creates a new space in the list but there is still no text. I was following tutorials but no one else seemed to have this issue in the comments. I get an error that says 'SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length' is this what is causing the problem?
This is my class file that creates the database
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;
}
}
This is my java class file
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;
}
}
And this is the activity xml file
<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>
The xml for my list view
<?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>
In getAllDrugs()
, didn't you mean:
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));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.