gpt4 book ai didi

java - 每次 Activity 恢复时,自定义 ListView 都会重复自身(android)

转载 作者:行者123 更新时间:2023-11-30 10:27:38 28 4
gpt4 key购买 nike

我的 android 自定义 ListView 存在这个问题,因此每次我退出 Activity (即单击模拟器上的“后退”按钮),然后返回到包含此自定义 ListView 的同一 Activity 时, ListView 都会添加一个额外的行。

比如原来是:

item a

当我离开该 Activity 并返回时,该行加倍:

item a
item a

但是,当我再次重新启动模拟器时,自定义 ListView 会返回到从 sqlite 检索到的原始数据数。

如何阻止行自身加倍?这是我的代码。

列表.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


}
}

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是一个(java-)单例。每次你调用public ArrayList<SalesItemInformationLV> retrieveAllForlist(Context) , 你 add displayiteminfo2 中的对象et 返回此列表。
如果你打第二次retrieveAllForlist ,您保留与其中已有对象的相同列表并向其中添加更多对象。

返回 private 是一个糟糕的模式函数中的实例对象。类(class)之外的任何人都可以修改列表。只需创建一个用于返回它即可。

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

关于java - 每次 Activity 恢复时,自定义 ListView 都会重复自身(android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45239038/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com