2017-02-18 124 views
0

請你能幫我解決以下問題。我試圖開發一個聯繫人目錄作爲我的應用的一部分,它允許用戶點擊每個聯繫人旁邊的呼叫和電子郵件按鈕並執行相關操作。但目錄顯示正常:ListView不顯示按鈕

1)當我運行應用程序時,phonebook_row.xml類中的調用和電子郵件按鈕未顯示。我該如何解決這個問題?

2)我怎樣才能打出電話和電子郵件點擊按鈕,只需從電話號碼和電子郵件(不包括「姓名」和「電子郵件」)旁邊存儲的字符串中打開號碼並打開電話撥號程序中的號碼/創建新的電子郵件地址? 我的代碼如下...

Directory.java:

import android.graphics.Bitmap; 

public class Directory { 

private Bitmap mAvatar; 
private String mName; 
private String mPhone; 
private String mEmail; 
private String mLocation; 

public Directory(Bitmap mAvatar, String mName, String mPhone, String mEmail, String mLocation) { 
    this.mAvatar = mAvatar; 
    this.mName = "Name: " + mName; 
    this.mPhone = "Phone Extension: " + mPhone; 
    this.mEmail = "Email: " + mEmail; 
    this.mLocation = "Location: " + mLocation; 
} 

public Bitmap getmAvatar() { 
    return mAvatar; 
} 

public void setmAvatar(Bitmap mAvatar) { 
    this.mAvatar = mAvatar; 
} 

public String getmName() { 
    return mName; 
} 

public void setmName(String mName) { 
    this.mName = mName; 
} 

public String getmPhone() { 
    return mPhone; 
} 

public void setmPhone(String mPhone) { 
    this.mPhone = mPhone; 
} 

public String getmEmail() { 
    return mEmail; 
} 

public void setmEmail(String mEmail) { 
    this.mEmail = mEmail; 
} 

public String getmLocation() { 
    return mLocation; 
} 

public void setmLocation(String mLocation) { 
    this.mLocation = mLocation; 
} 


} 

DirectoryAdapter.java:

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

import java.util.List; 

public class DirectoryAdapter extends BaseAdapter{ 

private Context mContext; 
private List<Directory> mListDirectory; 

public DirectoryAdapter(Context context, List<Directory> list) { 
    mContext = context; 
    mListDirectory = list; 
} 
@Override 
public int getCount() { 
    return mListDirectory.size(); 
} 

@Override 
public Object getItem(int i) { 
    return mListDirectory.get(i); 
} 

@Override 
public long getItemId(int i) { 
    return i; 
} 

@Override 
public View getView(int i, View view, ViewGroup viewGroup) { 
    Directory entry = mListDirectory.get(i); 

    if(view == null) { 
     LayoutInflater inflater = LayoutInflater.from(mContext); 
     view = inflater.inflate(R.layout.phonebook_row, null); 
    } 

    ImageView ivAvatar = (ImageView)view.findViewById(R.id.imgAvatar); 
    ivAvatar.setImageBitmap(entry.getmAvatar()); 

    TextView tvName = (TextView)view.findViewById(R.id.tvName); 
    tvName.setText(entry.getmName()); 

    TextView tvPhone = (TextView)view.findViewById(R.id.tvPhone); 
    tvPhone.setText(entry.getmPhone()); 

    TextView tvEmail = (TextView)view.findViewById(R.id.tvEmail); 
    tvEmail.setText(entry.getmEmail()); 

    TextView tvLocation = (TextView)view.findViewById(R.id.tvLocation); 
    tvLocation.setText(entry.getmLocation()); 
    return view; 
} 
} 

ListDirectory.java:

import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.ListView; 

import java.util.ArrayList; 
import java.util.List; 

public class ListDirectory extends AppCompatActivity { 

private ListView lvDirectory; 
private ImageView buttonPhone; 
private ImageView buttonEmail; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_list_directory); 

    lvDirectory = (ListView)findViewById(R.id.listDirectory); 
    buttonPhone = (ImageView) findViewById(R.id.ivPhone); 
    buttonEmail = (ImageView) findViewById(R.id.ivEmail); 

    List<Directory> listDirectory = new ArrayList<Directory>(); 
    listDirectory.add(new Directory(BitmapFactory.decodeResource(getResources(), R.drawable.johnsmith), "Mr John Smith", "1234", "[email protected]", "UK")); 
    listDirectory.add(new Directory(BitmapFactory.decodeResource(getResources(), R.drawable.jack), "Mr Jack Smith", "3242", "[email protected]", "USA")); 
    DirectoryAdapter adapter = new DirectoryAdapter(this, listDirectory); 
    lvDirectory.setAdapter(adapter); 
} 
} 

activity_list_directory.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_list_contacts" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
tools:context="com.example.XXXXX.computerscienceinduction.ListDirectory"> 

<ListView 
    android:id="@+id/listDirectory" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
</ListView> 

</LinearLayout> 

phonebook_row.xml:

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

<ImageView android:id="@+id/imgAvatar" 
    android:layout_width="70dip" 
    android:layout_height="70dip" 
    android:scaleType="fitCenter" 
    android:src="@mipmap/ic_launcher"/> 

<LinearLayout android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <LinearLayout android:orientation="horizontal" 
     android:layout_width="243dp" 
     android:layout_height="wrap_content"> 
     <TextView android:id="@+id/tvName" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textStyle="bold" 
      android:layout_centerHorizontal="true"/> 

    </LinearLayout> 

    <LinearLayout android:orientation="horizontal" 
     android:layout_width="243dp" 
     android:layout_height="wrap_content"> 
     <TextView android:id="@+id/tvPhone" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true"/> 
     <ImageView android:id="@+id/ivPhone" 
      android:layout_width="70dip" 
      android:layout_height="70dip" 
      android:scaleType="fitCenter" 
      android:src="@drawable/call_icon"/> 

    </LinearLayout> 


    <LinearLayout android:orientation="horizontal" 
     android:layout_width="244dp" 
     android:layout_height="wrap_content"> 
     <TextView android:id="@+id/tvEmail" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true"/> 
     <ImageView android:id="@+id/ivEmail" 
      android:layout_width="70dip" 
      android:layout_height="70dip" 
      android:scaleType="fitCenter" 
      android:src="@drawable/email_icon"/> 

    </LinearLayout> 


    <LinearLayout android:orientation="horizontal" 
     android:layout_width="243dp" 
     android:layout_height="wrap_content"> 
     <TextView android:id="@+id/tvLocation" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true"/> 

    </LinearLayout> 


</LinearLayout> 

</LinearLayout> 
+0

要顯示ImageViews,請嘗試將TextView(tvPhone,tvEmail)的layout_width替換爲「wrap_content」。使用substring()方法只獲得一部分字符串,例如。 entry.getmPhone()。substring(17)獲取沒有「電話分機:」的電話號碼。希望它有幫助! –

回答

1

在您的適配器類,你可以做以下的get視圖的方法,注意我的例子,其只對主叫方使用ImageView的。一個按鈕將具有相同的實現。

ImageView icon = (ImageView)view.findViewById(R.id.callicon); 
icon.setImageResource(R.drawable.ic_action_call); 
    icon.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      //----create new intent for the call action and give it the number 
      //this will just start the dailer but not complete the action 
      Intent callIntent = new Intent(Intent.ACTION_DIAL, 
        Uri.parse("tel:" + entry.getmPhone())); 
      callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      context.startActivity(callIntent); 
     } 
    });