如何在不同的列表视图项上设置调用意图?



我对开发很陌生,我正在做一个学校项目,我想将调用操作意图设置为列表视图上的不同项目。 我在我想传递意图的每个项目上设置了 onClickListener,但我不知道如何让他们拨打每个特定的电话号码。

这是我的代码 ' public class WordAdapter extensions ArrayAdapter 实现 View.OnClickListener {

/**
* Create a new {@link WordAdapter} object.
*
* @param context is the current context (i.e. Activity) that the adapter is being created in.
* @param words is the list of {@link Word}s to be displayed.
*/
public WordAdapter(Context context, ArrayList<Word> words) {
super(context, 0, words);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
// Get the {@link Word} object located at this position in the list
Word currentWord = getItem(position);
// Find the TextView in the list_item.xml layout with the ID 
TextView titleTextView = (TextView) listItemView.findViewById(R.id.title_name);
titleTextView.setText(currentWord.getTitleId());
// Find the TextView in the list_item.xml layout with the ID 
TextView priceTextView = (TextView) listItemView.findViewById(R.id.price_name);
// Get the default translation from the currentWord object and set this text on
// the default TextView.
priceTextView.setText(currentWord.getPriceId());
// Find the TextView in the list_item.xml layout with the ID 
TextView hoursTextView = (TextView) listItemView.findViewById(R.id.hours_name);
hoursTextView.setText(currentWord.getHoursClearwaterId());
// Find the TextView in the list_item.xml layout with the ID 
TextView phoneTextView = (TextView) listItemView.findViewById(R.id.phone_name);
phoneTextView.setText(currentWord.getPhoneClearwaterId());
//Set the OnlcickListener for this view
phoneTextView.setOnClickListener(this);
// Find the TextView in the list_item.xml layout with the ID 
TextView webTextView = (TextView) listItemView.findViewById(R.id.web_name);
webTextView.setText(currentWord.getWebClearwaterId());
phoneTextView.setOnClickListener(this);
// Find the ImageView in the list_item.xml layout with the ID image.
ImageView imageView = (ImageView) listItemView.findViewById(R.id.list_item_icon);
// Check if an image is provided for this word or not
if (currentWord.hasImage()) {
// If an image is available, display the provided image based on the resource ID
imageView.setImageResource(currentWord.getImageResourceId());
// Make sure the view is visible
imageView.setVisibility(View.VISIBLE);
} else {
// Otherwise hide the ImageView (set visibility to GONE)
imageView.setVisibility(View.GONE);
}
// Return the whole list item layout (containing 2 TextViews) so that it can be shown in
// the ListView.
return listItemView;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.phone_name:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse(**Here the method will get the many different phone numbers from my list data**);
startActivity(callIntent);
}
}
}

'

这个想法是用户可以点击并调用不同的企业。 谢谢

清单中的权限.xml

<uses-permission android:name="android.permission.CALL_PHONE" />

然后点击

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your phone"));
startActivity(intent);

在您的情况下

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + String.valueOf(word.get(i))));
startActivity(intent);

最好使用匿名类设置onClick侦听器,以便它可以捕获当前Word并使用它来生成正确的意图

final Word currentWord = getItem(position);
phoneTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+**get from `currentWord`**);
startActivity(callIntent);
}
});

好的,所以在转动项目后,我的审阅者向我展示了如何使用自动链接,然后我将其添加到布局上的文本视图中。

所以在list_item这些是变化:

  1. android:autoLink="phone">

    <TextView
    android:id="@+id/txtViewPhone"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Phone no: 12345"
    android:autoLink="phone"
    android:textSize="16sp"
    android:layout_margin="5dip">
    </TextView>
    
  2. android:autoLink="web">

    <TextView
    android:id="@+id/txtViewWeb"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Web: www.pareshnmayani.wordpress.com"
    android:autoLink="web"
    android:textSize="16sp"
    android:layout_margin="5dip">
    </TextView>
    

这对我有用。

最新更新