i have EditText and two buttons(SAVE & EDIT). how do i use the edittext to list a list of item which i want to create in a ArrayList. the edittext will give me the stored items, save button will give me the opportunity to save any item i want to save in the array and edit button will give me the opportunity to update existing item. thanks a lot for your effort. here is the MainActivity code..
Button btnSave, btnEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSave= (Button) findViewById(R.id.buttonSave);
btnEdit= (Button) findViewById(R.id.buttonEdit);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(MainActivity.this, InsertItem.class);
startActivity(intent);
}
});
btnEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(MainActivity.this, UpdateItem.class);
startActivity(intent);
}
});
}
}
This is updateActivity..
Button btnUpdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_update_item);
btnUpdate=findViewById(R.id.buttonUpdate);
btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(UpdateItem.this,"Update Item",Toast.LENGTH_LONG).show();
}
});
}
}
SaveActivity code....
Button btnApply;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_insert_item);
btnApply= findViewById(R.id.buttonApply);
btnApply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(InsertItem.this,"Item Inserted",Toast.LENGTH_LONG).show();
}
});
}
}
I'm not exactly sure what do you mean by saying "use the EditText to list a list of items", but you're probably looking for AutoCompleteTextView , where you can set your array of items in an adapter and then display them in a list form.
You can then modify the ArrayList by putting a new item or getting the item you want to edit by using OnItemClickListener .
EDIT:
For inserting an item
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
arrayList.add(valueFromEditText); // add element to arrayList
adapter.notifyDataSetChanged(); // refresh the adapter
}
});
Hope I could help you somehow :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.