2013-04-03 45 views
0

我在我的應用程序中有一個顯示數據庫內容的Listview。這是一個自定義的可點擊列表視圖,我想爲listview的每個元素添加一個按鈕,所以當你點擊這個按鈕時,它會打一個電話並撥打一個特定的號碼。爲ListView中的按鈕設置監聽器(沒有getView方法?)

我已經閱讀了一些類似的主題,但都指向適配器中的「getView」方法,但我不知道它指的是什麼。這裏是我的相關代碼有關我的列表視圖:

private void displayListView() { 

    // getExtra 
    Bundle bundle = getIntent().getExtras(); 
    String title = bundle.getString("title", "Choose here :"); 
    String inInterval = bundle.getString("inInterval"); 
    Log.d(TAG, "inInterval = " + inInterval); 

    poititle.setText(title); 


    // put the results of the method in a cursor 
    Cursor c = dbHelper.findPoiInTable(inInterval); 

    String[] columns = new String[] { DatabaseAdapter.COL_NAME, 
      DatabaseAdapter.COL_STREET, DatabaseAdapter.COL_WEBSITE, 
      DatabaseAdapter.COL_TELEPHONE, DatabaseAdapter.COL_REMARKS, 
      DatabaseAdapter.COL_PRICE }; 

    int[] to = new int[] { R.id.name, R.id.street, R.id.website, 
      R.id.telephone, R.id.remarks, R.id.price }; 

    cursorAdapter = new SimpleCursorAdapter(this, R.layout.poi_info, c, 
      columns, to, 0); 

    ListView listView = (ListView) findViewById(R.id.poilistview); 
    // Assign adapter to ListView 
    listView.setAdapter(cursorAdapter); 

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     // Comportement des éléments de la listview 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

      Intent i = new Intent(getApplicationContext(), 
        POIActivity.class); 

      String name = ((TextView) view.findViewById(R.id.name)) 
        .getText().toString(); 
      String website = ((TextView) view.findViewById(R.id.website)) 
        .getText().toString(); 
      String telephone = ((TextView) view 
        .findViewById(R.id.telephone)).getText().toString(); 
      String remarks = ((TextView) view.findViewById(R.id.remarks)) 
        .getText().toString(); 
      String price = ((TextView) view.findViewById(R.id.price)) 
        .getText().toString(); 
      // i.putExtra(ID_EXTRA, name) ; 
      i.putExtra(ID_NAME, name); 
      i.putExtra(ID_WEBSITE, website); 
      i.putExtra(ID_TELEPHONE, telephone); 
      i.putExtra(ID_REMARKS, remarks); 
      i.putExtra(ID_PRICE, price); 
      startActivity(i); 


     } 

    }); } 

,這裏是在XML文件中我的按鈕定義:

<Button 
    android:id="@+id/callButton" 
    android:layout_width="wrap_content" 
    android:layout_height="40dp" 
    android:layout_centerVertical="true" 
    android:layout_marginRight="10dp" 
    android:layout_toLeftOf="@+id/arrowImage" 
    android:text=" Call " 
    android:textSize="15sp" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:descendantFocusability="blocksDescendants" /> 

我當然嘗試設置監聽器的常用方式(.setOnClickListener等),但我想我的displayListView()方法必須真正做到。
在此先感謝!

回答

1

無法使用getView方法設置按鈕的點擊是不可能的。 您需要了解如何使用適配器。 每個適配器都有一個名爲getView的方法,該方法獲取位置作爲參數,並且適配器返回需要位於此位置的視圖。 您需要覆蓋SimpleCursorAdapter及其getView方法,並且在創建視圖(R.layout.poi_info)時,您需要將單擊監聽器分配給該按鈕。 你現在做的是當你點擊一個列表中的項目(而不是按鈕),你的監聽器被觸發。 無論如何,這是一個有很多討論的主題,Google中的快速搜索可以爲您提供答案。 下面是一個教程,我發現: http://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/

好運

編輯 - 示例:

請注意,我用newView在這裏。在你將閱讀更多關於適配器的信息後,閱讀關於CursorAdapter的內容,你將會明白它是什麼。這個適配器有一點不同的行爲。

public class ExtendedSimpleCursorAdapter extends SimpleCursorAdapter{ 

     public ExtendedSimpleCursorAdapter(Context context, int layout, 
       Cursor c, String[] from, int[] to, int flags) { 
      super(context, layout, c, from, to, flags); 

     } 

     public View newView (Context context, Cursor cursor, ViewGroup parent){ 
      View v = super.newView(context, cursor, parent); 
      v.findViewById(R.id.your_button_id).setOnClickListener(yourClickListener); 
     } 
    } 
+0

感謝這些解釋,我將研究更多關於適配器然後。只是一個問題:到目前爲止,我的代碼工作得很好,我可以用我的列表完成所有工作(除了按鈕偵聽器),每個元素都是可點擊的並且行爲正確。是否正在改變你建議的所有改寫,添加新的類?我已經使用了SimpleCursorAdaptater,是否可以將getView方法直接集成到我的代碼中,或者整個邏輯是重新思考?謝謝=) – Phalanx

+1

對不起,我沒有看到它是一個CursorAdapter,我認爲它是一個ArrayAdapter,但差異並不大。對於你的問題,不,新的適配器不會改變你的任何邏輯。將適配器想象爲UI和要呈現的數據之間的連接(在您的情況下爲數據庫中的數據)。適配器將獲取數據並創建列表中的項目。 Android提供基本的適配器,您需要做的是擴展主題以供您使用。我在答案中添加了一個可以在適配器中進行更改的示例。 – Elad92

+0

非常感謝,它有幫助。我會盡力使它現在工作! – Phalanx