2016-04-24 11 views
0

Ich arbeite an der Kalender-App, um eine Liste der vom Benutzer ausgewählten Elemente anzuzeigen. Ich habe Adapter in Fragment mit 2 Array-Listen (eine für Bild-Ressourcen und andere für Artikel Name) und Größe der Liste gesetzt, aber nicht zur Anzeige bestimmter Anzahl von Listenelementen nur das erste Element kommt und get_View-Methode ruft 10 mal nur für die erste Liste Element und nur ein Element wird angezeigt.Warum das erste Element von ListView mehrfach aufruft?

Code von ItemSelected Klasse

 package shopping.com.shopping.adapter; 
     import android.content.Context; 
     import android.util.Log; 
     import android.view.LayoutInflater; 
     import android.view.View; 
     import android.view.ViewGroup; 
     import android.widget.BaseAdapter; 
     import android.widget.ImageView; 
     import android.widget.ScrollView; 
     import android.widget.TextView; 
     import java.util.ArrayList; 
     import shopping.com.shopping.R; 

     public class ItemSelected extends BaseAdapter { 

      Context mContext; 
      int limit; 
      int count=0; 
      ScrollView sc_list; 
      private LayoutInflater inflater = null; 
      String item_name[]={"Egg","Bread","Milk","Watercan","Fruit","Egg","Bread"}; 
      int imgsrc[]={R.drawable.smallegg,R.drawable.smallbread,R.drawable.smallmilk,R.drawable.smallwatert,R.drawable.smallapple,R.drawable.smallegg,R.drawable.smallbread}; 
      ArrayList<String> listofIndexes,listofquantities; 
      ArrayList<String> location_or_society_details; 
      public ItemSelected(Context context ,ArrayList<String> listofIndexes,ArrayList<String> listofquantities,int limit) { 
       // "imgsrc" is the image-reference of selected item from the list 
       // "quantity" is the quantity of selected item which customer want to book 
       // "imgTitle" is the name of selected img like milk,bread,watertank 
       // "getimgSrc[]" is the array which contains refrences of all the items 
       // "get_title" is the array which contains all the items names 
       this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

       this.mContext = context; 
       this.limit=limit; 
       this.listofIndexes=listofIndexes; 
       this.listofquantities=listofquantities; 
      } 
      @Override 
      public int getCount() { 
       return limit; 
      } 

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

      @Override 
      public long getItemId(int position) { 

       return position; 
      } 

      @Override 
      public int getViewTypeCount() { 
       return getCount(); 
      } 

      @Override 
      public int getItemViewType(int position) { 
       return position; 
      } 

      @Override 
      public View getView(int position, View convertView, ViewGroup parent) { 
       View view = convertView; 
       Log.d("testing","afzal"); 
       count++; 
       TextView i_name=null,i_count=null; 
       ImageView i_image=null; 
       if(convertView == null) { 
        inflater = LayoutInflater.from(mContext); 
        view = inflater.inflate(R.layout.items_list, null); 
       } 
       i_image = (ImageView) view.findViewById(R.id.i_image); 
       i_name = (TextView) view.findViewById(R.id.i_name); 
       i_count = (TextView) view.findViewById(R.id.i_count); 
       i_name.setText("testing"); 
       i_count.setText("count"); 
       i_image.setImageResource(R.drawable.logo); 
       //setting respective value of Booked list_items with 'Item_name' and their 'Quantity'. suppose first user select item#5 with quantity 5 then item#2 with quatity 9 and so on, then first child of listView should b item#5 with quant 5 second list_item should be 2 with quantity 9 and so on 
     //  String index= listofIndexes.get(position); 
     //  String quantity=listofquantities.get(position); 
     //  i_name.setText(item_name[Integer.parseInt(index)]); 
     //  i_image.setImageResource(imgsrc[Integer.parseInt(index)]); 
       // i_count.setText(Integer.parseInt(quantity)); 
       return view; 
      } 
     } 

Code für TabFragment1 Klasse

package shopping.com.shopping.fragmensts; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.support.annotation.NonNull; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.ListView; 
import android.widget.ScrollView; 
import android.widget.Switch; 
import android.widget.Toast; 
import com.samsistemas.calendarview.widget.CalendarView; 
import com.samsistemas.calendarview.widget.DayView; 

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.Locale; 
import shopping.com.shopping.R; 
import shopping.com.shopping.activities.ListItemsActivity; 
import shopping.com.shopping.activities.SetOrder; 
import shopping.com.shopping.activities.SignUpSignIn; 
import shopping.com.shopping.adapter.ItemSelected; 
import shopping.com.shopping.adapter.PagerAdapter; 

public class TabFragment1 extends Fragment implements View.OnClickListener{ 

    private CalendarView mCalendarView; 
    private View myFragmentView; 
    Button btn; 
    ItemSelected adapter; 
    ListView listview; 
    LinearLayout l_lay; 
    ScrollView scroll; 
    ArrayList<String> listofIndexes,listofquantities; 
    int iTem_Index=3; 
    String item_name[]={"Egg","Bread","Milk","Watercan","Fruit","Egg","Bread"}; 
    int imgsrc[]={R.drawable.smallegg,R.drawable.smallbread,R.drawable.smallmilk,R.drawable.smallwatert,R.drawable.smallapple,R.drawable.smallegg,R.drawable.smallbread}; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     myFragmentView= inflater.inflate(R.layout.fragment_tab_fragment1, container, false); 
     mCalendarView = (CalendarView) myFragmentView.findViewById(R.id.calendar_view); 
     btn= (Button) myFragmentView.findViewById(R.id.try_it_now); 
     l_lay = (LinearLayout) myFragmentView.findViewById(R.id.linear); 
     listview = (ListView) myFragmentView.findViewById(R.id.booked_item); 
     scroll=(ScrollView)myFragmentView.findViewById(R.id.scroll_list); 
     SharedPreferences sh_pref=getActivity().getSharedPreferences("backToHome", Context.MODE_PRIVATE); 
     int flag= sh_pref.getInt("flag", 0); 
     String item=sh_pref.getString("item","null"); 
     int quantity=sh_pref.getInt("quantity",0); 

     //if user has booked something then listView in home page will be visible 
     if(flag==3){ 
      int count=0; 
      try { 
       listofIndexes=new ArrayList<String>(); 
       BufferedReader inputReader = new BufferedReader(new InputStreamReader(getActivity().openFileInput("ItemsBooked"))); 
       String readItem; 
       StringBuffer stringBuffer = new StringBuffer(); 
       while ((readItem = inputReader.readLine()) != null){ 
        listofIndexes.add(readItem); 
       } 
      } 
      catch (IOException e) { 
       e.printStackTrace(); 
      } 

      try { 
       listofquantities=new ArrayList<String>(); 
       BufferedReader inputReader = new BufferedReader(new InputStreamReader(getActivity().openFileInput("Quantity"))); 
       String readQuantity; 
       StringBuffer stringBuffer = new StringBuffer(); 

       while ((readQuantity = inputReader.readLine()) != null) { 
        count++; 
        Toast.makeText(getActivity(), "count is .."+count, Toast.LENGTH_SHORT).show(); 
        listofquantities.add(readQuantity); 
       } 
      } 
      catch (IOException e) { 
       e.printStackTrace(); 
      } 
      l_lay.setVisibility(View.GONE); 
      scroll.setVisibility(View.VISIBLE); 
      listview.setVisibility(View.VISIBLE); 
      // after matching and verifying, adding "item" into the array list 
      adapter= new ItemSelected(getActivity(),listofIndexes,listofquantities,count); 
      listview.setAdapter(adapter); 
     } 
     btn.setOnClickListener(this); 
     //put intent 
     mCalendarView.setFirstDayOfWeek(Calendar.SUNDAY); 
     mCalendarView.setIsOverflowDateVisible(true); 
     mCalendarView.setCurrentDay(new Date(System.currentTimeMillis())); 
     mCalendarView.setNextButtonColor(R.color.colorAccent); 
     mCalendarView.refreshCalendar(Calendar.getInstance(Locale.getDefault())); 
     mCalendarView.setNextButtonColor(R.color.bg_for_selecte_dday); 
     mCalendarView.setBackButtonColor(R.color.bg_for_selecte_dday); 
     //get current date 
     Date date=new Date(System.currentTimeMillis()); 
     mCalendarView.setCurrentDay(date); 
     mCalendarView.setOnDateSelectedListener(new CalendarView.OnDateSelectedListener() { 
      @Override 
      public void onDateSelected(@NonNull Date selectedDate) { 
       Toast.makeText(getActivity(), "second date is selected", Toast.LENGTH_SHORT).show(); 
       mCalendarView.setSelectedDayBackground(getResources().getColor(R.color.white)); 
       SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()); 
      } 
     }); 

     mCalendarView.setOnMonthChangedListener(new CalendarView.OnMonthChangedListener() { 
      @Override 
      public void onMonthChanged(@NonNull Date monthDate) { 
       SimpleDateFormat df = new SimpleDateFormat("dd MMMM yyyy", Locale.getDefault()); 

      } 
     }); 

     final DayView dayView = mCalendarView.findViewByDate(new Date(System.currentTimeMillis())); 
     return myFragmentView; 
    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.try_it_now: 
       Intent i=new Intent(getActivity(),ListItemsActivity.class); 
       i.putExtra("flag", 3); 
       startActivity(i); 
     } 
    } 

} 
+0

Ich habe in logcat, erste Artikel Anrufe mehrere Male eingecheckt aber zeigt nur einzelne Artikel in der Liste Anzeigen –

+0

jeder Freiwillige, die mir helfen kann.Ich werde dankbar sein. –

+0

Haben Sie sichergestellt, dass 'listofIndexes' mit anderen Werten initialisiert wird? –

Antwort

Verwandte Themen