如何为片段创建自定义列表

时间:2017-03-22 08:07:25

标签: android android-fragments custom-lists

我想为我的片段创建一个cutomlistview,我是初学者请你能给我写相应的代码来查看我的列表。

CustomListAdapter是红色的,我必须创建一个我已经尝试过的CustomListAdapter类但我不能这样做,如果你可以帮助我,我仍然必须这样做才能完成我的项目

这是我的代码

public class listadmin extends Fragment {
DatabaseHelper myDb;
private ListView mMyListView;
private listadmin MyListAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.listadmin, container, false);
    mMyListView = (ListView) view.findViewById(R.id.ListViewadmin);

    //create an ArrayList of a custom data class that you can create to hold the Admin Data
    ArrayList<CustomObjectClass> list = new ArrayList<>();
    //Now you need to get the data
    Cursor data = myDb.getAllAdmin();
    while (data.moveToNext()) {
        list.add(new CustomObjectClass(data.getString(1), data.getString(2)));
    }
    //Now add the data to a listview using a custom ListAdapter
    CustomListAdapter adapter = new CustomListAdapter(this, R.layout.custom_list, list);
    mMyListView.setAdapter((ListAdapter) adapter);
    return view;
}

/**
 * Example of CustomObjectClass
 **/
public class CustomObjectClass {
    private String nom;
    private String numero;

    public CustomObjectClass(String nom, String numero) {
        this.nom = nom;
        this.numero = numero;
    }
    //Create your getter and setter methods below
}

2 个答案:

答案 0 :(得分:0)

这是一个样本。我已经使用客户适配器

显示了片段上的区域列表
  public class FirstFragment extends Fragment {

private Button btnApply;
private ListView listArea;
    ArrayList<String> AreaArrayList = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);


}
    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle
    savedInstanceState){
        final View view = inflater.inflate(R.layout.fragment_one, container, false);//fragment_one layout is the layout for first tab in filter

        listArea=(ListView)view.findViewById(R.id.listArea);

    AreaArrayList.add("area1");
    AreaArrayList.add("area2");

            AreaListAdapter myOrderAdapter = new AreaListAdapter(getActivity(), AreaArrayList);
            listArea.setAdapter(myOrderAdapter);
        return view;
}


//adapter for the listview
public class AreaListAdapter extends BaseAdapter {
    ArrayList<String> AreaList;

    Context context;
    LayoutInflater inflater;

    public AreaListAdapter(Context context, ArrayList<String> AreaArrayList) {
        this.context = context;

        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (AreaArrayList != null) {

            this.AreaList = AreaArrayList;

        }

    }

    @Override
    public int getCount() {
        return AreaList.size();
    }

    @Override
    public Object getItem(int position) {
        return AreaList.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        int value;
        View areaRow = inflater.inflate(R.layout.area_filter_list_item, null);

        TextView tvAreaFilter = (TextView) areaRow.findViewById(R.id.tvAreaFilter); //To set name of menu item, for example Corn Soup

        tvAreaFilter.setText(AreaList.get(position));


        //row onClickListener
        areaRow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        return areaRow;
    }
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

}

答案 1 :(得分:0)

我在示例项目中使用了自定义listView作为Fragments,你可以查看我的整个项目here,这里是link的java文件,我实现了{{1} }。