如何访问Fragment中的Customadapter?

时间:2015-06-01 10:54:51

标签: android android-fragments custom-adapter

在我的应用程序中,我试图访问片段中的自定义适配器,但它不允许我访问自定义适配器的UI,以下是我的代码段代码,任何人都可以告诉我我正在犯的错误。

提前致谢

public class All_Products extends ListFragment {

private TextView txtsortby;

ListView list;
String[] itemname ={
        "$55.00",
        "$55.00",
        "$55.00",
        "$55.00"

};

Integer[] imgid={
        R.drawable.productfirst,
        R.drawable.productsecond,
        R.drawable.productfirst,
        R.drawable.productsecond

};

String[] itemprice={"BIG 50ml EDP","BIG 50ml EDP","BIG 50ml EDP","BIG 50ml EDP","BIG 50ml EDP","BIG 50ml EDP","BIG 50ml EDP","BIG 50ml EDP"};

public All_Products() {
}


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

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



    txtsortby=(TextView)rootView.findViewById(R.id.txt_sortby_allproducts);

    CustomListAdapter adapter=new CustomListAdapter(getActivity(), itemname, imgid,itemprice);
    list=(ListView)rootView.findViewById(R.id.listallproducts);
    list.setAdapter(adapter);

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

        }
    });

    final CharSequence[] items = {
            "Alphabetical", "Alphabetical","Price","Price"
    };

    txtsortby.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

            builder.setTitle("Sort By");
            builder.setItems(items, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    // Do something with the selection


                    txtsortby.setText(items[item]);



                }
            });
            AlertDialog alert = builder.create();
            alert.show();

        }
    });
    return rootView;
    class CustomListAdapter extends ArrayAdapter<String> {

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

        public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid, String[] itemprice) {
            super(context, R.layout.all_product_listitems, itemname);
            // TODO Auto-generated constructor stub

            this.context = context;
            this.itemname = itemname;
            this.imgid = imgid;
            this.itemprice = itemprice;
        }

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

            TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
            TextView txtprice = (TextView) rowView.findViewById(R.id.txt_allproductsname);
            ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
            Button detailss = (Button) rowView.findViewById(R.id.btn_details_allproducts);


            detailss.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getActivity(), AllProductDetails.class);
                    startActivity(intent);
                }
            });
            // TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);

            txtTitle.setText(itemname[position]);
            imageView.setImageResource(imgid[position]);
            txtprice.setText(itemprice[position]);
            //  extratxt.setText("Description "+itemname[position]);
            return rowView;

        }

    }


}
}

1 个答案:

答案 0 :(得分:1)

这样做: -

 public class All_Products extends ListFragment {
   //Watever variables u are using

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

       //vies defining here
        return rootView;
    }
       //CHANGE HERE

     class CustomListAdapter extends ArrayAdapter<String> {



        }


    }