为什么listView的第一个元素多次调用?

时间:2016-04-24 04:22:56

标签: android

我正在使用Calendar App显示用户选择的项目列表。我已经在片段中设置了适配器,其中包含2个array_lists(一个用于图像资源,另一个用于项目名称)和列表大小,而不是仅显示特定数量的列表项,只有第一个项目来了,get_View方法只调用第一个列表10次元素,只显示一个项目。

ItemSelected类的代码

        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;
            }
        }

TabFragment1

的代码
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);
        }
    }

}

1 个答案:

答案 0 :(得分:1)

由于声誉低,我无法发表评论。请发布布局文件。此外,不是使用两个arraylists和count变量,而是创建一个模型类。它将使您的代码干净且易于管理。

参考点号。这个链接http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/

中的12和13