Mohammad Ghanatian Mohammad Ghanatian - 3 months ago 12
Android Question

explain logics of dealing with objects in setOnItemClickListener in android

Look at the following code:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
word word = words.get(position);

mMediaPlayer = MediaPlayer.create(FamilyActivity.this, word.getAudioResourceId());
mMediaPlayer.start();

//*************Releasing resources
mMediaPlayer.setOnCompletionListener(mCompletionListener);
}
});
}


It belongs to a listview. when a row in that listview is clicked, this method recognize the click and plays the corresponding song for that item.

In the forth line you see this code :
word word = words.get(position);
What is "position"?And what is ".get()" method?and can we save them in a word object?

Would you explain this specific line of code, please?




PS:For more detail I'm gonna show you the "word" class and also "words" arraylist in case you needed to explain:

public class word {

//////Atributes
private String mDefaultTranslation;
private String mMiwokTranslation;
private int mImageResourceId=NO_IMAGE_PROVIDED;
private static final int NO_IMAGE_PROVIDED=-1;
private int mAudioResourceId;

//Constructor
public word(String defaultTranslation,String miwokTranslation,int audioResourceId){
mDefaultTranslation=defaultTranslation;
mMiwokTranslation=miwokTranslation;
mAudioResourceId=audioResourceId;
}
public word(String defaultTranslation,String miwokTranslation,int ImageResourceId,int audioResourceId){
mDefaultTranslation=defaultTranslation;
mMiwokTranslation=miwokTranslation;
mImageResourceId=ImageResourceId;
mAudioResourceId=audioResourceId;

}

//Getters
public String getDefaultTranslation(){
return mDefaultTranslation;
}
public String getMiwokTranslation(){
return mMiwokTranslation;
}
public int getImageResourceId(){return mImageResourceId;}
public boolean hasImage(){return mImageResourceId != NO_IMAGE_PROVIDED;}
public int getAudioResourceId(){return mAudioResourceId;}


and this is "words" arraylist:

final ArrayList<word> words = new ArrayList<word>();
words.add(new word("father", "әpә", R.drawable.family_father, R.raw.family_father));
words.add(new word("mother", "әṭa", R.drawable.family_mother, R.raw.family_mother));
words.add(new word("son", "angsi", R.drawable.family_son, R.raw.family_son));
words.add(new word("daughter", "tune", R.drawable.family_daughter, R.raw.family_daughter));
words.add(new word("older brother", "taachi", R.drawable.family_older_brother, R.raw.family_older_brother));
words.add(new word("younger brother", "chalitti", R.drawable.family_younger_brother, R.raw.family_younger_brother));
words.add(new word("older sister", "teṭe", R.drawable.family_younger_sister, R.raw.family_older_sister));
words.add(new word("ounger sister", "kolliti", R.drawable.family_younger_sister, R.raw.family_younger_sister));
words.add(new word("grandmother", "ama", R.drawable.family_grandmother, R.raw.family_grandmother));
words.add(new word("grandfather", "paapa", R.drawable.family_grandfather, R.raw.family_grandfather));

Answer

What is "position"?

Indirect answer - Think about the purpose of the click listener. What are you clicking on - an item. It needs to know which Item you clicked. Hence, position

And what is ".get()" method?

this seems to be answered yourself. words is an Arraylist. How do you "get" an item out of an Arraylist?

In either case, the JavaDoc could answer both of those questions

can we save them in a word object?

What is "them"? You are getting an instance of a word object, the variable name also happens to be word, though.

Save the position? I wouldn't recommend that. What good would storing the position of the word in one list when that same object could be in another list, at some different position?