2012-07-14 43 views
1

我想在我的listView項目上放一個按鈕並撥打電話......但到目前爲止,我無法弄清楚如何將該數據分配給該按鈕操作,這裏是我的代碼...列表項本身不可點擊,只有按鈕是。我使用一個適配器從數組中獲取數據Android List View Button撥打電話

public View getView(int position, View convertView, ViewGroup parent) 
{ 
    ViewHolder holder; 
    //Get the current location object 
    info lm = (info) getItem(position); 

    //Inflate the view 
    if(convertView==null) 
    { 
     convertView = dInflater.inflate(R.layout.dealbranch_layout, null); 
     holder = new ViewHolder(); 
     holder.name = (TextView) convertView.findViewById(R.id.address); 
     holder.call = (Button) convertView.findViewById(R.id.call); 
     convertView.setTag(holder); 
    } 
    else 
    { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    holder.name.setText(lm.getName()); 


    holder.call.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 

     Intent call = new Intent (Itent.ACTION_CALL); 

      /// No clue.. >,< /// /// lm.getPhone() should get the phone# for this row. 
      /// this action does not work ///// 

     startActivity(call); 
    } 
    }); 

    return convertView; 
} 

如果有人能指引我,那將會很棒。謝謝。

+0

傳遞您在該適配器的構造函數中的電話號碼並呼叫該號碼。 – 2012-07-14 03:32:51

+0

你能否詳述一下?非常感激=) – 2012-07-14 04:43:47

+1

請參閱@ azgolfer的答案,正是我會建議你。 – 2012-07-14 05:16:43

回答

3

我們不知道你是怎麼定義類 '信息' 所以這裏是我的猜測:

第一,更改行:

info lm = (info) getItem(position); 

final info lm = (info) getItem(position); 

然後在這裏是如何用電話號碼調用ACTION_DIAL:

holder.call.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View arg0) { 

    Intent call = new Intent (Intent.ACTION_DIAL, Uri.parse("tel:" + lm.getPhone())); 

     /// No clue.. >,< /// /// lm.getPhone() should get the phone# for this row. 
     /// this action does not work ///// 

    startActivity(call); 
} 

這是假定lm.getPhone()方法返回字符串中的電話號碼。

+0

感謝,它的工作原理...現在才startActivity是未定義的新onClickListener而無法啓動=( – 2012-07-14 05:14:37

+0

那麼,你可以嘗試context.startActivity(XXXX);其中上下文是你傳遞給你的ArrayAdapter活動 – azgolfer 2012-07-14 05:19:10

+0

無法正常工作。 =(System.out.println(lm.getPhone());我得到了正確的電話號碼,使用上面的代碼,我得到了NullPointerExeception;所以就是使用「tel:00000000」 – 2012-07-14 05:42:43