2014-02-19 17 views
0

我從我的數據庫中拉出一些數據,並讓它正確地填充TextView。我遇到的問題是當我點擊列表中的按鈕時,按鈕什麼也不做。我在onClick中添加了代碼完成,但該按鈕沒有響應。我查看了其他堆棧文章並嘗試瞭解決方案,但它們不起作用。我相信這是明顯的我缺少的東西。這裏是我的代碼listAdapter中的按鈕不工作

<?xml version="1.0" encoding="utf-8"?> 

<TableRow 
    android:id="@+id/visitorNotLeft" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/names" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="" /> 

    <TextView 
     android:id="@+id/companies" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="" /> 

    <Button 
     android:id="@+id/timeOut" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:descendantFocusability="blocksDescendants" 
     android:layout_weight="1" 
     android:text="@string/timeOut" /> 

</TableRow> 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    // TODO Auto-generated method stub 
    super.onListItemClick(l, v, position, id); 

    timeOut = (Button)v.findViewById(R.id.timeOut); 
    timeOut.setTag(position); 
    timeOut.setFocusable(false); 
    timeOut.setFocusableInTouchMode(false); 
    timeOut.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      finish(); 

     } 

    }); 
} 

這裏是我的適配器代碼

 @Override 
    protected void onPostExecute(String result) { 
     // TODO Auto-generated method stub 

     ListAdapter adapter = new SimpleAdapter(GetDataFromDB.this, 
       productsList, R.layout.list_of_visitors_not_signed_out, 
       new String[] { TAG_NAME, TAG_COMPANY }, new int[] { 
         R.id.names, R.id.companies }); 
     // updating listview 
     setListAdapter(adapter); 

    } 

如果你要我添加更多的代碼讓我知道。

+1

您需要添加事件監聽到getview方法在適配器 –

+0

你可以發佈您的適配器代碼? –

+0

我剛加了我的適配器代碼 – Aaron

回答

1

試試這個代碼,並餵我回任何沒有明顯的事情你

ListAdapter adapter = new SimpleAdapter(GetDataFromDB.this, productsList, R.layout.list_of_visitors_not_signed_out, 
    new String[] { TAG_NAME, TAG_COMPANY }, new int[] { 
      R.id.names, R.id.companies }){ 
      public View getView(int position, View convertView, android.view.ViewGroup parent) { 
    View v = super.getView(position, convertView, parent); 
    Button buttonObject= (Button)v.findViewById(R.id.timeOut); 
    buttonObject.setOnClickListener(new View.OnClickListener(){ 

      public void onClick(View v) { 

      // Write your code here 
     } 
    }); 
    return v; 
    } 
    }; 

// updating listview 
setListAdapter(adapter);' 
+0

謝謝,它工作。我剛剛將R.id.R.id.timeOut更改爲R.id.timeOut。 – Aaron

+0

歡迎您,我對此錯別字表示歉意:) –

0

您應該在getView重寫函數中創建onClickListener。見下面的例子:

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View rowView = inflater.inflate(R.layout.item, parent, false); 

    TextView text = (TextView) rowView.findViewById(R.id.text); 
    Button myButton = (Button) rowView.findViewById(R.id.mybutton); 

    myButton.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      // Do something    
     } 
    }); 


    return rowView; 
}