Good Day everyone!
I am new to android programming, struggling on how to attach _id of database to the listview adapter. Its almost consume as whole trying to figure out on how to do it.
Here's the code for list:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
setContentView(R.layout.activity_dictionarylist);
filterText = (EditText)findViewById(R.id.txtsearch);
ListView itemList = (ListView)findViewById(R.id.listView);
DBHelper dbHelper = new DBHelper(Dictionarylist.this);
String[] terms = dbHelper.dictionaryWords();
listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1,terms);
itemList.setAdapter(listAdapter);
itemList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Position " + position, Toast.LENGTH_LONG).show();
Intent intent = new Intent(Dictionarylist.this, DictionaryActvity.class);
intent.putExtra("DICTIONARY_ID", position);
startActivity(intent);
}
});
filterText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Dictionarylist.this.listAdapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
Here's the code for Database Helper:
public class DBHelper extends DBCon {
public DBHelper(Context context){
super(context);
}
public String[] dictionaryWords(){
String query = "Select * from main_dictionary";
Cursor cursor = this.getdbconnection().rawQuery(query, null);
ArrayList<String> wordterms = new ArrayList<String>();
if(cursor.moveToFirst()){
do{
String word = cursor.getString(cursor.getColumnIndexOrThrow("main_word"));
wordterms.add(word);
}
while (cursor.moveToNext());
}
cursor.close();
String[] dictionaryWords = new String[wordterms.size()];
dictionaryWords = wordterms.toArray(dictionaryWords);
return dictionaryWords;
}
public Quizzer getQuizById(int quizId){
Quizzer quizzer = null;
String query = "select * from main_dictionary where _id =" + quizId;
Cursor cursor = this.getdbconnection().rawQuery(query, null);
if(cursor.moveToFirst()){
do{
String word = cursor.getString(cursor.getColumnIndexOrThrow("main_word"));
String tagdef = cursor.getString(cursor.getColumnIndexOrThrow("tag_definition"));
String engdef = cursor.getString(cursor.getColumnIndexOrThrow("eng_defintion"));
String sample = cursor.getString(cursor.getColumnIndexOrThrow("example"));
quizzer = new Quizzer(word, tagdef, engdef, sample);
}
while(cursor.moveToNext());
}
cursor.close();
return quizzer;
}
I would appreciate any help.
AFAIK inside RecyclerView's
onBindViewHolder(ViewHolder holder, int position)
, you may want to attachholder.setTag(model.getId)
instead of holder.setTag(position) that we do. Take anOnClickListener
outside byholder.setOnClickListener(exampleListener)
and get that tag insideView.OnclickListener
by usingint positionId = (int) holder.getTag()
. This way you may utilize RecyclerView too.