vendredi 17 avril 2015

Android store SQLite db row id in custom ListView

I have an app that stores three strings in a SQLite db. In another activity I query the DB and populate a custom ListView with the strings. To do this I've implemented a BaseAdapter to manage the layout of the custom ListView.



public class DataAdapter extends BaseAdapter{
ArrayList<DictionaryItem> wordList = new ArrayList<DictionaryItem>();
LayoutInflater inflater;
Context context;

public DataAdapter(Context context, ArrayList<DictionaryItem> wordList) {
this.wordList = wordList;
this.context = context;
inflater = LayoutInflater.from(this.context); // only context can also be used
}

@Override
public int getCount() {
return wordList.size();
}

@Override
public Object getItem(int position) {
return wordList.get(position);
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;

if (convertView == null) {
convertView = inflater.inflate(R.layout.word_layout, null);
holder = new ViewHolder();
holder.tvEnglish = (TextView) convertView.findViewById(R.id.tvEnglish);
holder.tvSpanish = (TextView) convertView.findViewById(R.id.tvSpanish);
holder.tvSpanishFilename = (TextView) convertView.findViewById(R.id.tvSpanishFilename);

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvEnglish.setText(wordList.get(position).getEnglish());
holder.tvSpanish.setText(wordList.get(position).getSpanish());
holder.tvSpanishFilename.setText(wordList.get(position).getSpanishFilename());

return convertView;
}

static class ViewHolder {
TextView tvSpanish, tvEnglish, tvSpanishFilename;
}
}


What I would also like to do is store the row_id of the db entry in my ListView then retrieve the row_id if I wish to delete that entry from the database.


I've see posts relating to using setTag but I am not sure how to do this when using a ViewHolder as that method seems to address adding content to views in my layout. I don't consider row_id an item to display in my ListView.


Some insight would be appreciated.


Aucun commentaire:

Enregistrer un commentaire