2014-02-05 107 views
2

我有一個自定義列表,其中每行都有一個圖像視圖和一個文本視圖以及兩個按鈕。如何知道自定義列表中單擊哪個按鈕

|```````````| text box 
|Image View | 
|___________| Button1 Button2 

它是一排列表的佈局。 我定製AdapterClass是..

public class sublist extends ArrayAdapter<String> { 

    private final Activity context; 
    private final Vector<String> shortDisJornals; 
    private final Vector<String> imageId1; 

    public sublist(Activity context, Vector<String> s1, 
      Vector<String> imageId1) { 
     super(context, R.layout.sublist, s1); 
     this.context = context; 
     this.shortDisJornals = s1; 
     this.imageId1 = imageId1; 
     Log.d("image vector", ""+imageId1); 

    } 

    @Override 
    public View getView(final int position, View view, ViewGroup parent) { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     View rowView = inflater.inflate(R.layout.sublist, null, true); 
     TextView txtTitle = (TextView) rowView 
       .findViewById(R.id.textViewjurnals); 
     ImageView imageView = (ImageView) rowView 
       .findViewById(R.id.imageViewJurnals); 
     Button buyJurnals=(Button)rowView.findViewById(R.id.buttonjurnalsBuy); 
     buyJurnals.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Toast.makeText(getContext(), "sdsd"+position, 3000).show(); 
      } 
     }); 
     Button detailsJurnalis=(Button)rowView.findViewById(R.id.buttonjrnalsDetails); 
     txtTitle.setText(shortDisJornals.elementAt(position)); 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
     .permitAll().build(); 

     StrictMode.setThreadPolicy(policy); 
     String image = imageId1.elementAt(position); 
     try { 
     Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
       image).getContent()); 
      imageView.setImageBitmap(null); 
       imageView.setImageBitmap(bitmap); 
       // setContentView(image); 
       } catch (MalformedURLException e) { 
       e.printStackTrace(); 
       } catch (IOException e) { 
       e.printStackTrace(); 
       }catch(Exception e){} 


     return rowView; 


    } 
} 
在我的主要活動

現在 -

lv = (ListView) findViewById(R.id.listView1); 
titleVector = new Vector<String>(); 
imageUrl = new Vector<String>(); 
lv.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { 
    Toast.makeText(getApplicationContext(), "sd", 3000).show(); 

    } 
}); 

但敬酒時,我按一下按鈕

+0

檢查此http://stackoverflow.com/a/12813906/1168654 –

+0

你可以告訴你是否希望單擊按鈕或lsitview行的偵聽器。它的混亂和不明確。 – Raghunandan

回答

0

要設置一個onClickListener對的一劑不顯示按鈕:buttonjurnalsBuy。點擊這個按鈕應該可以工作。只需爲另一個按鈕設置另一個監聽器來處理對該按鈕的點擊。

如果單擊列表中的整行,則會調用在setOnItemClickListener中設置的偵聽器。

如果您爲某些視圖設置單獨的點擊偵聽器並且設置了OnItemClickListener,則如果用戶單擊其中一個視圖,則會調用點擊偵聽器。如果用戶單擊行中的其他位置,則會調用OnItemClickListener。

+0

當您單擊列表行時,imageview無法獲得焦點。所以敬酒不顯示! – Raghunandan

+0

如果點擊不是由行中的某個視圖處理的,則應調用OnItemClickListener。 – Janusz

相關問題