【发布时间】:2018-01-19 16:26:28
【问题描述】:
listview 中的每个项目都有一个自定义布局。但布局中的按钮无法识别 MainActivity 中的活动。
ListView 显示在主视图中。 每个项目的布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txt_bookname_listview"
android:layout_width="234dp"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="@+id/btn_renewed_listview"
android:layout_alignParentTop="true"
android:textAlignment="center"
android:gravity="center"
android:textStyle="bold"
android:textSize="18dp"
android:layout_toStartOf="@+id/btn_delete_book" />
<Button
android:id="@+id/btn_renewed_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Renewed"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textAllCaps="false"
android:background="@android:color/holo_green_light"
android:onClick="clickedRenew"
/>
<Button
android:id="@+id/btn_delete_book"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/btn_renewed_listview"
android:background="@android:color/holo_red_dark"
android:onClick="clickedDelete"
android:text="Delete"
android:textAllCaps="false" />
</RelativeLayout>
函数位于 MainActivity.java 文件中。 与按钮相关的功能如下:
public void clickedRenew(View view)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
TextView textView = new TextView(getApplicationContext());
textView.setText("Have You renewed the Book?");
textView.setPadding(10,10,10,10);
textView.setGravity(Gravity.CENTER);
textView.setTypeface(null, Typeface.BOLD);
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
builder.setCustomTitle(textView)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create().show();
}
public void clickedDelete(View view)
{
Log.d("LetMeDelete", "here");
final View view1 = view;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
TextView textView = new TextView(getApplicationContext());
textView.setText("Have You retured the book?");
textView.setPadding(10,10,10,10);
textView.setGravity(Gravity.CENTER);
textView.setTypeface(null, Typeface.BOLD);
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
textView.setTextColor(Color.RED);
builder.setCustomTitle(textView)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
View parent = (View) view1.getParent();
TextView bookname = (TextView) parent.findViewById(R.id.txt_bookname_listview);
String bookName = bookname.getText().toString();
database.deleteData(bookName);
updateListView(database.getAllData());
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create().show();
}
public void updateListView(Cursor data)
{
ArrayList<String> arrayList = new ArrayList<>();
while (data.moveToNext())
{
arrayList.add(data.getString(1));
}
if(arrayAdapter == null)
{
arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.item_book_name,R.id.txt_bookname_listview, arrayList);
listView.setAdapter(arrayAdapter);
}
else
{
arrayAdapter.clear();
arrayAdapter.addAll();
arrayAdapter.notifyDataSetChanged();
arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.item_book_name, R.id.txt_bookname_listview, arrayList);
listView.setAdapter(arrayAdapter);
}
data.close();
database.close();
}
android studio的logcat如下:
01-20 04:08:06.081 32344-32344/com.example.imad.bookrenewalert E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.imad.bookrenewalert, PID: 32344
java.lang.IllegalStateException: Could not find method clickedDelete(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.Button with id 'btn_delete_book'
at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:5368)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5327)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24697)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
我现在已经检查了很多次拼写,但我找不到任何错误。 clickedDelete 方法已完全实现,但 clickedRenew 未实现。 logcat 中显示的错误是在单击 listview 项中的 Delete 按钮之后。
【问题讨论】:
-
在哪个XML中,你把按钮放在了哪个类中,
clickedDelete方法存在于哪个类中? -
@SrikarReddy 我将按钮放置在自定义布局中以显示列表视图项,列表视图显示在主布局中,这些功能位于 MainActivity.Java 文件中。
-
您将该方法放在了错误的 Java 文件中。我认为您可能会夸大适配器中的自定义布局。如果您使用的是 Recyclerview,那么您已经为视图持有者中的各个项目设置了点击侦听器。
-
@SrikarReddy 我在 MainActivity 的 ArrayAdapter 方法中使用了自定义布局。但主布局使用相对布局。
-
@MikeM.Cheers。好的
标签: android listview button onclick