gpt4 book ai didi

java - 将onClickListener与ListView中的CheckBoxes一起使用

转载 作者:行者123 更新时间:2023-12-02 12:10:56 26 4
gpt4 key购买 nike

因此,我一直在尝试在切换复选框时进行注册。到目前为止,我还没有运气。 onClickListener是否适合这种情况?当我尝试使用它时,出现一个错误。

MainActivity.java

  private static final String TAG = "MainActivity";
private HabitHelper mHelper;
private ListView mHabitView;
private ArrayAdapter<String> mAdapter;

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

mHelper = new HabitHelper(this);
mHabitView = (ListView) findViewById(R.id.habitList);

FloatingActionButton myFab = (FloatingActionButton) findViewById(R.id.addHabit);
CheckedTextView chkBox = (CheckedTextView) findViewById(R.id.habitItem);

myFab.setOnClickListener(this);


chkBox.setOnClickListener(this);

updateUI();
}

public void onClick (View v){

final EditText habitText = new EditText(this);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Add a new habit")
.setMessage("What habit would you like to start working on?")
.setView(habitText)
.setPositiveButton("Add", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
String habit = String.valueOf(habitText.getText());
SQLiteDatabase db = mHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(HabitContract.HabitEntry.COLUMN_NAME_TITLE, habit);
db.insertWithOnConflict(HabitContract.HabitEntry.TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE);
db.close();
updateUI();

}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();

}


public class CheckBoxClick implements AdapterView.OnItemClickListener{

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
CheckedTextView ctv = (CheckedTextView)arg1.findViewById(R.id.habitItem);
if (ctv.isChecked()){
Toast.makeText(MainActivity.this, "You have completed your habit for the day", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "Make sure you complete your task before checking me!", Toast.LENGTH_SHORT).show();
}
}
}

private void updateUI(){
ArrayList<String> habitList = new ArrayList<>();
SQLiteDatabase db = mHelper.getReadableDatabase();
Cursor cursor = db.query(HabitContract.HabitEntry.TABLE, new String[]{HabitContract.HabitEntry._ID, HabitContract.HabitEntry.COLUMN_NAME_TITLE}, null, null, null, null, null);
while (cursor.moveToNext()){
int idx = cursor.getColumnIndex(HabitContract.HabitEntry.COLUMN_NAME_TITLE);
habitList.add(cursor.getString(idx));
}
if (mAdapter == null){
mAdapter = new ArrayAdapter<>(this, R.layout.item_habit, R.id.habitItem, habitList);
mHabitView.setAdapter(mAdapter);
}
else {
mAdapter.clear();
mAdapter.addAll(habitList);
mAdapter.notifyDataSetChanged();
}
cursor.close();
db.close();
}

private void deleteHabit(View view){
View parent = (View) view.getParent();
TextView habitTextView = (TextView) parent.findViewById(R.id.habitItem);
String habit = String.valueOf(habitTextView.getText());
SQLiteDatabase db = mHelper.getWritableDatabase();
db.delete(HabitContract.HabitEntry.TABLE, HabitContract.HabitEntry.COLUMN_NAME_TITLE + " = ?", new String[] {habit});
db.close();
updateUI();
}}


activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.example.jonathan.myapplication.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">


<ListView
android:id="@+id/habitList"
android:layout_width="0dp"
android:layout_height="0dp"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/addHabit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:fabSize="mini"
app:srcCompat="@android:drawable/ic_input_add"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintRight_toRightOf="parent"
tools:ignore="RtlHardcoded" />
</android.support.constraint.ConstraintLayout>


item_habit.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">


<CheckedTextView
android:id="@+id/habitItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:padding="5dp"
android:checked="false"
android:clickable="true"
android:focusable="true"
android:text=""
/>

</RelativeLayout>


错误

java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.jonathan.myapplication / com.example.jonathan.myapplication.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.CheckedTextView。在空对象引用上的setOnClickListener(android.view.View $ OnClickListener)'
                                                                                  在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                                  在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                                  在android.app.ActivityThread.-wrap11(未知来源:0)
                                                                                  在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1593)
                                                                                  在android.os.Handler.dispatchMessage(Handler.java:105)
                                                                                  在android.os.Looper.loop(Looper.java:164)
                                                                                  在android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                  在java.lang.reflect.Method.invoke(本机方法)
                                                                                  在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)
                                                                                  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                               引起原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.widget.CheckedTextView.setOnClickListener(android.view.View $ OnClickListener)”
                                                                                  在com.example.jonathan.myapplication.MainActivity.onCreate(MainActivity.java:50)
                                                                                  在android.app.Activity.performCreate(Activity.java:6975)
                                                                                  在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                                                  在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                                                  在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                                  在android.app.ActivityThread.-wrap11(未知来源:0)
                                                                                  在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1593)
                                                                                  在android.os.Handler.dispatchMessage(Handler.java:105)
                                                                                  在android.os.Looper.loop(Looper.java:164)
                                                                                  在android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                  在java.lang.reflect.Method.invoke(本机方法)
                                                                                  在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)
                                                                                  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

最佳答案

您没有为Lis​​tView创建视图,而CheckedTestView不在布局内。

您尝试为其设置侦听器的位置实际上应该在将创建CheckedTestViews的ListAdapter内。

我看到,您使用了CursorAdapter,可以检查单击次数,然后进入listView槽:

 listView.setOnItemClickListener(new OnItemClickListener(){/**/});


但是,您必须删除chb.setOnClickListener(this),因为它们是列表中的项目,而不是布局中的单个视图。

关于java - 将onClickListener与ListView中的CheckBoxes一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46552958/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com