我看到了这个问题的答案,但他们没有解决我的问题。我有一个Gridview
,其中包含我想要的图片和字符串,当我选择其中的一个时,必须在另一活动中返回该字符串
这是我的第一个活动:
public class TypeActivity extends AppCompatActivity {
public String selectedItem;
private GridView type_grid;
private String[] voiture_type ={"2
roues","Petit","Moyen","Grand","Haut","Très haut"
};
private int [] images={R.drawable.deuxroues,R.drawable.smallcar,R.drawable.car,R.drawable.grandvoiture,R.drawable.haut,R.drawable.car};
private Button confirm ;
public static DisplayMetrics metrics = new DisplayMetrics();
public static int width = metrics.widthPixels;
public static int height = metrics.heightPixels;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_type);
confirm = (Button) findViewById(R.id.confirm_type);
type_grid =(GridView)findViewById(R.id.gridview);
GridAdapter gridAdapter = new GridAdapter(this,voiture_type,images);
type_grid.setAdapter(gridAdapter);
type_grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
selectedItem = parent.getItemAtPosition(position).toString();
}
});
confirm.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(com.example.macbookpro.tunipark.activity.TypeActivity.this, Reservation.class);
i.putExtra("typeValue",selectedItem.trim());
startActivity(i);
}
});
}
}
这是我的GridAdapter
代码。我在第二个活动中调用了我的额外数据,但是此代码无法显示。可能有人有任何想法
public class GridAdapter extends BaseAdapter {
Context context;
private final String[] voiture_type;
private final int[] images;
View view;
LayoutInflater layoutInflater;
public GridAdapter(Context context, String[] voiture_type, int[] images) {
this.context = context;
this.voiture_type = voiture_type;
this.images = images;
}
@Override
public int getCount() {
return voiture_type.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null)
{
view = new View(context);
view = layoutInflater.inflate(R.layout.single_item,null);
view.setMinimumHeight(TypeActivity.height/6);
ImageView imageView = (ImageView)view.findViewById(R.id.img_item);
TextView textView = (TextView)view.findViewById(R.id.type_item);
imageView.setImageResource(images[position]);
textView.setText(voiture_type[position]);
}
return view;
}
}
答案 0 :(得分:0)
在您的 setOnItemClickListener 中获取GridView所选项目的值
type_grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
selectedItem = voiture_type[position];
}
});