我在尝试从SQLlite数据库显示数据时遇到问题,并在自定义列表视图中显示3textviews和1个切换按钮
主要活动:
public class PaymentReminderActivity extends ActionBarActivity {
ReminderDataSource datasource;
ReminderClass[] reminderObject;
ReminderClass reminder;
// Array of booleans to store toggle button status
public boolean[] status;
ListView lvReminder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment_reminder);
reminderObject = new ReminderClass[10000];
reminder = new ReminderClass();
datasource = new ReminderDataSource(this);
//GET ALL DATA
datasource.open();
final List<ReminderClass> values = datasource.getAllUsers();
if(values.isEmpty()!=false) {
for (int i = 0; i < values.size(); i++) {
reminder = values.get(i);
reminderObject[i].setType(reminder.getType());
reminderObject[i].setDesc(reminder.getDesc());
reminderObject[i].setDay(reminder.getDay());
}
}
datasource.close();
/** Restore from the previous state if exists */
if(savedInstanceState!=null){
status = savedInstanceState.getBooleanArray("status");
}
lvReminder = (ListView) findViewById(R.id.lv_countries);
registerForContextMenu(lvReminder);
AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> lv, View item, int position, long id) {
ListView lView = (ListView) lv;
SimpleAdapter adapter = (SimpleAdapter) lView.getAdapter();
HashMap<String,Object> hm = (HashMap) adapter.getItem(position);
/** The clicked Item in the ListView */
RelativeLayout rLayout = (RelativeLayout) item;
/** Getting the toggle button corresponding to the clicked item */
ToggleButton tgl = (ToggleButton) rLayout.getChildAt(2);
String strStatus = "";
if(tgl.isChecked()){
tgl.setChecked(false);
strStatus = "Off";
status[position]=false;
}else{
tgl.setChecked(true);
strStatus = "On";
status[position]=true;
}
Toast.makeText(getBaseContext(), (String) hm.get("txt") + " : " + strStatus, Toast.LENGTH_SHORT).show();
}
};
lvReminder.setOnItemClickListener(itemClickListener);
List<HashMap<String,Object>> aList = new ArrayList<HashMap<String,Object>>();
// if(reminderObject!=null) {
for (int i = 0; reminderObject[i+1]!=null ; i++) {
HashMap<String, Object> hm = new HashMap<String, Object>();
hm.put("txt", reminderObject[i].getType());
hm.put("txt2", reminderObject[i].getDesc());
hm.put("txt3", reminderObject[i].getDay()+" of every month");
hm.put("stat", status[i]);
aList.add(hm);
}
// }
// Keys used in Hashmap
String[] from = {"txt","txt2","txt3","stat" };
// Ids of views in listview_layout
int[] to = { R.id.tv_item,R.id.tv_item2, R.id.tv_item3, R.id.tgl_status};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.lv_layout, from, to);
lvReminder.setAdapter(adapter);
}
/** Saving the current state of the activity
* for configuration changes [ Portrait <=> Landscape ]
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBooleanArray("status", status);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_payment_reminder, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_add) {
Intent intent = new Intent();
intent.setClass(this, NewReminderActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.lv_countries) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_list, menu);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
long selectid = info.id; //_id from database in this case
int selectpos = info.position;
switch(item.getItemId()) {
case R.id.add:
// add stuff here
return true;
case R.id.edit:
// edit stuff here
return true;
case R.id.delete:
return true;
default:
return super.onContextItemSelected(item);
}
}
}
这是自定义列表视图的布局xml文件 lvlayout文件:
<?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">
<TextView
android:id="@+id/tv_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/tv_item2"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:paddingLeft="5dp"
android:layout_below="@+id/tv_item"
android:layout_alignParentStart="true" />
<ToggleButton
android:id="@+id/tgl_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:clickable="false"
android:layout_alignBottom="@+id/tv_item3"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/tv_item3"
android:layout_below="@+id/tv_item2"
android:layout_alignParentStart="true" />
</RelativeLayout>
在logcat上有一个错误,空指针异常指向代码的这一部分的错误,在onCreate方法的开头:
final List<ReminderClass> values = datasource.getAllUsers();
if(values.isEmpty()!=false) {
for (int i = 0; i < values.size(); i++) {
reminder = values.get(i);
reminderObject[i].setType(reminder.getType());
reminderObject[i].setDesc(reminder.getDesc());
reminderObject[i].setDay(reminder.getDay());
}
}
数据文件:
public class ReminderDataSource {
private SQLiteDatabase database;
private ReminderSQLHelper dbHelper;
private String[] allColumn = {
Reminder.COLUMN_TYPE,
Reminder.COLUMN_DESC,
Reminder.COLUMN_DAY
};
public ReminderDataSource(Context context){
dbHelper = new ReminderSQLHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close(){
dbHelper.close();
}
public void insertRecord(ReminderClass reminder){
ContentValues values = new ContentValues();
values.put(Reminder.COLUMN_TYPE, reminder.getType());
values.put(Reminder.COLUMN_DESC, reminder.getDesc());
values.put(Reminder.COLUMN_DAY, reminder.getDay());
database = dbHelper.getWritableDatabase();
database.insert(Reminder.TABLE_NAME, null, values);
database.close();
}
public List<ReminderClass> getAllUsers() {
List<ReminderClass> records = new ArrayList<ReminderClass>();
Cursor cursor = database.query(Reminder.TABLE_NAME, allColumn, null,
null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
ReminderClass reminder = new ReminderClass();
reminder.setType(cursor.getString(0));
reminder.setDesc(cursor.getString(1));
reminder.setDay(cursor.getString(2));
records.add(reminder);
cursor.moveToNext();
}
cursor.close();
return records;
}
}
谢谢。我感谢你的帮助
答案 0 :(得分:0)
好吧试试这个:
reminderObject = new ReminderClass[10000];
reminder = new ReminderClass();
// datasource = new ReminderDataSource(this); //remove this
//GET ALL DATA
datasource.open();
final List<ReminderClass> values = datasource(getApplicationContext()).getAllUsers();
if(values.isEmpty()!=false) {
for (int i = 0; i < values.size(); i++) {
reminder = values.get(i);
reminderObject[i].setType(reminder.getType());
reminderObject[i].setDesc(reminder.getDesc());
reminderObject[i].setDay(reminder.getDay());
}
}