Android - onClick按钮无法在片段内的列表视图中的适配器中工作

时间:2015-10-23 07:36:42

标签: android android-layout onclicklistener

我希望我的标题有意义,基本上我在listview中有一个按钮,作为片段内listview中的适配器布局。

我的问题是当我在片段类中声明它时,我的onClick方法deleteQuote(View v)无法被识别。它错误地显示了主要活动中未找到的内容。所以我将它转移到主活动,但问题是我需要view标签通过getTag在条目中获取主键,以删除数据库和listview适配器中的条目。我应该把我的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如何处理碎片。

如何以及在何处声明我的方法可以使用视图标记正确调用?

1 个答案:

答案 0 :(得分:0)

在自定义适配器OnClickListener方法中使用此getView声明

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.your_lv_item_layout, parent, false);
    view.findViewById(R.id.your_bt).setOnClickListener(new View.OnClickListener(){
         @Override
         public void onClick(View arg0) {
            //do what ever you want
         }
    });

    return view;
  }

而不是public void deleteQuote_motorcar(final View v)