我正在开发我的第一个应用程序,但我遇到了一个我无法克服的问题。 我正在为餐馆创建一个ListView,其中(名称,电话,徽标和位置)将在其列表项中显示。
除协调外,一切进展顺利。 我只能用getview方法对坐标进行硬编码,但我不知道如何将其与arraylist一起更改。
有没有办法将它作为另一个议论添加到arraylist?
任何帮助将不胜感激:)
这些是我的代码,我在其中添加了一个字符串作为位置,Intent打开固定坐标。
用于新对象类:
public class Cell {
private String mName;
private String mPhone;
private int mLogo;
private String mLocation;
public Cell (String name, String phone, int logo, String location){
mName = name;
mPhone = phone;
mLogo = logo;
mLocation = location;}
public String getmName() {return mName;}
public String getmPhone(){return mPhone;}
public int getmLogo(){return mLogo;}
public String getmLocation() {return mLocation;}
这适用于customAdapter:
public class CellAdapter extends ArrayAdapter<Cell> {
private static final String LOG_TAG = CellAdapter.class.getSimpleName();
public CellAdapter(Activity context, ArrayList<Cell> restaurant) {
super(context, 0, restaurant);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
final Cell currentNumber = getItem(position);
TextView mapView = (TextView) listItemView.findViewById(R.id.map_view);
mapView.setText(currentNumber.getmLocation());
mapView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
mapIntent.setData(Uri.parse("geo:47.6, -122.3"));
getContext().startActivity(mapIntent);
}
});
TextView nameView = (TextView) listItemView.findViewById(R.id.name_view);
nameView.setText(currentNumber.getmName());
TextView phoneView = (TextView) listItemView.findViewById(R.id.phone_view);
phoneView.setText(currentNumber.getmPhone());
phoneView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent call = new Intent(Intent.ACTION_DIAL);
call.setData(Uri.parse("tel: " + currentNumber.getmPhone()));
getContext().startActivity(call);
}
});
ImageView logoView = (ImageView) listItemView.findViewById(R.id.logo_view);
logoView.setImageResource(currentNumber.getmLogo());
return listItemView;
}
这是活动代码:
public class RestaurantsActivity extends AppCompatActivity {Intent mapIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurants);
final ArrayList<Cell> restaurant = new ArrayList<>();{
restaurant.add(new Cell("Restaurant 1", "12345", R.drawable.logo_1, "MAP"));
restaurant.add(new Cell("Restaurant 2", "67890", R.drawable.logo_2, "MAP"));
CellAdapter restaurantAdapter = new CellAdapter(this, restaurant);
ListView restaurantView = (ListView) findViewById(R.id.restaurant_view);
restaurantView.setAdapter(restaurantAdapter);
}
}
}
我需要的最终结果是当用户点击(MAP)时更新mapIntent中与不同餐馆的数据:
答案 0 :(得分:0)
如果我正确地回答了您的问题,以下步骤可以帮助您解决此问题:
mapIntent.setData(currentNumber.getLocationURI());