我正在尝试向城市指南应用程序添加一个电话按钮,其中每个项目中都应该有一个电话图标,单击该图标会将我重定向到已经写下电话号码的拨号器应用程序。
这是主要活动代码
public class Resto extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_resto);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ListView lv;
lv=(ListView) findViewById(R.id.listview);
String[] names= {"L'olivo verde","Lounge36","Punto Alto","Sa7se7", "Maison Jabbour", "Pizza Napoli", "Capri","Classic burger joint","kaakunada","Ichiban Express","Ikura Sushi","Machos" };
int[] imageids = {R.drawable.lolivo,R.drawable.lounge,R.drawable.punto,R.drawable.sahseh, R.drawable.jabbour, R.drawable.napoli, R.drawable.capri,R.drawable.cbj, R.drawable.kaak, R.drawable.ichiban, R.drawable.ikura,R.drawable.machos, R.drawable.gate};
lv.setAdapter(new CustomAdapter(this,names,imageids));
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
这是自定义适配器代码
public class CustomAdapter extends BaseAdapter{
String[] result;
Context context;
int[] image ID;
LayoutInflater inflater=null;
public CustomAdapter(Context c, String[] names, int[] imageids)
{
result = names;
context= c;
imageID = imageids;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return result.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
public class Holder {
TextView tv;
ImageView img;
}
public View getView(int i, View view, ViewGroup viewGroup) {
// i is the position. starts at 0 and keeps on incremeting till the end of the data
Holder holder= new Holder();
final View rowView;
rowView = inflater.inflate(R.layout.restos,null);
holder.tv = (TextView) rowView.findViewById(R.id.textView);
holder.img= (ImageView) rowView.findViewById(R.id.imageView);
holder.img = (ImageView) view.findViewById(R.id.imageclick);
holder.tv.setText(result[i]);
holder.img.setImageResource(imageID[i]);
rowView.setTag(i);
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "position " + rowView.getTag(), Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
首先,您必须创建一个自定义列表视图,如@Sahdeep Singh在注释中提到的那样。然后,您必须将此代码放在item onclick方法中:
listview.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?>adapter,View v, int position){
ItemClicked item = adapter.getItemAtPosition(position);
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:0123456789"));
startActivity(intent);
}
});
有关详细信息,请查看此答案。希望这有帮助。