Ok, I have been searching thick and thin, and I am having some issues implementing a BaseAdapter.
I have been able to implement a Simple Cursor Adapter http://code.google.com/android/samples/ApiDemos/src/com/example/android/apis/view/List7.html as per the example above.
There is a pretty good BaseAdapter example here : List14 google example
I am wanting to create my own List Adapter using BaseAdapter to show a listView, with multiple items from a Database. I know this can be done using the Simple Cursor Adapter, but I am looking to handle rows differently, so I want to be able to draw each row by overriding getView. The data would be pulled from a cursor.
I know this code is ugly for getting to the cursor data, but assuming I have populated a cursor. What suggestions do you have on this if column 8 contains the image resource id. :
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
cursor.moveToPosition(position);
ImageView i = new ImageView(mContext);
i.setImageResource(cursor.getShort(8));
i.setAdjustViewBounds(true);
i.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return i;
}
Do you have any good examples of a BaseAdapter being drawn using a cursor?
-
Can someone offer me some insight to redraw the list view? On a delete, I want it to rebuild.
Chris.
public void onCreate(Bundle bundle) { super.onCreate(bundle); //setContentView(R.layout.findlist); //getListView().setEmptyView(findViewById(R.id.empty)); mDbHelper = new DBHelper(this); mDbHelper.open(); cursor = mDbHelper.fetchAllLocations(); startManagingCursor(cursor); mAdapter = new myListAdapter(this); setListAdapter(mAdapter); } public class myListAdapter extends BaseAdapter { public myListAdapter(Context c) { ... public int getCount() { ... public Object getItem(int position) { ... public long getItemId(int position) { ... @Override public View getView(int position, View convertView, ViewGroup parent) { cursor.moveToPosition(position); RowView rv; if (convertView == null) { rv = new RowView(mContext,(cursor.getString(2)), (cursor.getString(5)), position); } else { rv = (RowView) convertView; rv.setTitle(cursor.getString(2)); rv.setDialogue(cursor.getString(5)); rv.setFocusable(true); rv.setClickable(true); } return rv; } } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); // Do what you will need to w/ a List Row Click Intent h = new Intent(FindList.this,showmaplocation.class); startActivityForResult(h,SAVE_TABS); } private class RowView extends LinearLayout { public RowView(Context context, String title, String words, int position) { super(context); this.setOrientation(VERTICAL); // Here we build the child views in code. They could also have // been specified in an XML file. mTitle = new TextView(context); mTitle.setText(title); addView(mTitle, new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); mDialogue = new TextView(context); mDialogue.setText(words); addView(mDialogue, new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); Button mButton = new Button(context); mButton.setFocusable(false); mButton.setId(position); addView(mButton, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); currentPosition = position; Button button = (Button)findViewById(position); button.setOnClickListener(mDeleteListener); } private OnClickListener mDeleteListener = new OnClickListener() { public void onClick(View v) { // To send a result, simply call setResult() before your // activity is finished, building an Intent with the data // you wish to send. Intent data = new Intent(); data.setAction("Corky!"); setResult(RESULT_OK, data); // finish(); cursor.moveToPosition(v.getId()); mDbHelper.deleteLocation(cursor.getInt(0)); // mAdapter.remove() mAdapter.notifyDataSetChanged(); setListAdapter(mAdapter); Toast mToast; mToast = Toast.makeText(FindList.this, "Deleted" , Toast.LENGTH_LONG); mToast.show(); } }; /** * Convenience method to set the title of a SpeechView */ public void setTitle(String title) { mTitle.setText(title); } /** * Convenience method to set the dialogue of a SpeechView */ public void setDialogue(String words) { mDialogue.setText(words); } private TextView mTitle; private TextView mDialogue; } }
0 comments:
Post a Comment