2016-06-11 58 views
0

所以我有一個自定義適配器的listView,我希望每當我按下按鈕,我可以從MainActivity的OnItemClickListener監聽它。如何通過listView中的按鈕訪問主Activity中的數據?

所以我的問題是,當我點擊ListView中的按鈕時,我會傳遞什麼,以便我可以從主Activity讀取它?

@Override 
public View getView(final int pos, View convertView, ViewGroup parent) { 
    Holder holder = new Holder(); 
    final CustomProductAdapter p=this; 
    final View rowView; 
    rowView = inflater.inflate(R.layout.product_list_test, null); 
    holder.productView=(TextView) rowView.findViewById(R.id.productView); 
    holder.descView=(TextView) rowView.findViewById(R.id.descView); 
    holder.imageView=(ImageView) rowView.findViewById(R.id.imageView); 
    holder.productCost=(TextView) rowView.findViewById(R.id.productCost); 
    holder.iButton=(ImageButton) rowView.findViewById(R.id.imageButton); 
    holder.iButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 


      //What will I put here? 


     } 
    }); 
    holder.productView.setText(proNames[pos]); 
    holder.descView.setText(R.string.description_product); 
    holder.productCost.setText("0.00"); 
    ImageView image =holder.imageView; 
    imageloader.DisplayImage(images[pos],image); 

    rowView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(context, "You Clicked "+proNames[pos], Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    return rowView; 
} 

,這樣我可以從這裏像這樣讀它在我的MainActivity.java(我不知道這是否是corect方式)

listView = (ListView)findViewById(R.id.listView); 
    listView.setAdapter(new CustomProductAdapter(this, images, proNames1, desc)); 
    AdapterView.OnItemClickListener mMessageClickedHandler = new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView parent, View v, int position, long id) { 
      // Do something in response to the click 
      long ids=v.getId(); 
      if(ids==R.id.imageButton){ 
       Toast.makeText(getBaseContext(),"Button clicked at "+position,Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }; 
    listView.setOnItemClickListener(mMessageClickedHandler); 

。我是初學者,所以請幫助我,如果我不正確。

回答

1

使用接口進行通信是實現此目的的方法之一。

//create an interface 
interface ButtonClickNotify{ 
    void onBUttonClick(int position); 
} 

然後實現它的主要活動

public calss MainActivity extends AppCompatActivity implements ButtonClickNotify{ 

    //Override the interface method 
    @Override 
    public void onButtonClick(int position){ 
     //do something 
    } 
} 

然後調用從適配器接口。

//declare an instance variable 
ButtonClickNotify buttonNotify; 
//Initiate the iterface in the constructor 
public MyListAdapter(Context context /*and other parameters*/){ 
    //after other constructor methods including calling super 
    try{ 
     buttonNotify=(MainActivity)context; 
    }catch(Throwable e){ 
     //interface is not implemented 
    } 
} 

//at the button onclick 
holder.iButton.setOnClickListener(new View.OnClickListener{ 
    @Overrride 
    public void onClick(View v){ 
     try{ 
      buttonNotify.onButtonClick(position); 
     }catch (Throwable e){ 
      //interface can be null 
     } 
    } 
}); 

而你並不需要在rowViewlistView.setOnItemClickListener做同樣有onClick

+0

感謝您的意見。 – theSereneRebel

相關問題