private ArrayList<Sticky_notes> notes;
private ArrayAdapter<Sticky_notes> my_adapter;
my_adapter = new ArrayAdapter<Sticky_notes>(this,
android.R.layout.simple_list_item_1,
notes);
list_view.setAdapter(my_adapter);
This will display all the fields in my database. But I wanted to show only fields that are relevant. This is what my data class looks like
public class Sticky_notes
{
private String row_id;
private String title;
private String note;
public Sticky_notes (String row_id, String title, String note)
{
super();
this.row_id = row_id;
this.title = title;
this.note = note;
}
public String get_title()
{
return title;
}
public String get_note()
{
return note;
}
}
private static class Note_Adapter extends ArrayAdapter<Sticky_notes>
{
public Note_Adapter(Activity context, ArrayList<Sticky_notes> data)
{
// The layout containing the listView where the data is going to end up.
super(context, R.layout.activity_sticky, data);
}
@Override public View getView(int position, View convertView, ViewGroup parent)
{
}
}
The listView typically has far more data than the number of displayed rows. When the list is scrolled then the rows and their associated data are out of the visible area. The objects for the rows can be reused for the newly visible rows. This is what the getView method is for. This method will use a local static class that will hold the view data, I have called this class Note_holder.
private static class Note_Adapter extends ArrayAdapter<Sticky_notes>
{
// The class for holding the view data
private static class Note_Holder
{
TextView txtNote;
TextView txtTitle;
}
public Note_Adapter(Activity context, ArrayList<Sticky_notes> data)
{
// The layout containing the listView where the data is going to end up.
super(context, R.layout.activity_sticky, data);
}
@Override public View getView(int position, View convertView, ViewGroup parent)
{
}
}
private static class Note_Adapter extends ArrayAdapter<Sticky_notes>
{
private static class Note_Holder
{
TextView txtNote; // The data is made up of Note and Title
TextView txtTitle;
}
public Note_Adapter(Activity context, ArrayList<Sticky_notes> data)
{
// The layout containing the listView where the data is going to end up.
super(context, R.layout.activity_sticky, data);
}
@Override public View getView(int position, View convertView, ViewGroup parent)
{
Note_Holder holder; // The viewed data
if (convertView == null)
{
// There was not existing object, so inflate the xml into it.
holder = new Note_Holder();
// Inflate the layout containing the textViews that I want to
// add to the listView
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.note_item, parent, false);
holder.txtTitle = (TextView) convertView.findViewById(R.id.item_title);
holder.txtNote = (TextView) convertView.findViewById(R.id.item_note);
//The convertView has been used, so set the tag with the view information
convertView.setTag(holder);
}
else
{
// We have a convertView, so get the view information from the tag.
holder = (Note_Holder) convertView.getTag();
}
// Fill the Note_holder class with data that has been passed from the listView
Sticky_notes temp_data = getItem(position);
String title = temp_data.get_title(); //The get methods from the data class.
String note = temp_data.get_note();
holder.txtTitle.setText("\n " + title);
holder.txtNote.setText(" " + note);
return convertView;
}
}
/* my_adapter = new ArrayAdapter<Sticky_notes>(this,
android.R.layout.simple_list_item_1,
notes);
*/
my_adapter = new Note_Adapter(this, notes);
list_view.setAdapter(my_adapter);
When the variable 'notes' has been populated with the contents of your database the new adapter will display it to the available amount of screen. When you create your onClickListener to enable editing of your item you can use this adapter to access your data:
list_view.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//Use the adapter to get the item at 'position'
String note_body = my_adapter.getItem(position).get_note();
//This will edit the clicked item.
Intent i = new Intent("app.simple_sticky.EDIT_NOTE");
i.putExtra("note", note_body);
startActivity(i);
}
});
my_adapter.clear(); // empty the adapter
my_adapter.addAll(notes); // Load it up with the updated arraylist.
list_view.setAdapter(my_adapter);
This will display your data from the database and, more importantly, refresh the display after editing without having to reload it from the database.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)