[英]How can i get a button to add an item to an arraylist then to a listview in another fragment activity
[英]How can i add a listview item each time i press a button from another activity?
我有一個listview項,其中包含2個對象:1個Textview(String)和1個Switch按鈕(布爾)。 我正在使用自定義適配器和自定義ArrayList來顯示這些項目,並且我想在每次按下來自不同活動的按鈕時添加一個項目。 問題是,當我按下按鈕以添加新的列表視圖項時,它將用新的列表項替換舊的列表項,而不是添加新的行。 我嘗試使用myadapter.notifydatasetchanged()更新列表視圖,但是沒有用,因為當活動發生變化時,忘記了數組的大小,就像從頭開始添加新行一樣。
//Constructor
MyAdapter(Context context, List<Model> ModelArrayLista)
{
super(context,0,ModelArrayLista);
this.contexts = context;
this.ModelArrayList = ModelArrayLista;
}
@Override
public int getCount() {
return ModelArrayList.size();
}
public void remove(int position)
{
ModelArrayList.remove(position);
this.notifyDataSetChanged();
}
public View getView(int position, View convertView, @NonNull ViewGroup parent)
{
View listItem = convertView;
if(listItem == null)
{
listItem = LayoutInflater.from(contexts).inflate(R.layout.listview_item, parent, false);
}
Model currentModel = ModelArrayList.get(position);
TextView mtime = listItem.findViewById(R.id.textView1);
mtime.setText(currentModel.getTime());
final Switch mswitch = listItem.findViewById(R.id.switch1);
mswitch.setChecked(currentModel.getSwitch_btn());
final Intent disabled_intent = new Intent(contexts, Clock_Activity.class);
final Intent enable_intent = new Intent(contexts,Clock_Activity.class);
// Switch button - Turn on/Turn off
mswitch.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(mswitch.isChecked())
{
enable_intent.putExtra("enable",en);
contexts.startActivity(enable_intent);
}
else
{
disabled_intent.putExtra("disable", dis);
contexts.startActivity(disabled_intent);
}
}
});
return listItem;
}
public class Model extends ArrayList<Model> {
private String time;
private boolean switch_btn;
public Model(String wra,Boolean switcher)
{
this.time = wra;
this.switch_btn = switcher;
}
public String getTime()
{
return time;
}
public boolean getSwitch_btn()
{
return switch_btn;
}
}
//OnCreate
Initialisations();
//get a String value that control our adapter
Intent intent = getIntent();
Bundle b = intent.getExtras();
if(b!= null)
{
get_time = b.getString("time");
}
int num = intent.getIntExtra("number", 0);
list = new ArrayList<>();
//call our custom adaptersS
myAdapter = new MyAdapter(this,list);
listView.setAdapter(myAdapter);
//get a boolean value that controls our adapter
showthelist = intent.getBooleanExtra("keyList",false);
if(showthelist)
{
list.add(new Model(get_time,true));
myAdapter.notifyDataSetChanged();
}
if(num == 1)
{
list.set(0,new Model(get_time,false));
}
從技術上講,如果活動被破壞,則不可能將數據保留在列表中。
您可以實施以下兩種解決方案之一,
創建SQLite數據庫或共享首選項以保存列表,因此,每次創建實例時,構造函數都會讀取數據庫並將其加載到列表視圖中。
如果您的數據不是那么重要(例如,在關閉應用程序后不希望使用該數據),則保持該活動不變,並在其上堆疊其他活動。 下面是執行此操作的偽代碼。
ActivityIntact{
//on some button click
startActivity(NewActivity);
}
NewActivity{
//Add a close button or just finish this activity anyhow you want
closeButton.setOnclickListener(new View.onClickListener(){
@Override
public void onClick(View view){
NewActivity.this.finish(); //This is important to destroy this activity
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.