2013-01-03 39 views
1

我目前正在研究一個需要顯示聯繫人列表的應用程序。 你可以點擊他們的名字,它會帶你進入一個新的屏幕,顯示有關此聯繫人的信息,並允許你通過選擇他們的電話號碼給他們打電話。但是,在列表視圖行中選擇imageview android

我想在列表中名稱旁邊添加一個呼叫圖標(當前ImageView),並且當用戶按下呼叫圖標而不是名稱時,電話會立即呼叫該用戶而不是轉到該信息頁。

我已經添加了圖像的行,使人們點擊,但我完全不知道如何實現onClickListener它。我做了很多搜索,但大多數教程似乎只解釋如何將圖像添加到行中,而不是如何向其添加OnClickListener並讓它執行某些操作。

下面是分別列表和該行的XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <ListView android:id="@+id/android:list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 
    <TextView android:id="@+id/android:empty" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/no_notes"/> 
</LinearLayout> 

而這裏的一個用於行:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <TextView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/text1" 
     android:layout_width="260dp" 
     android:layout_height="60dp" 
     android:focusable="false" 
     android:textColor="#FFF" 
     android:textSize="26sp" /> 

    <ImageView 
     android:id="@+id/phoneButton" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignBottom="@+id/text1" 
     android:layout_toRightOf="@id/text1" 
     android:clickable="true" 
     android:contentDescription="TODO" 
     android:focusable="false" 
     android:src="@android:drawable/sym_action_call" /> 

</RelativeLayout> 

而這裏的onListItemClick方法:

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    Intent i = new Intent(this, PersonalPhonebookView.class); 
    i.putExtra(NotesDbAdapter.KEY_ROWID, id); 
    startActivityForResult(i, ACTIVITY_VIEW); 
} 

你們中的一些人可能會認識到Android記事本教程中的代碼片段。這是因爲我使用記事本代碼來創建自定義聯繫人列表。我不是一個好的程序員,所以我需要一些東西來開始。

這裏是屏幕的圖像

enter image description here

當用戶選擇的一個名稱是什麼我是,他去該用戶的信息頁面。我想要的是,當用戶選擇聯繫人右側的電話時,電話會自動撥打該用戶的電話號碼。我怎樣才能做到這一點?

+0

你有沒有實現自定義'BaseAdapter'來填充列表? –

+0

不,我還沒有。你的意思是這樣的:http://anujarosha.wordpress.com/2011/11/30/how-to-use-baseadapter-and-onitemclicklistener-in-android/ 事實上,該鏈接解決了我問題!在7小時內,我可以在這裏回答我自己的問題。感謝您的問題,我設法找到解決方案。謝謝! – user1945317

+0

更正,我還沒有找到解決方案。我鏈接的教程中的圖片不是互動的,所以我的問題仍然存在:( – user1945317

回答

0

寫imageView.setOnClickListener在填充時列表中的每一行適配器的getView()方法。

0

嘗試用這種getView方法來創建custom BaseAdapter

@Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 

     ViewHolder holder; 

     if (convertView == null) { 
      LayoutInflater viewInflater; 
      viewInflater = getLayoutInflater(); 
      convertView = viewInflater.inflate(R.layout.listview, null); 

      holder = new ViewHolder(); 
      holder.textview = (TextView) convertView.findViewById(R.id.TextView01); 
      holder.imageview = (ImageView) convertView.findViewById(R.id.ImageView01); 

      convertView.setTag(holder); 

     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     if (holder.textview != null && position < data_text.length) 
      holder.textview.setText(data_text[position]); 
     if (holder.imageview != null && position < data_image.length){ 
      holder.imageview.setImageResource(data_image[position]); 
      holder.imageview.setOnClickListener(new OnClickListener() {     
       @Override 
       public void onClick(View v) { 
        Toast.makeText(getApplicationContext(), "Image Clicked : "+position, Toast.LENGTH_LONG).show();      
       } 
      }); 
     } 
     return convertView; 
    } 

    static class ViewHolder { 
     TextView textview; 
     ImageView imageview; 
    } 

感謝。

0

您可以在上添加該組件的點擊getView方法本身 這裏是它的一個小例子。

....... 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    ................. 
    ................. 
    ................. 
    txt_assassinate.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Log.d("Adapter k ", " position " + position); 
      openpopup(); 
     } 
    }); 
    return convertView; 
} 


............. 
相關問題