当我点击一个项目时,如何点击从gridview弹出的对话框内的按钮。这个gridview在片段内?
它不允许我为这个ok按钮创建onclicklistener。 错误提示: 类型View中的方法setOnClickListener(View.OnClickListener)不适用于参数(new DialogInterface.OnClickListener(){})
这是我的代码:
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MotionEventCompat;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class DiningFragment extends Fragment{
GridView gridView;
TextView Details1;
TextView Details2;
static final String[] NAMES = new String[] { "aroma", "aura", "crave",
"urbanbean", "flava", "map" };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View inflatedView = inflater.inflate(R.layout.dining, container,
false);
Details1 = (TextView) inflatedView.findViewById(R.id.dining_details1);
Details2 = (TextView) inflatedView.findViewById(R.id.dining_details2);
gridView = (GridView) inflatedView.findViewById(R.id.gridView);
gridView.setAdapter(new DiningAdapter(getActivity(), NAMES));
String textLinks1 = "tesetestsetest";
String textLinks2 = "testtest";
if (Details1 != null) {
// set text to text view
Details1.setText(textLinks1);
Linkify.addLinks(Details1, Linkify.ALL); // linkify all links in
// text.
// you can set link color with the help of text view property
Details1.setLinkTextColor(Color.BLUE);
}
if (Details2 != null) {
// set text to text view
Details2.setText(textLinks2);
Linkify.addLinks(Details2, Linkify.ALL); // linkify all links in
// text.
// you can set link color with the help of text view property
Details2.setLinkTextColor(Color.BLUE);
}
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(
getActivity().getApplicationContext(),
((TextView) v.findViewById(R.id.dining_label))
.getText(), Toast.LENGTH_SHORT).show();
**// dialog
final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.dining_dialog);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.dining_dialogtext);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.dining_dialogimage);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dining_dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();**
}
});
return inflatedView;
}
}
答案 0 :(得分:0)
我的工具不正确的OnClickListener,尝试实现View.OnClickListener而不是DialogInterface.OnClickListener。
使用import
导入View.OnClickListener
而不是
import DialogInterface.OnClickListener