Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 每次活动恢复时,自定义listview都会重复自身(android)_Java_Android_Listview - Fatal编程技术网

Java 每次活动恢复时,自定义listview都会重复自身(android)

Java 每次活动恢复时,自定义listview都会重复自身(android),java,android,listview,Java,Android,Listview,我的android自定义listview存在此问题,因此每次我退出活动(即单击emulator上的“后退”按钮),然后返回到包含此自定义listview的同一活动时,listview都会向自身添加一行 例如,原来是: item a 当我离开该活动并返回该活动时,行加倍: item a item a 但是,当我再次重新启动emulator时,自定义listview返回到从sqlite检索的原始数据数量 我如何阻止行加倍? 这是我的密码 list.java: //DATABASE MyI

我的android自定义listview存在此问题,因此每次我退出活动(即单击emulator上的“后退”按钮),然后返回到包含此自定义listview的同一活动时,listview都会向自身添加一行

例如,原来是:

item a
当我离开该活动并返回该活动时,行加倍:

item a
item a
但是,当我再次重新启动emulator时,自定义listview返回到从sqlite检索的原始数据数量

我如何阻止行加倍? 这是我的密码

list.java:

//DATABASE
    MyItems mi;
    //For Items display - ArrayList
    private ArrayList<SalesItemInformationLV> displayiteminfo;
           /* new ArrayList<SalesItemInformationLV>(); */
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_sale_item);



        final float sellingpvalue = 13.5f;
        final float costpvalue = 19.0f;
        final String datesoldvalue = "9/9/1995";
        final String staffdiscountvalue = "true";

        mi = MyItems.getInstance();
        displayiteminfo = mi.retrieveAllForlist(getApplicationContext());



        //New array adapter for customised ArrayAdapter
        final ArrayAdapter<SalesItemInformationLV> adapter = new itemArrayAdapter(this, 0, displayiteminfo);
        //displayiteminfo - the ArrayList of item objects to display.

        //Find the list view, bind it with custom adapter
        final ListView listView = (ListView)findViewById(R.id.customListview);
        listView.setAdapter(adapter);
       // listView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 9));

        //LONG PRESS CONTEXT MENU
        registerForContextMenu(listView);

        //Selecting the listview item!
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SalesItemInformationLV saleitem = displayiteminfo.get(position);
                String namevalue = saleitem.getItemname();
                int qtyvalue = saleitem.getItemquantity();

                Intent myintent = new Intent(ListSaleItemActivity.this, ViewSaleDetails.class);
                myintent.putExtra("itemname", namevalue);
                myintent.putExtra("itemqty", qtyvalue);
                myintent.putExtra("itemcp", costpvalue);
                myintent.putExtra("itemsp", sellingpvalue);
                myintent.putExtra("itemds", datesoldvalue);
                myintent.putExtra("itemsstaffdis", staffdiscountvalue);

                startActivity(myintent);

            }
        });



    }



    //custom Arrayadapter
    class itemArrayAdapter extends ArrayAdapter<SalesItemInformationLV>
    {
        private  Context context;
        private List<SalesItemInformationLV> item;



        //constructor, call on creation
        public itemArrayAdapter(Context context, int resource, ArrayList<SalesItemInformationLV> objects) {

            //chaining to "default constructor" of ArrayAdapter manually
            super(context, resource, objects);
            this.context = context;
            this.item = objects;

        }

        //called to render the list

        public View getView(int position, View convertView, ViewGroup parent)
        {


            //get the item we are displaying
            SalesItemInformationLV iteminfo = item.get(position);

            //get the inflater and inflate the xml layout for each item
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.item_layout, null);


            //Each component of the custom item_layout
            TextView name = (TextView) view.findViewById(R.id.ItemNameSales);
            TextView qty = (TextView)view.findViewById(R.id.ItemNameQty);

            //set the name of item - access using an object!
            name.setText(String.valueOf(iteminfo.getItemname()));

            //set the quantity of item - access using an object!
            qty.setText(String.valueOf(iteminfo.getItemquantity()));

            return view;
            //Now return to onCreate to use this cuztomized ArrayAdapter


        }
    }
//数据库
MyMi;
//用于项目显示-ArrayList
私有ArrayList displayiteminfo;
/*新的ArrayList()*/
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u list\u sale\u item);
最终浮动销售价值=13.5f;
最终浮动成本值=19.0f;
最终字符串datesoldvalue=“9/9/1995”;
最后一个字符串staffdiscountvalue=“true”;
mi=MyItems.getInstance();
displayiteminfo=mi.retrieveAllForlist(getApplicationContext());
//用于定制阵列适配器的新阵列适配器
最终ArrayAdapter=new itemArrayAdapter(this,0,displayiteminfo);
//displayiteminfo—要显示的项对象的ArrayList。
//找到列表视图,将其与自定义适配器绑定
最终ListView ListView=(ListView)findViewById(R.id.customListview);
setAdapter(适配器);
//setLayoutParams(新的LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,9));
//长按关联菜单
registerForContextMenu(列表视图);
//选择listview项目!
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
SalesItemInformationLV saleitem=displayiteminfo.get(位置);
字符串namevalue=saleitem.getItemname();
int qtyvalue=saleitem.getItemquantity();
Intent myintent=新的Intent(ListSaleItemActivity.this、ViewSaleDetails.class);
myintent.putExtra(“itemname”,namevalue);
myintent.putExtra(“项目数量”,数量值);
myintent.putExtra(“itemcp”,成本值);
myintent.putExtra(“itemsp”,sellingpvalue);
myintent.putExtra(“itemds”,datesoldvalue);
myintent.putExtra(“itemsstaffdis”,staffdiscountvalue);
星触觉(myintent);
}
});
}
//定制阵列适配器
类itemArrayAdapter扩展了ArrayAdapter
{
私人语境;
私人清单项目;
//构造函数,在创建时调用
公共项ArrayAdapter(上下文上下文、int资源、ArrayList对象){
//手动链接到ArrayAdapter的“默认构造函数”
超级(上下文、资源、对象);
this.context=上下文;
this.item=对象;
}
//调用以呈现列表
公共视图getView(int位置、视图转换视图、视图组父视图)
{
//获取我们正在显示的项目
SalesItemInformationLV iteminfo=item.get(位置);
//获取充气器并为每个项目充气xml布局
LayoutInflater充气器=(LayoutInflater)context.getSystemService(Activity.LAYOUT\u充气器\u SERVICE);
视图=充气机。充气(R.layout.item_布局,空);
//自定义项布局的每个组件
TextView name=(TextView)view.findViewById(R.id.ItemNameSales);
TextView数量=(TextView)view.findViewById(R.id.ItemName数量);
//使用对象设置项访问的名称!
name.setText(String.valueOf(iteminfo.getItemname());
//设置使用对象访问项目的数量!
qty.setText(String.valueOf(iteminfo.getItemquantity());
返回视图;
//现在返回onCreate以使用此cuztomized ArrayAdapter
}
}
Myitems.java:

public class MyItems extends Application {

    //ID and contact information
    private List<String> contactList;
    private List<Integer> contactIdList;
    private  static MyItems ourInstance = new MyItems();

    //Populate SaleItemInformationLV
    private ArrayList<SalesItemInformationLV> displayiteminfo2 =
            new ArrayList<SalesItemInformationLV>();

    public  MyItems()
    {
        contactList = new ArrayList<String>();
        contactIdList = new ArrayList<Integer>();
    }

    public  static  MyItems getInstance(){
        return ourInstance;
    }
//RETRIEVE ALL ENTRIES
    //LISTVIEW
    public  ArrayList<SalesItemInformationLV> retrieveAllForlist(Context c)
    {
        Cursor myCursor;
        String mystring = "";

        MyDbAdapter db = new MyDbAdapter(c);
        db.open();
        //contactIdList.clear();
        //contactList.clear();
        myCursor = db.retrieveAllEntriesCursor();

        if (myCursor !=null && myCursor.getCount()>0)
        {
            myCursor.moveToFirst();
            do {

                displayiteminfo2.add(new SalesItemInformationLV(myCursor.getString(db.COLUMN_NAME_ID), db.COLUMN_QTYSOLD_ID));

            } while (myCursor.moveToNext());
        }
        db.close();
        return  displayiteminfo2;
    }
公共类MyItems扩展应用程序{
//身份证和联系方式
私人名单联系人名单;
私有列表联系人列表;
私有静态MyItems ourInstance=新MyItems();
//填充SaleItemInformationLV
专用ArrayList displayiteminfo2=
新的ArrayList();
公共物品()
{
contactList=新的ArrayList();
contactIdList=新的ArrayList();
}
公共静态MyItems getInstance(){
回归自然状态;
}
//检索所有条目
//列表视图
公共ArrayList retrieveAllForlist(上下文c)
{
游标myCursor;
字符串mystring=“”;
MyDbAdapter db=新的MyDbAdapter(c);
db.open();
//contactIdList.clear();
//contactList.clear();
myCursor=db.retrieveAllEntriesCursor();
if(myCursor!=null&&myCursor.getCount()>0)
{
myCursor.moveToFirst();
做{
displayiteminfo2.add(新的SalesItemInformationLV(myCursor.getString(db.COLUMN\u NAME\u ID)、db.COLUMN\u QTYSOLD\u ID));
}while(myCursor.moveToNext());
}
db.close();
返回displayiteminfo2;
}
MyItems
是一个(java-)单例。每次调用
public ArrayList retrieveAllForlist(Context)
,都会在
displayiteminfo2
中添加
对象,并返回此列表。
如果第二次调用
retrieveAllForlist
,则会保留同一个列表,其中包含已存在的对象,并向其中添加更多对象

在f中返回
private
实例对象是一种错误的模式
public  ArrayList<SalesItemInformationLV> retrieveAllForlist(Context c)
{
    ArrayList<SalesItemInformationLV> items = new ArrayList<SalesItemInformationLV>();

    Cursor myCursor;
    String mystring = "";

    MyDbAdapter db = new MyDbAdapter(c);
    db.open();
    //contactIdList.clear();
    //contactList.clear();
    myCursor = db.retrieveAllEntriesCursor();

    if (myCursor != null && myCursor.getCount() > 0)
    {
        myCursor.moveToFirst();
        do {
            items.add(new SalesItemInformationLV(myCursor.getString(db.COLUMN_NAME_ID), db.COLUMN_QTYSOLD_ID));
        } while (myCursor.moveToNext());
    }
    db.close();

    return items;
}