2011-04-20 54 views

回答

4

你可以試試這個:

public class buttonWithList extends ListActivity { 
    /** Called when the activity is first created. */ 
    String[] items={"azhar","j"}; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     setListAdapter(new bsAdapter(this)); 


    } 
    public class bsAdapter extends BaseAdapter 
    { 
     Activity cntx; 
     public bsAdapter(Activity context) 
     { 
      // TODO Auto-generated constructor stub 
      this.cntx=context; 

     } 

     @Override 
     public int getCount() 
     { 
      // TODO Auto-generated method stub 
      return items.length; 
     } 

     @Override 
     public Object getItem(int position) 
     { 
      // TODO Auto-generated method stub 
      return items[position]; 
     } 

     @Override 
     public long getItemId(int position) 
     { 
      // TODO Auto-generated method stub 
      return items.length; 
     } 

     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) 
     { 
      View row=null; 
      // TODO Auto-generated method stub 
      /*if(convertView==null) 
      { 

      }*/ 
       LayoutInflater inflater=cntx.getLayoutInflater(); 
       row=inflater.inflate(R.layout.row, null); 
       TextView tv=(TextView)row.findViewById(R.id.txtRow); 
       Button btn=(Button)row.findViewById(R.id.btnRow); 
       tv.setText(items[position]); 
       btn.setOnClickListener(new OnClickListener(){ 

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         onListItemClick(this, this, position, null); 
        } 
        }); 


      return row; 
     } 

     protected void onListItemClick(OnClickListener onClickListener, 
       OnClickListener onClickListener2, int position, Object object) { 
      // TODO Auto-generated method stub 
      //Toast.makeText(this, items[position], 3000); 
      System.out.println(items[position]); 
     } 
    } 
} 
+1

可以給我一個演示項目wiyh xml編碼和某種操作onitemclicklistener和按鈕單擊 – Hitesh 2011-04-21 07:07:07

1

你需要實現一個ListAdapter。這個對象讓我們定義每一行的元素。另外,您將使用ViewBinder來指定行的操作。因此,您將爲每行添加一個按鈕,並使用ViewBinder設置onClickItemListener。