2

Situation:

  • I have a ListView with my own ListAdapter.
  • In every row I have two Buttons.

I'm trying to implement the onClick methods for the two Buttons, but I don't find the right solution. This is the getView method from my ListAdapter with my two Buttons:

    public View getView(final int groupPosition, View convertView, ViewGroup parent) {
         if (convertView == null) {
              convertView = inflater.inflate(R.layout.modul_item, null);
         }

         TextView tv = (TextView) convertView.findViewById(R.id.modul_title);
         tv.setText(modul_overviewActivity.getvalue().get(groupPosition));

         Button Button_1 = (Button)convertView.findViewById(R.id.button1);
         Button Button_2 = (Button)convertView.findViewById(R.id.button2);

    return convertView;
}
  • In my OnCLickListeners I want to change parameters of the Objects which are displayed in the ListView.
  • The Name of the Objects is displayed, but they have a few more parameters (variables), which I want to edit in another Activity.
  • This Activity should open if I click one of the Buttons.
6
  • is that c#? May you please specify the language somehow (either in the text or in the tags). Thanks! Commented Feb 16, 2012 at 16:25
  • 1
    it's about android, so it's java! Commented Feb 16, 2012 at 16:27
  • well it is not a statement, you can program in Android also with C# or php. Commented Feb 16, 2012 at 16:33
  • ok sorry, i didn't know that! Commented Feb 16, 2012 at 16:35
  • If you want help you should add more details: what have you tried until know, what do you want to do in those OnClickListener, what errors have you encountered etc Commented Feb 16, 2012 at 16:43

3 Answers 3

2

I didn't understand exactly what you want to do in your button listeners but check this code:

public View getView(final int groupPosition, View convertView, ViewGroup parent) {
         if (convertView == null) {
              convertView = inflater.inflate(R.layout.modul_item, null);
         }

         TextView tv = (TextView) convertView.findViewById(R.id.modul_title);
         tv.setText(modul_overviewActivity.getvalue().get(groupPosition));

         Button Button_1 = (Button)convertView.findViewById(R.id.button1);
         Button_1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
                    //this is how you start a new Activity(i guess you want this for editing the details)
            Intent i = new Intent(List4.this, DetailsAct.class);            
                    startActivity(i);
                }

            });
         Button Button_2 = (Button)convertView.findViewById(R.id.button2);

    return convertView;
}

This is just for starting a new activity, the intent could carry more data to the new activity so you can send other important information(like the position of the element for which to calculate stuff, this depends of what kind of data and how you store it ).

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

3 Comments

I understand how to start a new activity and i can do it from other activities. But my ListAdapter is of course an extra class, which looks like this: public class modul_ListAdapter extends BaseAdapter implements OnClickListener {...} The adapter class does not know startActivity(i). Do you have any ideas how to solve this problem?
@user1206662 Modify your modul_listAdapter constructor so you can also take a Context object and store it in a private variable, like ctx. Then in the activity where you create the adapter you pass the Activity context(whith the keyword this) in your adapter constructor. Then you ca use ctx.startActivity(i). Also i see you use the LayoutInflater so you already have somewhere(probably the constructor) a context reference(i don't know the code for you entire adapter class)
it works! i didn't really understand the meaning of Context, but now it's obvious! thanks a lot! :)
0

if you're using xml for the button, add android:onClick="onClick" to it.

1 Comment

ok, thanks! but how does my main activity know in which listview row i clicked the button?
0

Write a public method in your Activity like

public void onClick(View v) {
.....
}

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.