0

I have two java classes below that make a customListView. I got the code from somebody I'm working on an app with but I don't really understand her code. I want to add buttons that will bring you to an new activity when they are clicked, and change the text of "Description of "+itemname[positionof] to be different for each section(she has them all grouped so I can't change each individually).

I know that you can do something like

public void onLeaderboardClick(View view)
    {
        Intent intent = new Intent(MainActivity.this , Leaderboard.class);
        startActivity(intent);
    }

to start the new activites but as I've said above I'm just not sure how to split them up indivually so each can have a diffeernt onClickValue

public class ListMainActivity extends Activity {
        ListView list;
        String[] itemname ={
                "Capture The Flag",
                "Leaderboard",
                "Log out"};

    Integer[] imgId={
            R.drawable.capturetheflag,
            R.drawable.leaderboard,
            R.drawable.logout,
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_main);

        CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgId);
        list=(ListView)findViewById(R.id.list);
        list.setAdapter(adapter);

        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                String Selecteditem= itemname[+position];
                Toast.makeText(getApplicationContext(), Selecteditem, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

and

public class CustomListAdapter extends ArrayAdapter<String>{

private final Activity context;
private final String[] itemname;
private final Integer[] imgid;

public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid) {
    super(context, R.layout.mylist, itemname);
    this.context=context;
    this.itemname=itemname;
    this.imgid=imgid;
}
public View getView(int position,View view,ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    View rowView=inflater.inflate(R.layout.mylist, null,true);

    TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);

    txtTitle.setText(itemname[position]);
    imageView.setImageResource(imgid[position]);
    extratxt.setText("Description of "+itemname[position]);
    return rowView;
};
}
JMeehan1
  • 49
  • 3
  • 12
  • I believe you can set an onClickListener inside the `getView` of the `CustomListAdapter`. – Tim Dec 28 '15 at 15:30

5 Answers5

0

You can add individual on click listener to every element in the list item by specifying them in the custom adapter

How do i call onclick listener of a button which resides in listview item

View this question it might be helpful to you

Community
  • 1
  • 1
Ameya Kulkarni
  • 198
  • 1
  • 2
  • 14
0

I think you should add a button into row layut layout. Then in getView() method of Adapter class :

Button btnClick= (Button) rowView.findViewById(R.id.btnClick);
btnClick.setOnClickListener(@Override
    public void onClick(View v) {
        //todo something with each button
    });
CodeMonster
  • 300
  • 5
  • 19
0

For anyone looking back at this question, I ended up making a switch statement

JMeehan1
  • 49
  • 3
  • 12
0
       # In your Adapter Class while inflating xml add a buttons as required 
     #   and get the references for that buttons then write onClick on #that button in your getView method of Adapter 


    public View getView(int position,View view,ViewGroup parent) {
        LayoutInflater inflater=context.getLayoutInflater();
        View rowView=inflater.inflate(R.layout.mylist, null,true);

        TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);


    Button button =(Button) rowView.findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   // Do your Work 

Launch the Required Intent 
                }
            });

        txtTitle.setText(itemname[position]);
        imageView.setImageResource(imgid[position]);
        extratxt.setText("Description of "+itemname[position]);
        return rowView;
    };
Ramesh Kanuganti
  • 275
  • 1
  • 11
0

You will have to use rowView.setOnClickListener() in the getViewMethod of the class custom adapter and inside that you can then specify which activity you wanna go based on the position of the view.

Jawad Ahmed
  • 306
  • 1
  • 5
  • 9