这是我的ArrayAdapter代码。当我点击LinearLayout“cat”时,它会在dialog.show()上给出错误。我不知道如何在ArrayAdapter类中创建自定义对话框。 当我删除创建对话框部分时,一切正常。
提前致谢
CategoryAdapter.java
public class CategoryAdapter extends ArrayAdapter<String> {
private final Context context;
String[] menu = new String[25] ;
String[] menu2 = new String[25];
String[] menu3 = new String[25];
private LayoutInflater inflater;
viewholder vh;
public CategoryAdapter(Context context, String [] menu,String [] menu2,String [] menu3) {
super(context, R.layout.categoryadapter, menu);
this.context = context;
this.menu = menu;
this.menu2=menu2;
this.menu3=menu3;
}
public View getView(final int position, View convertView, ViewGroup parent) {
{
vh=new viewholder();
if (inflater == null)
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.categoryadapter, parent, false);
vh.cat=(LinearLayout) convertView.findViewById(R.id.category);
convertView.setTag(vh);
}
vh.cat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(getContext());
dialog.setContentView(R.layout.update_categore_dialog);
dialog.setTitle("Update Your Category");
dialog.show();
Toast.makeText(getContext(), "Clicked", Toast.LENGTH_LONG).show();
}
});
return convertView;
}
public class viewholder
{
LinearLayout cat;
}
}
答案 0 :(得分:5)
使用context
代替getContext()
final Dialog dialog = new Dialog(context);
最后,请传递context
vh.cat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.update_categore_dialog);
dialog.setTitle("Update Your Category");
dialog.show();
Toast.makeText(getContext(), "Clicked", Toast.LENGTH_LONG).show();
}
});
答案 1 :(得分:0)
尝试 -
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.update_categore_dialog);
dialog.setTitle("Update Your Category");
dialog.show();
答案 2 :(得分:0)
尝试以下链接,希望你喜欢它。
holder.btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder alert = new AlertDialog.Builder((Activity)_context);
alert.setMessage("Do you want to delete?");
alert.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int whichButton) {
dialog.cancel();
}
});
alert.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int whichButton) {
OrderDetailListAdatper.this._listOrderListInfoAdapter
.remove(position); OrderDetailListAdatper.thisnotifyDataSetChanged();
}
});
alert.create().show(); // btw show() creates and shows it..
}
});
在你的适配器getview中,粘贴该代码,它对我来说很好。
abstract class BasicTable[T](tag: Tag, tableName: String)
extends Table[T](tag, tableName) {
def id = column[Option[Long]]("ID", O.PrimaryKey, O.AutoInc)
def id1 = column[Long]("ID1")
def id2 = column[String]("ID2")
def idn = column[String]("IDn")
}
class T1(tag: Tag) extends BasicTable[caseClassA](tag, "A") {
...
override def * = ...
}
答案 3 :(得分:0)
在getView()
方法中,将getContext()
替换为context
变量。
vh.cat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
//remaining code...
}
});
在初始化CateogoryAdapter
时,请不要传递getApplicationContext()
,而是传递上下文(Activity.this
)。
CategoryAdapter adapter = CategoryAdapter(YourActivity.this, menu, menu2, menu3)