如何从listview适配器中调用活动中的功能

时间:2015-02-25 06:54:33

标签: android listview adapter

有人能帮助我吗?我有4个活动A,B,C,D的tabhost。然后在活动A中,它具有带图像的自定义列表视图。如果单击其图像,则可以从活动A调用函数?我该如何解决这个案子?提前谢谢。

这是我的listview适配器源:

holder.imgitem.setOnClickListener(new OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub          
                String qty="";
                if (DataProcessorResult.get(position).getSeparate().equals("INDONESIA_NATURAL") || DataProcessorResult.get(position).getSeparate().equals("INDONESIA_WOOD"))
                {
                    qty="20";
                }else {
                    if (DataProcessorResult.get(position).getSeparate().equals("DOLCEDECOR") || (DataProcessorResult.get(position).getSeparate().equals("ILBLUA")))
                    {
                        qty="10";
                    }else {
                        if (DataProcessorResult.get(position).getItemCode().substring(0, 2).equals("SF")) {qty="10";}
                        else if (DataProcessorResult.get(position).getItemCode().substring(0, 2).equals("LC")) {qty="12";}
                        else if (DataProcessorResult.get(position).getItemCode().substring(0, 2).equals("DA")) {qty="24";}
                        else if (DataProcessorResult.get(position).getItemCode().substring(0, 2).equals("SC")) {qty="48";}
                        else if (DataProcessorResult.get(position).getItemCode().substring(0, 2).equals("FS")) {qty="48";}
                        else if (DataProcessorResult.get(position).getItemCode().substring(0, 2).equals("DT")) {qty="10";}
                        else {qty="10";}
                    }
                }

                SQLiteDatabase dbinsert = dbHelper.getWritableDatabase();
                String sqlinsert="";
                if (OtherClass.getActiveCustomerCode()!=null && !OtherClass.getActiveCustomerCode().isEmpty()) {
                    sqlinsert="INSERT INTO tcontainer (`item_code`,`CustCode`,`separate`,`qty`,`price`,`priceeuro`,`material`,`leg_color`,`nails`,`remark`,`tampil`) VALUES ("+
                             "'"+DataProcessorResult.get(position).getItemCode()+"'," +
                             "'"+OtherClass.getActiveCustomerCode()+"'," +
                             "'"+DataProcessorResult.get(position).getSeparate()+"'," +
                             "'"+qty+"'," +
                             "'0'," +
                             "'0'," +
                             "''," +
                             "''," +
                             "''," +
                             "''," +
                             "'1')";
                }else {
                    sqlinsert="INSERT INTO tcontainer (`item_code`,`CustCode`,`separate`,`qty`,`price`,`priceeuro`,`material`,`leg_color`,`nails`,`remark`,`tampil`) VALUES ("+
                             "'"+DataProcessorResult.get(position).getItemCode()+"'," +
                             "''," +
                             "'"+DataProcessorResult.get(position).getSeparate()+"'," +
                             "'"+qty+"'," +
                             "'0'," +
                             "'0'," +
                             "''," +
                             "''," +
                             "''," +
                             "''," +
                             "'1')";                    
                }
                dbinsert.execSQL(sqlinsert);
                Toast.makeText(mycontext, DataProcessorResult.get(position).getItemName()+" has been added to container", Toast.LENGTH_SHORT).show();
               // HERE to call function from activity A

            }
        });

1 个答案:

答案 0 :(得分:0)

1)创建一个界面:

public interface MyInterface{
    public void myMethod();
}

2)在您的活动中实施它:

 public class A_Activity extends Activity implements MyInterface{
        public void myMethod(){
            //call any method/function of your activity from here
        }
 }

3)然后将您的活动传递给ListAdater:

public YourAdapter extend xxxxx {
    private MyInterface myInterface;

    public MyAdapter(MyInterface myInterface){
        this.myInterface = myInterface;
    }
}

4)最后,在您的适配器中,您可以通过以下方式调用您的活动功能:myInterface.myMethod();