gpt4 book ai didi

java - 如何从 EditText 和按钮/初始化对象填充 ArrayList

转载 作者:行者123 更新时间:2023-11-29 04:22:42 28 4
gpt4 key购买 nike

你好,我如何使用 EditTextbutton 填充我的 ArrayList,问题是我不知道如何从 EditText 我可以初始化一个对象,然后将它添加到我的 ArrayList

谢谢

enter image description here

//主要 Activity

  public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ListView view = (ListView)findViewById(R.id.listview);
EditText edit = (EditText)findViewById(R.id.editText);
Button button = (Button)findViewById(R.id.button);

Todo eat = new Todo("Eat ");
Todo sleep = new Todo("Sleep");

ArrayList<Todo> todolist = new ArrayList<>();
todolist.add(eat);
todolist.add(sleep);

Todoadapter adapter = new Todoadapter(this,R.layout.custom_adapter_layout,todolist);
view.setAdapter(adapter);
}
}

//对象

public class Todo {
private String todo;
public Todo(String todo) {
this.todo = todo;
}
public String getTodo() {
return todo;
}
public void setTodo(String todo) {
this.todo = todo;
}
}

//数组适配器

public class Todoadapter extends ArrayAdapter<Todo> {

private Context mcontext;
int mresource;
private List<Todo> objects = new ArrayList<>();

public Todoadapter(@NonNull Context context, int resource, @NonNull ArrayList<Todo> objects) {
super(context, resource, objects);
this.mresource = resource;
this.mcontext = context;
this.objects = objects;
}

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {

String todo = getItem(position).getTodo();
LayoutInflater inflater = LayoutInflater.from(mcontext);
convertView = inflater.inflate(mresource, parent, false);
final CheckBox box = (CheckBox) convertView.findViewById(R.id.checkBox2);
box.setText(todo);
box.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(box.isChecked()){
//Delay to see animation
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
objects.remove(getItem(position)); //Your object list where you have the itens
notifyDataSetChanged(); //If you are using a recycler view.
}
},300); //adding 0.3 sec delay
}
}
});
return convertView;

}

}

enter image description here

最佳答案

首先在 XML 或通过 java 中为按钮添加监听器

activity_main

<Button ...
android:id="@+id/button"
android:onClick="addTodo"/>

MainActivity.java

1.) 使适配器和列表实例成为类的全局实例

2.) 创建addTodo方法(保持公开并添加View参数)

3.) 点击后,从edittext中获取数据,创建一个Todo实例并添加到列表中

4.) 通知适配器反射(reflect)变化

public class MainActivity extends AppCompatActivity {
// 1
private ArrayList<Todo> todolist;
private Todoadapter adapter;
private EditText edit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ListView view = (ListView)findViewById(R.id.listview);
edit = (EditText)findViewById(R.id.editText);
Button button = (Button)findViewById(R.id.button);

Todo eat = new Todo("Eat ");
Todo sleep = new Todo("Sleep");

todolist = new ArrayList<>();
todolist.add(eat);
todolist.add(sleep);

adapter = new Todoadapter(this,R.layout.custom_adapter_layout,todolist);
view.setAdapter(adapter);
}
// 2
public void addTodo(View view){
// 3
todolist.add(new Todo(edit.getText().toString()));
// 4
adapter.notifyDataSetChanged();
}
}

关于java - 如何从 EditText 和按钮/初始化对象填充 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48137383/

28 4 0