我正在尝试向城市指南应用添加电话按钮,其中每个项目中应该有一个电话图标,点击后会将我重定向到拨号应用,并且电话号码已经写好。
这是主要的活动代码
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();
}
});
}
}
这是customAdapter代码
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;
}
}
答案 0 :(得分:0)
首先,你必须在评论中创建@Sahdeep Singh所提到的自定义列表视图。然后,您必须将此代码放在项目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);
}
});
有关详情,请查看this个答案。希望这会有所帮助。