0

I am trying to pass information to the next activity in my application based on which list view item was selected. However, startActivity is called when a custom button(id: button_go) is clicked.

I believe the common way to pass information based on which list view item is selected is through the following code:

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position, long id){
            Intent intent = new Intent(getActivity(), Detail_Activity.class);

        }
    });

But this does not work in my class as it is connected to a button. My code within my list View fragment follows.

class CustomAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        return testArray.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = getActivity().getLayoutInflater().inflate(R.layout.customlayout,null);


        Button button_go = (Button)view.findViewById(R.id.button_go);


        button_go.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            }
                // Perform action on click
                //startActivity(new Intent(getActivity(), Detail_Activity.class));
            }
        });


        return view;
    }
}

}

Ryan.H
  • 361
  • 4
  • 19

2 Answers2

1

Create your Intent in your button's onClickListener, and put the position (or any other list item specific info) of the button clicked in the Intent as an extra.

button_go.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), Detail_Activity.class));
            intent.putExtra("position", i);
            startActivity(intent);
        }
    });

Then get the position of the button clicked in the new Activity's onCreate() with:

Integer buttonPos = getIntent().getIntExtra("position");
rguessford
  • 370
  • 4
  • 10
0

I'm not sure about what is your question. Could you please tell me what is happening when you click on the button? Try to add a log to be sure that the onClick is called.

Your problem is that you don't know how to pass information in the new activity or that your activity isn't starting?

FYI, you can retrieve the item at this position by calling getItem(position).