2015-06-01 3 views
-1

В моем приложении я пытаюсь получить доступ к пользовательскому адаптеру в фрагменте, но его не позволяет мне получить доступ к пользовательскому интерфейсу пользовательского адаптера, следуя моему кодеку фрагмента, кто-нибудь может сказать мне, что я делаю.Как получить доступ к пользовательскому адаптеру в фрагменте?

Заранее спасибо

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; 

     } 

    } 


} 
} 
+0

Вы получаете ошибку? –

+0

Изменить здесь из 'View rowView = inflater.inflate (R.layout.all_product_listitems, null, true);' to 'Просмотр rowView = inflater.inflate (R.layout.all_product_listitems, parent, false);' – Piyush

+0

Вы возвращаете ur rootView, перед классом intializing CustomListAdapter расширяет ArrayAdapter { – Pankaj

ответ

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> { 



     } 


    } 
+0

вы можете здесь помочь http://stackoverflow.com/questions/30611704/navigation-drawer-listview-not-refreshing-listitems – Aditya

Смежные вопросы