0

I am new to android. I have implemented custom ArrayAdapter in my Android Application using view holder. The getView() function of my ArrayAdapter is as follows for reference:

@Override
public View getView(int position, View convertView, final ViewGroup parent) {
    View row = convertView;
    MyClassViewHolder myClassViewHolder;
    MyClass myClass;
    if(row == null) {
        LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
        row = inflater.inflate(resourceId, parent, false);
        if(resourceId == R.layout.my_row_item) {
            myClassViewHolder = new MyClassViewHolder();
            myClassViewHolder.title = (EditText) row.findViewById(R.id.title);
            myClassViewHolder.switch = (Switch) row.findViewById(R.id.switch);
        }
    } else {
        myViewHolder = (MyViewHolder) row.getTag();
    }
    if(resourceId == R.layout.my_row_item) {
        myClass = (MyClass) myClassList.get(position); //myClassList sent as parameter to constructor of adapter
        if(myClassViewHolder != null && myClass != null) {
            myClassViewHolder.title.setText(myClass.getTitle());
            myClassViewHolder.switch.setChecked(myClass.isEnabled());
            myClassViewholder.id = myClass.getId();
            myClassViewHolder.switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    //GET ID OF THE ROW ITEM HERE
                }
            });
        }
    }
}
  1. First of all I want to associate an id which is from database to every row item to perform actions on them. So please confirm if the way I have done is is right or wrong.
  2. Secondly in the above code I have a String as title and a Switch in every row item. I want to set an onClickListener on each switch. On toggling the switch i want to get the id of the row item which is associated as per point 1.

Thanks in advance. Please let me know if I haven't described my problem properly.

5
  • why you need id for each row? while you can get its row number (position) already? Commented Nov 4, 2014 at 10:36
  • Well I want to perform some action in database related to the rowitem when I toggle the switch. Is there any other way to achieve it? Commented Nov 4, 2014 at 10:45
  • set tag to each ID, and getTag() to recognize it? Commented Nov 5, 2014 at 5:59
  • Thank you Darpan. I achieved it by using tag just like you said. How do I approve your answer/comment? Commented Nov 5, 2014 at 7:37
  • Have posted my comment with an example and answer; You may mark/upvote now. Commented Nov 5, 2014 at 11:47

2 Answers 2

1

Yes your code looks fine as for second part you should make a listener on switch and then get the id form the row and do switch from one id to another.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for replying. As you can see that I have set an onCheckedChangeListener on the switch. Will you please tell me how to do that exactly. How can I refer to the list item in the listener to the switch?
0

You may need to set a tag for each row item and thus you can identify each row. Here is an example -

row.setTag(1);

and to retrive the tag -

row.getTag();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.