片段选项卡中的自定​​义listView

时间:2014-07-20 15:02:22

标签: android android-fragments

我试图在片段中制作自定义列表视图,我搜索了其他问题,但我找不到任何东西 我的列表适配器就是这个

public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context,
        String[] web, Integer[] imageId) {
    super(context, R.layout.listview, web);
    this.context = context;
    this.web = web;
    this.imageId = imageId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView= inflater.inflate(R.layout.listview, null, true);
    TextView txtTitle = (TextView) rowView.findViewById(R.id.textView1);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
    txtTitle.setText(web[position]);
    imageView.setImageResource(imageId[position]);
    return rowView;
}
}  

我的Fragment类是这样的:

public class tab1 extends Fragment {

ListView list;
  String[] string = {
    "Shoe",
      "Clothes",

  } ;
  Integer[] imageId = {
      R.drawable.shoe,
      R.drawable.clothes,

  };    

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {



    View rootView = inflater.inflate(R.layout.tab1, container, false);

    CustomList adapter = new CustomList(tab1.this, string, imageId);
        list=(ListView) rootView.findViewById(R.id.list);
            list.setAdapter(adapter);
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        //Toast.makeText(tab1.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
                    }
                });


    return rootView;
}

}

问题在于这一部分: CustomList adapter = new CustomList(tab1.this,string,imageId); 构造函数( public CustomList(Activity context,             String [] web,Integer [] imageId))只接受Activity

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

你试过了吗?

CustomList adapter = new CustomList(getActivity(), string, imageId);