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

时间:2018-07-07 14:54:03

标签: android listview

我对开发非常陌生,我正在一个学校项目上工作,我想在listView上将呼叫操作意图设置为不同的项目。 我想传递意图的每个项目都设置了onClickListener,但是我不知道如何让他们呼叫每个特定的电话号码。

这是我的代码  `公共类WordAdapter扩展了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);
        }

    }
}

`

这个想法是,用户可以点击并致电其他公司。 谢谢

3 个答案:

答案 0 :(得分:0)

manifest.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);

答案 1 :(得分:0)

您最好将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);
    }
});

答案 2 :(得分:0)

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

所以在list_item上是这些更改:

  1. android:autoLink =“ phone”

    select_related()
  2. android:autoLink =“ web”

     <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>
    

这对我有用。