0

I am very beginner so please help me out , I am using android studio , and I saw on internet one is using arrayadapter and listfragment to list set of strings, I was doing it for button and i am failed on it , kindly help me out :) thanks in advance....

public class MainActivityListFragment extends ListFragment {

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    Button[] btn= new Button[3];
    ArrayAdapter<Button> adapter = new ArrayAdapter<Button>(getActivity(),R.layout.buttonview,btn );
    setListAdapter(adapter);

}
 @Override
 public void onListItemClick(ListView lv,View v, int position,long id)
 {
     super.onListItemClick(lv,v,position,id);
 }

}
Blackbelt
  • 156,034
  • 29
  • 297
  • 305

2 Answers2

0

Here is the answer to your question, use a custom adapter with getview

ListView row buttons: How do I create a custom Adapter that connects a View.OnClickListener to a button on each row of a ListView?

Community
  • 1
  • 1
Vsegar
  • 113
  • 1
  • 8
0

You can't do it this way using Array adapter. You need to create custom adapter for this.

public class MyArrayAdapter extends ArrayAdapter<String[]>{
    Context mContext;
    String[] data;

   public MyArrayAdapter(Context context, int textViewResourceId, String temp) {
       super(context, textViewResourceId);
       mContext = context;
       this.data = temp;
    }

@Override
public View getView(int position, View convertView, ViewGroup parent{
    View row = convertView;
    ViewHolder holder = new ViewHolder();

    if(row == null){
        LayoutInflater inflater ((Activity)mContext).getLayoutInflater();
        row = inflater.inflate(R.layout.pager, parent, false);
        holder.v1 = (Button) row.findViewById(R.id.button);
    row.setTag(holder);
}
else
{
    holder = (ViewHolder) row.getTag();
}
holder.v1.setText(data[position])
return row;
}
    public static class ViewHolder {
       Button button;
    }
}

You can create custom adapter this way and send data to list. pager is your layout and it should have button in it.