5. Main activity
btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*getting the data from edit text and converting to String.*/
String textToDisplay = textInput.getText().toString();
/*leading and trailing whitespace omitted.*/
textToDisplay = textToDisplay.trim();
/*if text in edit text is not empty then it will add text dynamically to
listview otherwise it's won't add.*/
if (!textToDisplay.isEmpty()) {
arrayList.add(textToDisplay); //dynamically add String to arrayList
Toast.makeText(getApplicationContext(), "Item Saved", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Please Insert Text",
Toast.LENGTH_LONG).show();
}
/*reset the edit text to empty string*/
textInput.setText("");
}
});
• Enter text here
• Tap on “ADD” button
• Text added to list
Following action will take place after clicking ADD button in main activity
6. Main activity
list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/*A toast provides simple feedback about which item is clicked and it's position in a
small popup*/
Toast.makeText(getApplicationContext(), "" + (position+1) +") " +
arrayList.get(position) , Toast.LENGTH_SHORT).show();
/* getting text of item on which we clicked on ListView on screen */
String copyData = arrayList.get(position);
/* creating an intent to launch a new activity */
Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
/* putting the data in the intent to send */
intent.putExtra("myOriginalKey",copyData);
intent.putExtra("myKeyPosition",position);
/* start the activity */
startActivityForResult(intent, Edit_Mode );
}
});
Tap on a particular item to edit/delete in another activity
Following action will take place after Tapping on any the item from list
7. Second activity
btnSave.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
/* getting the data from edit text and convert it to String. */
String editedText = textcopy.getText().toString();
Toast.makeText(getApplicationContext(), "New Text Saved" ,
Toast.LENGTH_SHORT).show();
/* After getting the data from edit text and reset it to empty
String. */
textcopy.setText("");
Intent intent = new Intent();
/* putting the data in the intent to send */
intent.putExtra("myEditedKey",editedText);
intent.putExtra("myEditedKeyPosition",position);
setResult( RESULT_EDIT , intent);
SecondActivity.this.finish();
}
});
• Tap item to edit • Then edit
• Tap on “SAVE” button
• Auto return to list
Following action will take place after clicking SAVE button in Second activity
8. btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Text Deleted",
Toast.LENGTH_LONG).show();
/* Reset edit text box to empty string */
textcopy.setText("");
Intent intent = new Intent();
/* putting the data in the intent to send */
intent.putExtra("myEditedKeyPosition",position);
setResult(RESULT_DELETE, intent);
SecondActivity.this.finish();
}
});
• Tap item to edit • Tap on “DELETE” button • Auto return to list
Second activity
Following action will take place after clicking DELETE button in Second activity