我试图在Android中制作待办事项列表应用。这是我的代码
MainActivity.java
import com.vrishankgupta.adapter.myPkg.listClass;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<listClass> arr = new ArrayList<listClass>(0);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arr.add(new listClass("Task 1",false));
arr.add(new listClass("Task 2",true));
arr.add(new listClass("Task 3",true));
arr.add(new listClass("Task 4",false));
final CustomAdapter adapter = new CustomAdapter(this,arr);
final ListView lv = findViewById(R.id.vishulv);
Log.e("Task 1", arr.get(0).isActive()+"" );
lv.setAdapter(adapter);
final EditText etnew = findViewById(R.id.etNew);
Button add = findViewById(R.id.addButn);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(etnew.getText()==null || etnew.getText().toString().equals(""))
{
Toast.makeText(getApplicationContext(),"Enter data",Toast.LENGTH_LONG).show();
}
else
{
arr.add(new listClass(etnew.getText().toString(),false));
CustomAdapter adapter = new CustomAdapter(MainActivity.this,arr);
final ListView lv = findViewById(R.id.vishulv);
lv.setAdapter(adapter);
etnew.setText("");
}
}
});
}
}
CustomAdapter
package com.vrishankgupta.adapter;
public class CustomAdapter extends ArrayAdapter<listClass> {
ArrayList<listClass> arr;
public CustomAdapter(@NonNull Context context, ArrayList<listClass> arr) {
super(context,R.layout.detail, arr);
this.arr = new ArrayList<listClass>(0);
this.arr.addAll(arr);
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull final ViewGroup parent) {
final ViewHolder holder;
if(convertView == null)
{
holder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.detail,parent,false);
holder.textview = (TextView)convertView.findViewById(R.id.todoTaskTV);
holder.checkBox = (CheckBox)convertView.findViewById(R.id.checkBut);
holder.button = (Button)convertView.findViewById(R.id.delBut);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
listClass task = arr.get(position);
Log.e("Checking", position + task.getTask().toString() );
holder.textview.setText(task.getTask());
holder.checkBox.setChecked(task.isActive());
holder.checkBox.setTag(task);
holder.button.setTag(task);
notifyDataSetChanged();
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox)v;
listClass task = (listClass) cb.getTag();
Toast.makeText(getContext(),"Checkbox",Toast.LENGTH_LONG).show();
task.setActive(cb.isChecked());
Log.e("isActive Task1", arr.get(1).isActive() +"" );
}
});
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button im = (Button) v;
listClass task = (listClass) im.getTag(R.id.delBut);
arr.remove(task);
Log.e("Del but",position + "");
Toast.makeText(getContext(),"Del",Toast.LENGTH_LONG).show();
}
});
Log.e("isActive Task1", arr.get(1).isActive() +"" );
return convertView;
}
}
ViewHolder类
public class ViewHolder {
TextView textview;
Button button;
CheckBox checkBox;
}
listClass
public class listClass {
String task;
boolean active;
public listClass(String task, boolean active) {
this.task = task;
this.active = active;
}
public String getTask() {
return task;
}
public void setTask(String task) {
this.task = task;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.vrishankgupta.adapter.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter New Task"
android:id="@+id/etNew"
android:textSize="24sp"
android:layout_gravity="left"
android:gravity="left"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="ADD"
android:id="@+id/addButn"
android:textSize="24sp"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/vishulv"
android:layout_marginTop="94dp"
/>
</FrameLayout>
detail.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="left"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/todoTaskTV"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:paddingLeft="8dp"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/delBut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Delete"
android:layout_marginRight="5dp"
/>
<CheckBox
android:id="@+id/checkBut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/delBut"
android:layout_centerVertical="true"
android:layout_marginRight="10dp" />
</RelativeLayout>
我想删除单击删除按钮上的List元素或用checkBox切换任务状态,我甚至没有看到Checkbox和Delete Button的clickListener中出现的Toast,只是什么都没发生,无法弄明白怎么做..
答案 0 :(得分:0)
在你的适配器中试试这个
holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isChecked()) {
//checked
Toast.makeText(getContext(),"active checkbox",Toast.LENGTH_LONG).show();
}
else
{
//not checked
Toast.makeText(getContext(),"inactive checkbox",Toast.LENGTH_LONG).show();
}
});
答案 1 :(得分:0)
制作回调界面以解决您的问题。我认为这是你想达到的最佳方式。
OnItemClickListener
public class OnItemClickListener implements View.OnClickListener {
private int position;
private OnItemClickCallback onItemClickCallback;
public OnItemClickListener(int position, OnItemClickCallback
onItemClickCallback) {
this.position = position;
this.onItemClickCallback = onItemClickCallback;
}
@Override
public void onClick(View view) {
onItemClickCallback.onItemClicked(view, position);
}
public interface OnItemClickCallback {
void onItemClicked(View view, int position);
}
}
在您的适配器类
中private OnItemClickListener.OnItemClickCallback onItemClickCallback;
public CustomAdapter(@NonNull Context context, ArrayList<listClass> arr,
OnItemClickListener.OnItemClickCallback onItemClickCallback)
{
super(context,R.layout.detail, arr);
this.arr = new ArrayList<listClass>(0);
this.onItemClickCallback = onItemClickCallback;
this.arr.addAll(arr);
}
和
holder.imageButton.setOnClickListener(new OnItemClickListener(position,
onItemClickCallback));
在您的活动中
private OnItemClickListener.OnItemClickCallback
onItemDeleteClickCallback = new
OnItemClickListener.OnItemClickCallback() {
@Override
public void onItemClicked(View view, int position) {
//here you will get delete item pos
}
};
和
CustomAdapter adapter = new CustomAdapter(MainActivity.this, arr,
onItemDeleteClickCallback );
final ListView lv = findViewById(R.id.vishulv);
lv.setAdapter(adapter);
希望这会对你有所帮助。