Android如何使用CardView OnClick方法拨打电话号码

时间:2016-05-15 20:13:21

标签: android android-recyclerview

我试图通过点击不同的CardView从我的应用程序拨打号码,但它不适合我。

每当我尝试添加意图时,startActivity都会变为红色并弹出错误消息“无法解析方法”。 我甚至尝试在startActivity()之前添加上下文但它崩溃了应用程序。

我要点击4个CardView才能发起呼叫 我有3个Java类:CardViewDataAdapter,其中声明了recyclerView Holder,MainActivity和itemObject 在activity_main中,我使用了recycleler_view。在cardview_row中,我使用了card_view并将其与回收站视图绑定。

请帮助我,我已经搜索了这个问题并且还查看了堆栈溢出但尚未找到解决方案。

The error message image

CardViewDataAdapter.java代码:

public class CardViewDataAdapter extends RecyclerView.Adapter<CardViewDataAdapter.ViewHolder> {

    private List<ItemObject> itemList;

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        // each data item is a string and an image in this case
        public ImageView image_view;
        public TextView image_name;
        private Context context;

        public ViewHolder(View itemLayoutView) {

            super(itemLayoutView);
            this.image_view = (ImageView) itemLayoutView.findViewById(R.id.image_view);
            this.image_name = (TextView) itemLayoutView.findViewById(R.id.image_name);

            // Store the context
            this.context = context;
            // Attach a click listener to the entire row view
            itemLayoutView.setOnClickListener(this);
        }

        // Handles the row being being clicked
        @Override
        public void onClick(View view) {

            if (getPosition() == 0){
                Intent callIntent = new Intent(Intent.ACTION_DIAL);
                callIntent.setData(Uri.parse("tel:123456789"));
                startActivity(callIntent);
            } else if (getPosition() == 1){
                Toast.makeText(view.getContext(), "position2 = " + getPosition(), Toast.LENGTH_SHORT).show();
            } else if (getPosition() == 2){
                Toast.makeText(view.getContext(), "position3 = " + getPosition(), Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(view.getContext(), "position4 = " + getPosition(), Toast.LENGTH_SHORT).show();
            }
            //context.startActivity(intent);
        }
    }

    // Create new views (invoked by the layout manager)
    @Override
    public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                             int viewType) {
        // create a new view
        View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.cardview_row, null);


        // create ViewHolder
        ViewHolder viewHolder = new ViewHolder(itemLayoutView);
        return viewHolder;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {

        // - get data from your itemsData at this position
        // - replace the contents of the view with that itemsData
        viewHolder.image_view.setImageResource(itemList.get(position).getPhoto());
        viewHolder.image_name.setText(itemList.get(position).getName());


    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return this.itemList.size();
    }   

    public CardViewDataAdapter(Context context, List<ItemObject> itemList) {
        this.itemList = itemList;
    }     
}

MainActivity.java代码:

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);

        // use a GridLayout manager
        mLayoutManager = new GridLayoutManager(this, 2);
        mRecyclerView.setLayoutManager(mLayoutManager);

        // specify an adapter
        List<ItemObject> rowListItem = getAllItemList();
        mAdapter = new CardViewDataAdapter(MainActivity.this, rowListItem);
        mRecyclerView.setAdapter(mAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            Toast.makeText(getApplicationContext(), "Settings Clicked",
                    Toast.LENGTH_SHORT).show();
            return true;
       /* } else if (id == R.id.action_search) {
            Toast.makeText(getApplicationContext(), "Search Clicked",
                    Toast.LENGTH_SHORT).show();
            return true;*/
        }
        return super.onOptionsItemSelected(item);
    }

    //Handles the references to items displayed on each cards
    private List<ItemObject> getAllItemList() {

        List<ItemObject> allItems = new ArrayList<ItemObject>();
        allItems.add(new ItemObject("Fire", R.drawable.fire));
        allItems.add(new ItemObject("Ambulance", R.drawable.ambulance));
        allItems.add(new ItemObject("Police", R.drawable.police));
        allItems.add(new ItemObject("AntiSquad", R.drawable.police));
        return allItems;
    }
}

itemsObject.java代码:

   public class ItemObject {

    private String name;
    private int photo;

    public ItemObject(String name, int photo) {
        this.name = name;
        this.photo = photo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPhoto() {
        return photo;
    }

    public void setPhoto(int photo) {
        this.photo = photo;
    }
}

cardview_row.xml代码:

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


    <!-- CardView with customized attributes -->
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="170dp"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        cardview:cardCornerRadius="20dp"
        cardview:cardElevation="90dp"
        android:elevation="2dp"
        android:background="@drawable/myrect"
      >

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

            <ImageView
                android:id="@+id/image_view"
                android:layout_width="fill_parent"
                android:layout_height="120dp"
               />

            <TextView
                android:id="@+id/image_name"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:background="#4757b3"
                android:textColor="@android:color/white"
                android:fontFamily="sans-serif-condensed"
                android:textSize="20sp"
                android:textAlignment="center" />
        </LinearLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

您的问题似乎有Context

要获取上下文,您需要将代码更改为以下内容:

Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:123456789"));
view.getContext().startActivity(callIntent);

答案 1 :(得分:0)

试试这个:

btnTelephone.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    //Call 1st phone number api
                                    Uri callIntentUri = Uri.parse("tel:"+telephoneStringForCall);
                                    Intent callIntent = new Intent(Intent.ACTION_DIAL, callIntentUri);
                                    startActivity(callIntent);
                                }
                            });

并检查你的观点初始化,你丢失了什么