Good day, please kindly help me review this code and tell me what i am doing wrong. My listview is not returning the correct row position. it keeps giving me the previous row entry and not the one clicked and it gives me the error "android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0" when i try to click on the first entry in the list.
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class ViewItems extends ListActivity implements OnItemClickListener{
DBAdapter adapter;
Cursor cursor;
ListView list;
static final int ACTIVITY_EDIT = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_list);
adapter = new DBAdapter(this);
adapter.open();
fillData();
list = (ListView)findViewById(android.R.id.list);
list.setOnItemClickListener(this);
}
protected void fillData() {
Cursor cursor = adapter.retrieveItems();
startManagingCursor(cursor);
String[] from = new String[]{DBAdapter.NAME, DBAdapter.DATE, DBAdapter.TIME};
int[] to = new int[]{R.id.viewNameId, R.id.viewDateId, R.id.viewTimeId};
customCursorAdapter items = new customCursorAdapter(this, R.layout.view_items, cursor, from, to);
setListAdapter(items);
}
public 开发者_StackOverflow社区class customCursorAdapter extends SimpleCursorAdapter {
private int layout;
Context context;
public customCursorAdapter(Context context, int layout, Cursor cursor, String[]from, int[] to) {
super(context, layout, cursor, from, to);
this.layout = layout;
this.context = context;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder;
if(view != null){
holder = new ViewHolder();
holder.viewName = (TextView)view.findViewById(R.id.viewNameId);
holder.viewStartDate = (TextView)view.findViewById(R.id.viewDateId);
holder.viewStartTime = (TextView)view.findViewById(R.id.viewTimeId);
view.setTag(holder);
}else{
holder = (ViewHolder)view.getTag();
}
int namecol = cursor.getColumnIndex(DBAdapter.NAME);
String name = cursor.getString(namecol);
if(holder.viewName != null){
holder.viewName.setText(name);
holder.viewName.setTextColor(Color.RED);
}
String startDate = cursor.getString(cursor.getColumnIndex(DBAdapter.DATE));
holder.viewStartDate.setText(startDate);
String startTime = cursor.getString(cursor.getColumnIndex(DBAdapter.TIME));
holder.viewStartTime.setText(startTime);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
final View view = inflater.inflate(layout, parent, false);
return view;
}
@Override
public long getItemId(int id){
return id;
}
@Override
public Object getItem(int position){
return position;
}
}
static class ViewHolder{
TextView viewName;
TextView viewStartDate;
TextView viewStartTime;
}
public void onItemClick(AdapterView parent, View view, int position, long rowId) {
cursor = adapter.retrieveRow(rowId);
Intent edit = new Intent(this,AddItem.class);
edit.putExtra(DBAdapter.KEY_ID, rowId);
edit.putExtra(DBAdapter.NAME, cursor.getString(cursor.getColumnIndex(DBAdapter.NAME)));
edit.putExtra(DBAdapter.DATE, cursor.getString(cursor.getColumnIndex(DBAdapter.DATE)));
edit.putExtra(DBAdapter.TIME, cursor.getString(cursor.getColumnIndex(DBAdapter.TIME)));
startActivityForResult(edit, ACTIVITY_EDIT);
}
}
I have tried and reviewed examples but no success. I have also tried to use OnListItemClick() method but that gives me a "NullPointerException", i don't know why. Its been so frustrating for some time now. Thanks in advance for your time.
精彩评论