gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:20:17 25 4
gpt4 key购买 nike

我正在尝试通过单击不同的 CardView 从我的应用程序调用号码,但它对我不起作用。

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

我有 4 个 CardViews 可以点击以发起调用和我有 3 个 Java 类:声明 recyclerView Holder 的 CardViewDataAdapter、MainActivity 和 itemObject在 activity_main 中我使用了 recycler_view。在 cardview_row 中,我使用了 card_view 并将其与回收站 View 绑定(bind)。

请帮助我,我用谷歌搜索了这个问题,也在堆栈溢出上查找了它,但还没有找到解决方案。

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>

最佳答案

看起来你有一个上下文问题。

要获取上下文,您需要将代码更改为如下所示:

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

关于Android 如何使用 CardView OnClick 方法调用电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37243184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com