简体   繁体   English

如何在Android中使用arrayAdapter和Listfragment制作按钮列表?

[英]How to make List of button using arrayAdapter and Listfragment in android?

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.... 我是一个非常初学者,所以请帮助我,我正在使用android studio,并且我在互联网上看到一个正在使用arrayadapter和listfragment列出一组字符串,我正在为按钮做它,但我失败了,请帮帮我:) 提前致谢....

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);
 }

}

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. 传呼机是您的布局,它应该有按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM