我正在使用自定义适配器在listview中显示联系人,我创建onClick功能,当任何项目点击时打开自定义对话框 . 之后,我想从对话框中获取联系人号码,但是当我尝试获取错误弹出窗口时 .
IllegalStateException: Could not execute method for android:onClick
Custom Dialog From Custom Adapter
// Other code
// This code is working fine problem is in activity class
public void onClick(View v) {
Toast.makeText(context, "Item click", Toast.LENGTH_SHORT).show();
String phoneNumber = phone.getText().toString();
String userName = name.getText().toString();
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle(userName);
EditText etxtContactNumber = (EditText) dialog.findViewById(R.id.etxtContactNumber);
etxtContactNumber.setText(phoneNumber);
dialog.show();
}
// reset of the code
Custom Dialog
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="@+id/etxtContactNumber" />
<Button
android:text="Send SMS"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnSendMessage"
android:onClick="sendMessage" />
<Button
android:text="Phone Call"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnPhoneCall"
android:onClick="phoneCall" />
Main Activity
protected void sendMessage(View view){
Toast.makeText(this, "Send Message", Toast.LENGTH_SHORT).show();
EditText etxtContactNumber = (EditText) view.findViewById(R.id.etxtContactNumber);
String phoneNumber = etxtContactNumber.getText().toString();
String uri= "smsto:"+phoneNumber;
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
startActivity(intent);
}
我知道错误的原因 etxtContactNumber
不在此视图中 . 这也不在主要活动视图中,然后它是如何获得它的 .
Custom adapter and MainActivity both are two different files
3 回答
您不需要自定义适配器中的etxtContactNumber文本 .
您必须创建customDialog.java类并在onCreate()下面执行以下代码 .
您必须在自定义适配器的onClick上执行以下代码 .
希望这能解决您的问题 .
如果要在customAdapter中创建
dialog
并想要在MainActivity中获取值,只需创建对话框public static
并在承包商中初始化它Custom Adapter
Main Activity
请在下面的行中输入自定义视图
然后呢