Mohammad Ghanatian Mohammad Ghanatian - 1 year ago 102
Android Question

Explain the logic of dealing with objects in setOnItemClickListener

Look at the following code:

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

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

//*************Releasing resources

It belongs to a Listview.

When a row in that ListView is clicked, this method intercepts the click and plays the corresponding song for that item.

In the fourth line you see this code:
word word = words.get(position);

  • What is "position"?

  • And what is the ".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 see it:

public class word {

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

public word(String defaultTranslation,String miwokTranslation,int audioResourceId){
public word(String defaultTranslation,String miwokTranslation,int ImageResourceId,int audioResourceId){


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 Source

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?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download