我正在尝试在这些类之间传递两个变量“getName”和“getImage”的值。
public class FruitsFragment extends Fragment {
public FruitsFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.word_list, container, false);
// Create a test list of vegs
final ArrayList<Veg> vegs = new ArrayList<Veg>();
vegs.add(new Veg("FELFEL", R.drawable.splash));
vegs.add(new Veg("FELFEL", R.drawable.splash));
vegs.add(new Veg("FELFEL", R.drawable.splash));
vegs.add(new Veg("FELFEL", R.drawable.splash));
vegs.add(new Veg("FELFEL", R.drawable.splash));
vegs.add(new Veg("FELFEL", R.drawable.splash));
vegs.add(new Veg("FELFEL", R.drawable.splash));
vegs.add(new Veg("FELFEL", R.drawable.splash));
// Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
// adapter knows how to create list items for each item in the list.
VegAdapter adapter = new VegAdapter(getActivity(), vegs);
// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// word_list.xml layout file.
ListView listView = (ListView) rootView.findViewById(R.id.list);
// Make the {@link ListView} use the {@link VegAdapter} created above, so that the
// {@link ListView} will display list items for each {@link Word} in the list.
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Get the {@link Word} object at the given position the user clicked on
Veg veg = vegs.get(position);
FelfelActivity fel = new FelfelActivity();
fel.setImage(veg.getImage());
fel.setDes(veg.getName());
Intent intent = new Intent(FruitsFragment.this.getActivity(), FelfelActivity.class);
FruitsFragment.this.startActivity(intent);
}
});
return rootView;
}
}
和
public class FelfelActivity extends AppCompatActivity {
int ID;
String NAME;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Change to numbers
setContentView(R.layout.felfel);
ImageView imageView = (ImageView) findViewById(R.id.imageF);
imageView.setImageResource(ID);
imageView.setVisibility(View.VISIBLE);
TextView textView = (TextView) findViewById(R.id.des);
textView.setText(NAME);
}
public void setImage(int id){
ID = id;
}
public void setDes(String name){
NAME = name;
}
}
它似乎不起作用,因为当我在手机上运行应用程序并点击列表中的任何元素时,它会指示我进行正确的活动,但没有图像或名称,只有空白背景。
我想知道是否有一种方法可以传递“getImage”的值并将其设置为名为“imageF”的ImageView,并将“getName”的值设置为名为“des”的TextView。
答案 0 :(得分:0)
将详细信息传递给您用于启动活动的意图中的其他活动:
Intent intent = new Intent(FruitsFragment.this.getActivity(), FelfelActivity.class);
intent.putExtra("name", name);
intent.putExtra("image", image);
FruitsFragment.this.startActivity(intent);
访问下一个活动的意图
String name = getIntent().getStringExtra("name");
String image = getIntent().getStringExtra("image");
Intent的docs有更多信息(请参阅标题为&#34; Extras&#34;)。