网格视图setOnItemClickListener()不适用于自定义网格视图

时间:2015-09-24 07:11:32

标签: android android-gridview onitemclicklistener

我有一个按钮,当我点击该按钮时,我的第一个自定义网格视图弹出为对话框。当我单击某个项目时,我的第二个自定义网格视图会弹出,从而忽略我的第一个自定义网格视图。现在问题在这里,当我点击第二个自定义网格视图中的项目时没有任何反应。我的setOnItemClickListener()没有在那里工作。我无法在我做错的地方追查我的问题。

显示我的第一个网格视图`

    public void outletList() {
    dialogOutlet = new Dialog(SelectCategory.this);

    dialogOutlet.setContentView(R.layout.outlet_dialogue_grid);
    dialogOutlet.setTitle("Select Outlet");

    final GridView lv = (GridView) dialogOutlet.findViewById(R.id.gridOutletView);

    oa = new OutletAdapter(this, ParseData.OutletList);

    lv.setAdapter(oa);
    outletFlag = 1;

    lv.setOnItemClickListener(this);
    dialogOutlet.show();


    dialogOutlet.setOnDismissListener(new OnDismissListener() {

        @Override
        public void onDismiss(DialogInterface dialog) {
            // TODO Auto-generated method stub
            outletFlag = 0;
        }
    });

  }`

显示第二个网格视图`

    public void billList() {
    dialogueBill = new Dialog(SelectCategory.this);

    dialogueBill.setContentView(R.layout.bill_dialogue_grid);
    dialogueBill.setTitle("Select Bill");

    final GridView bv = (GridView) dialogueBill.findViewById(R.id.gridBillView);

    ba = new BillAdapter(this, ParseData.BillList);

    bv.findFocus();

    bv.setAdapter(ba);
    bv.setFocusableInTouchMode(true);
    bv.requestFocus();
    bv.setClickable(true);
    bv.setFocusable(true);
    System.out.println("focusable "+bv.isFocusableInTouchMode());
    System.out.println("focusable "+bv.findFocus());

    System.out.println("Outlet Flag Bill List>>>>>>"+outletFlag);

    bv.setOnItemClickListener(this);

    dialogueBill.show();

    dialogueBill.setOnDismissListener(new OnDismissListener() {

        @Override
        public void onDismiss(DialogInterface dialog) {
            // TODO Auto-generated method stub
            outletFlag = 0;
        }
    });

}`

在项目上单击

public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {

    System.out.println("Outlet Flag inside Item Click>>>>>>"+outletFlag);

    if(outletFlag!=1){

        HashMap<String, String> out = BillAdapter.billData.get(pos);
        String sel_bill = out.get(ParseData.KEY_BILL_NO);

        System.out.println("Selected Bill>>>>>>"+sel_bill);

        selected_bill = sel_bill;


        Intent iii = new Intent(SelectCategory.this, FBHome.class);
        startActivity(iii);


    }
    else{

        HashMap<String, String> out = OutletAdapter.outletData.get(pos);
        String sel_name = out.get(ParseData.KEY_NAME);
        String sel_code = out.get(ParseData.KEY_CODE);
        out_pos = pos;
        selected_outlet = sel_code;
        selected_outlet_name = sel_name;
        dialogOutlet.dismiss();
        outletFlag = 0;
        new GetBillDetails().execute();


    }

}

提前致谢:)

2 个答案:

答案 0 :(得分:3)

在你的Gridview布局中,如果你使用像Button或ImageButton这样的Clickable Widgets,请改变它并尝试。

那是android无法识别Gridadpater视图中的点击事件,所以如果你改变那一切应该可以正常工作

你可以尝试

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@android:color/darker_gray"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/bt_grid"
        android:layout_width="wrap_content"
        android:textColor="@android:color/white"
        android:layout_height="wrap_content" />

</LinearLayout>

答案 1 :(得分:0)

尝试使用警报对话框

 private void showGridDialog() {
            // Prepare grid view
    final GridView bv = (GridView) 
     dialogueBill.findViewById(R.id.gridBillView);

      ba = new BillAdapter(this, ParseData.BillList);

      bv.findFocus();

      bv.setAdapter(ba);
      bv.setFocusableInTouchMode(true);
      bv.requestFocus();
      bv.setClickable(true);
      bv.setFocusable(true);
            bv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    // do something here
                }
            });

            // Set grid view to alertDialog
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setView(bv);
            builder.setTitle("Goto");
            builder.show();
        }