Answer the question
In order to leave comments, you need to log in
How to implement element editing in ListView -- Android by button?
Hello, help me implement editing an element on a button click. The first time you click on the list item, the content is copied to the editText, the next time you change the text and then click on the button, the selected item should be replaced with what is in the editText
Here is the MainActivity.java code:
package com.example.chasie.myapp;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.manuelpeinado.multichoiceadapter.MultiChoiceArrayAdapter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView listView;
MyArrayAdapter adapter;
Toast toast;
EditText editText;
Button btn_add, btn_edit, btn_clear, btn_delete;
String[] names;
//final ArrayList<String> items = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_add = (Button) findViewById(R.id.btn_add);
Button btn_edit = (Button) findViewById(R.id.btn_edit);
Button btn_clear = (Button) findViewById(R.id.btn_clear);
Button btn_delete = (Button) findViewById(R.id.btn_delete);
final ListView listView = (ListView) findViewById(android.R.id.list);
final ArrayList<String> items = new ArrayList<String>();
final EditText editText = (EditText) findViewById(R.id.edit_text);
adapter = new MyArrayAdapter(savedInstanceState, this, items);
adapter.setAdapterView(listView);
adapter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
adapter.setItemChecked(position, true);
toast.makeText(getApplicationContext(), "Item click: " + adapter.getItemId(position), Toast.LENGTH_SHORT).show();
String itemString = adapter.getItem(position).toString();
editText.setText(itemString);
}
});
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
items.add(editText.getText().toString());
adapter.notifyDataSetChanged();
editText.setText("");
}
});
btn_edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//int index = items.;
items.set(0, editText.getText().toString());
adapter.notifyDataSetChanged();
}
});
btn_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
items.clear();
adapter.notifyDataSetChanged();
}
});
btn_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String delete_string = editText.getText().toString();
items.remove(delete_string);
adapter.notifyDataSetChanged();
}
});
}
@Override
protected void onSaveInstanceState(Bundle outState) {
adapter.save(outState);
}
public class MyArrayAdapter extends MultiChoiceArrayAdapter<String> {
public MyArrayAdapter(Bundle savedInstanceState, Context context, List<String> items) {
super(savedInstanceState, context, R.layout.my_item_checkable, android.R.id.text1, items);
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.my_action_mode, menu);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (item.getItemId() == R.id.menu_share) {
Toast.makeText(getContext(), "Share", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question