【发布时间】:2015-10-23 07:36:42
【问题描述】:
我希望我的标题有意义,基本上我在列表视图中有一个按钮,一个布局作为片段内列表视图中的适配器。
我的问题是我的 onClick 方法 deleteQuote(View v) 在片段类中声明时无法识别。它出错了一些关于在主要活动中找不到的东西。所以我将它转移到主要活动中,但问题是我需要视图标记通过 getTag 获取条目中的主键,以删除数据库和列表视图适配器中的该条目。我应该把我的 onClick 方法放在哪里我仍然可以获取视图标签或者实现 setonclicklisteners 的最佳方法是什么?
片段类
public class QuotationList extends Fragment{
ListView lvQuotationsMotorcar;
ArrayList<Quotation> menuList_motorcar;
CustomMotorcarQuoteAdapter cmqa;
ListView lvQuotationsFire;
ArrayList<Quotation> menuList_fire;
CustomFireQuoteAdapter cfqa;
Button btnDeleteQuote_motorcar;
Spinner spQuotationType;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_online_quotation_list, container, false);
lvQuotationsMotorcar = (ListView) rootView.findViewById(R.id.lvQuotationsMotorcar);
menuList_motorcar = new ArrayList<Quotation>();
cmqa = new CustomMotorcarQuoteAdapter(getActivity(), R.layout.motorcarquotationlayout, menuList_motorcar);
lvQuotationsMotorcar.setAdapter(cmqa);
lvQuotationsFire = (ListView) rootView.findViewById(R.id.lvQuotationsFire);
menuList_fire = new ArrayList<Quotation>();
cfqa = new CustomFireQuoteAdapter(getActivity(), R.layout.firequotationlayout, menuList_fire);
lvQuotationsFire.setAdapter(cfqa);
final SQLiteDatabase mydatabase = getActivity().openOrCreateDatabase("INLIS", Context.MODE_PRIVATE, null);
spQuotationType = (Spinner) rootView.findViewById(R.id.spQuotationType);
btnDeleteQuote_motorcar = (Button) getActivity().findViewById(R.id.btnDeleteMotorcarQuote);
Cursor resultSet = mydatabase.rawQuery("Select * from QuotationMotorcar", null);
if (resultSet != null){
if (resultSet .moveToFirst()) {
while (!resultSet.isAfterLast()) {
//stuff
cmqa.add(q);
resultSet.moveToNext();
}
}
}else{
Toast.makeText(getActivity(), "No Pending Motorcar Quotations", Toast.LENGTH_LONG).show();
}
Cursor resultSet1 = mydatabase.rawQuery("Select * from QuotationFire", null);
if (resultSet1 != null){
if (resultSet1 .moveToFirst()) {
while (!resultSet1.isAfterLast()) {
//stuff
cfqa.add(q);
resultSet1.moveToNext();
}
}
}else{
Toast.makeText(getActivity(), "No Pending Fire Quotations", Toast.LENGTH_LONG).show();
}
mydatabase.close();
//more code
return rootView;
}
onClick 方法 deleteQuote
public void deleteQuote_motorcar(final View v){
final SQLiteDatabase mydatabase = getActivity().openOrCreateDatabase("INLIS", Context.MODE_PRIVATE, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setTitle("Warning!");
alertDialogBuilder
.setMessage("Are you sure you want to delete this quote?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Quotation itemToRemove = (Quotation)v.getTag();
cmqa.remove(itemToRemove);
mydatabase.execSQL("DELETE FROM QoutationMotorcar WHERE QuoteNumber =" + itemToRemove.getQuoteNumber() + "; ");
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
如果它只是扩展一个活动类,它就可以很好地工作。希望有人可以提供帮助。我真的很生气 onClick 对 Fragments 不起作用。
我如何以及在哪里声明我的方法可以使用视图标签正确调用?
【问题讨论】:
-
为了处理行,你可以使用 listview.setonitemclick 代替在适配器内设置 onclick
-
@AjayPandya 但是我在布局条目中有另一个按钮也需要标签。另一个获取视图标记并将其发送到数据库服务器。我刚刚在我的示例中显示了 1,这是人们理解的删除部分。
-
从哪里调用删除方法
标签: android android-layout onclicklistener