我有各种各样的片段,但是我显示了有一个名为ShowItem.java的bug的片段。 我在另一个片段中添加了数据,并在setter和getter的帮助下将其发送到另一个片段,并将其添加到ShowItem.java片段上的listview中。我有一个选项,当用户点击保存选项然后listview整个数据保存到数据库并清除数据库时,我回到showItem.java片段然后它显示listview中的数据库和列表视图数据。这是代码ShowItem.java
public class ShowItem extends Fragment {
// initialization and variable declaration
ArrayList<String> inventoryArray;
private ArrayAdapter<String> listAdapter;
public DBAdapter mDBAdapter;
String date_time = null;
String invoice_number=null;
String quality=null;
String user;
ListView list_data;
String a,b,c,d;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_show_item, null, false);
// getting the listview and back button id
list_data = (ListView) view.findViewById(R.id.idata);
Button button_back = (Button) view.findViewById(R.id.iback);
mDBAdapter=new DBAdapter(getActivity());
inventoryArray = new ArrayList<>(Inventory.mInventoryModels.size());
// accessing the model and set on the list view
for(UseModel useModel: Home.mInventoryModels){
user=useModel.getUser();
}
for (InventoryModel model : Inventory.mInventoryModels) {
date_time = model.getDate();
invoice_number = model.getInvoice_number();
quality = model.getQuality();
inventoryArray.add(0,user+" "+date_time + " " + invoice_number + " " + quality);
listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, inventoryArray);
list_data.setAdapter(listAdapter);
}
// for showing the data on listview from the database
Cursor cursor=mDBAdapter.selectLogs();
if(cursor.moveToFirst()) {
do{
a = cursor.getString(1);
b = cursor.getString(2);
c = cursor.getString(3);
d=cursor.getString(4);
inventoryArray.add(a + " " + b + " " + c+ " "+ d );
}
while(cursor.moveToNext());
}
button_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Inventory inventory = new Inventory();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.defaultFrame, inventory).commit();
}
});
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
super.onCreateOptionsMenu(menu, inflater);
}
// menu option
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.send:
deletingData();
return true;
case R.id.save:
savingData();
listAdapter.clear();
listAdapter.notifyDataSetChanged();
return true;
case R.id.profile:
Profile profile = new Profile();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.defaultFrame, profile).commit();
return true;
default:
break;
}
return true;
}
// method for saving the data into the database
public void savingData(){
boolean data=mDBAdapter.gettingValues(user,date_time,invoice_number,quality);
if(data){
try{
Toast.makeText(getActivity(), "Data saved", Toast.LENGTH_SHORT).show();
}
catch (Exception e){
e.printStackTrace();
}
}
else{
Toast.makeText(getActivity(), "Data not saved",Toast.LENGTH_SHORT).show();
}
}
//deleting the data from the database
public void deletingData(){
new AlertDialog.Builder(getActivity()).setMessage("Are you sure? This will delete all entries")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mDBAdapter.clearData();
listAdapter.clear();
listAdapter.notifyDataSetChanged();
Toast.makeText(getActivity(), "Database cleared", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getActivity().finish();
}
}).setIcon(R.mipmap.ic_launcher).show();
}
}
在将数据添加到listview之后,在保存并清除listview后,它看起来像第一个截图,但是当我回到showItem.java片段时,它有listview,那么它看起来像第二个截图,我认为它同时显示数据库和listview值。 的更新
public class DBAdapter extends SQLiteOpenHelper {
public static final String database_name="inventoryData";
public static final String table="dataTable";
public static final String id="_id";
public static final String userName="user";
public static final String datetime="date_time";
public static final String invoicenumber="invoice_number";
public static final String i_quality="quality";
public DBAdapter(Context context) {
super(context, database_name, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
// creation of table into a database
String defaultTable="create table datatable (id integer primary key, user varchar(255), date_time varchar(255), invoice_number varchar(255), quality varchar(255));";
sqLiteDatabase.execSQL(defaultTable);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
// if table exists then it will drop the table
sqLiteDatabase.execSQL("drop table if exists dataTable");
onCreate(sqLiteDatabase);
}
// saving the data into the table's column
public boolean gettingValues(String user,String date_time,String invoice_number,String quality) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues data = new ContentValues();
data.put(userName,user);
data.put(datetime, date_time);
data.put(invoicenumber, invoice_number);
data.put(i_quality, quality);
// insert values
long a = database.insert(table, null, data);
if (a == -1) {
return false;
} else {
return true;
}
}
// deleting the data
public void clearData(){
SQLiteDatabase database=this.getWritableDatabase();
database.delete(table,null,null);
}
// selecting the data from the database
public Cursor selectLogs(){
SQLiteDatabase database=this.getWritableDatabase();
Cursor cursor=database.rawQuery("select * from "+table,null);
return cursor;
}
}
答案 0 :(得分:0)
IndexError
这是正确的方法;清除arraylist,你传递给适配器,然后通知适配器数据已被切换(或清除)。
答案 1 :(得分:0)
尝试在将所有数据添加到列表后创建适配器:
for (InventoryModel model : Inventory.mInventoryModels) {
date_time = model.getDate();
invoice_number = model.getInvoice_number();
quality = model.getQuality();
inventoryArray.add(0,user+" "+date_time + " " + invoice_number + " " + quality);
}
// for showing the data on listview from the database
Cursor cursor=mDBAdapter.selectLogs();
if(cursor.moveToFirst()) {
do{
a = cursor.getString(1);
b = cursor.getString(2);
c = cursor.getString(3);
d=cursor.getString(4);
inventoryArray.add(a + " " + b + " " + c+ " "+ d + " (DB)");
}
while(cursor.moveToNext());
}
listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, inventoryArray);
list_data.setAdapter(listAdapter);