如何在下一个中显示文本和图像 activity
How can show text and image in next activity
我创建了 android 类似图库的应用,其中有一些使用 gridview 片段的图像。
功能正常,图像也显示在片段 gridview 和书籍中 activity 但文本未显示(文本未设置)
这是我的片段代码:
GridView grid;
String[] web = {
"Google",
"Github",
"Instagram",
"Facebook",
"Flickr",
"Pinterest",
"Quora",
"Twitter",
"Vimeo",
"WordPress",
"Youtube",
"Stumbleupon",
"SoundCloud",
"Reddit",
"Blogger"
} ;
int[] imageId = {
R.drawable.person7,
R.drawable.person1,
R.drawable.person2,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7
};
int[] fullimg = {
R.drawable.person7,
R.drawable.person1,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.fragment_status,container,false);
CustomGrid adapter = new CustomGrid(getActivity(), web, imageId, fullimg);
//CustomGrid adapter = new CustomGrid(MainActivity.this, web, imageId);
grid=(GridView) view.findViewById(R.id.grid);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
String value=(String)grid.getItemAtPosition(i);
Intent intent=new Intent(getActivity(),Book.class);
intent.putExtra("web", web[i]);
intent.putExtra("imageId", imageId[i]);
intent.putExtra("fullimg", fullimg[i]);
startActivity(intent);
}
});
//return inflater.inflate(R.layout.fragment_status, container, false);
return view;
}
这是我的书 activity 代码,其中要显示文本和图像
图片显示正常,但我不知道如何显示文字:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book);
TextView textView = (TextView) findViewById(R.id.txttitle);
ImageView imageView = (ImageView)findViewById(R.id.imageView2);
//textView.setText(txttitle);
//textView.setText(web[i]);
imageView.setImageResource(getIntent().getIntExtra("fullimg", R.drawable.person7));
}
//textView.setText(txttitle);
//textView.setText(web[i]);
imageView.setImageResource(getIntent().getIntExtra("fullimg", R.drawable.person7));
您不会像检索图像那样检索传递的文本 ;)
只需从 Intent 中获取您的文本。我正在考虑 web
包含您的文本,然后下面的代码将为您获取文本
textView.setText(getIntent().getStringExtra("web"));
设置textview只是被注释掉了。只需将 TextView 设置为类似于 imageview:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book);
TextView textView = (TextView) findViewById(R.id.txttitle);
ImageView imageView = (ImageView)findViewById(R.id.imageView2);
textView.setText(getIntent().getStringExtra("web"))
imageView.setImageResource(getIntent().getIntExtra("fullimg", R.drawable.person7));
}
不是通过 Intent 将变量传递给下一个 activity,而是使用应用程序 class 来存储数据并在应用程序中的任何地方使用。
在传递文本值时输入您要输入的常量
textView.setText(getIntent().getStringExtra("YourKeyValue"));
我创建了 android 类似图库的应用,其中有一些使用 gridview 片段的图像。
功能正常,图像也显示在片段 gridview 和书籍中 activity 但文本未显示(文本未设置)
这是我的片段代码:
GridView grid;
String[] web = {
"Google",
"Github",
"Instagram",
"Facebook",
"Flickr",
"Pinterest",
"Quora",
"Twitter",
"Vimeo",
"WordPress",
"Youtube",
"Stumbleupon",
"SoundCloud",
"Reddit",
"Blogger"
} ;
int[] imageId = {
R.drawable.person7,
R.drawable.person1,
R.drawable.person2,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7
};
int[] fullimg = {
R.drawable.person7,
R.drawable.person1,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7,
R.drawable.person7
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.fragment_status,container,false);
CustomGrid adapter = new CustomGrid(getActivity(), web, imageId, fullimg);
//CustomGrid adapter = new CustomGrid(MainActivity.this, web, imageId);
grid=(GridView) view.findViewById(R.id.grid);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
String value=(String)grid.getItemAtPosition(i);
Intent intent=new Intent(getActivity(),Book.class);
intent.putExtra("web", web[i]);
intent.putExtra("imageId", imageId[i]);
intent.putExtra("fullimg", fullimg[i]);
startActivity(intent);
}
});
//return inflater.inflate(R.layout.fragment_status, container, false);
return view;
}
这是我的书 activity 代码,其中要显示文本和图像 图片显示正常,但我不知道如何显示文字:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book);
TextView textView = (TextView) findViewById(R.id.txttitle);
ImageView imageView = (ImageView)findViewById(R.id.imageView2);
//textView.setText(txttitle);
//textView.setText(web[i]);
imageView.setImageResource(getIntent().getIntExtra("fullimg", R.drawable.person7));
}
//textView.setText(txttitle);
//textView.setText(web[i]);
imageView.setImageResource(getIntent().getIntExtra("fullimg", R.drawable.person7));
您不会像检索图像那样检索传递的文本 ;)
只需从 Intent 中获取您的文本。我正在考虑 web
包含您的文本,然后下面的代码将为您获取文本
textView.setText(getIntent().getStringExtra("web"));
设置textview只是被注释掉了。只需将 TextView 设置为类似于 imageview:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book);
TextView textView = (TextView) findViewById(R.id.txttitle);
ImageView imageView = (ImageView)findViewById(R.id.imageView2);
textView.setText(getIntent().getStringExtra("web"))
imageView.setImageResource(getIntent().getIntExtra("fullimg", R.drawable.person7));
}
不是通过 Intent 将变量传递给下一个 activity,而是使用应用程序 class 来存储数据并在应用程序中的任何地方使用。
在传递文本值时输入您要输入的常量 textView.setText(getIntent().getStringExtra("YourKeyValue"));