问题描述
首先即时动态加载数据到一个网格,其是在PMenu.java,那么每一个项具有更多的按钮。有一次我pressed,我想加载图像,名称及其在物品描述视图金额。
林使用自定义网格将数据加载到PMenu.java网格,我已经放置在自定义网格的一个按钮,以便将导航到viewmore.java
我想知道,一旦我preSS的按钮,然后如何将数据加载到viewmore.java文件
PMenu.java片段
的GridView网格;@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
查看查看= inflater.inflate(R.layout.menu_grid_main,集装箱,FALSE); 新PMenuAsyncTask(getActivity(),这一点).execute();
格=(GridView控件)view.findViewById(R.id.grid);
返回视图。
}@覆盖
公共无效onTaskCompleted(JSONArray responseJson){ 尝试{
清单<串GT;说明=新的ArrayList<串GT;();
清单<串GT; imageUrls =新的ArrayList<串GT;(); 的for(int i = 0; I< responseJson.length(); ++ I){
的JSONObject对象= responseJson.getJSONObject(I) 如果((object.getString(MainCategoryID))。等于(1)
&功放;&安培; (object.getString(SubCategoryID))。等于(1)){
Log.i(IMAGEURL,object.getString(IMAGEURL));
imageUrls.add(object.getString(IMAGEURL));
Log.i(描述,object.getString(描述));
descriptions.add(object.getString(描述));
} } CustomGrid适配器=新CustomGrid(getActivity(),描述,
imageUrls);
grid.setAdapter(适配器);
}赶上(JSONException E){
e.printStackTrace();
}
}
CustomGrid类
公共类CustomGrid延伸BaseAdapter {
私人上下文的背景下;
私人最终名单<串GT;描述;
私人最终名单<串GT; imageUrls; 公共CustomGrid(上下文C,列表与LT;弦乐>的描述,列表与LT;弦乐> imageUrls){
this.context = C;
this.descriptions =描述;
this.imageUrls = imageUrls;
} @覆盖
公众诠释的getCount(){
返回descriptions.size();
} @覆盖
公共对象的getItem(INT位置){
返回descriptions.get(位置);
} @覆盖
众长getItemId(INT位置){
返回的位置;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
ViewHolder持有人;
如果(convertView == NULL){
持有人=新ViewHolder();
convertView = LayoutInflater.from(上下文).inflate(
R.layout.fragment_pizza,父母,假);
holder.ivImage =(ImageView的)convertView
.findViewById(R.id.grid_image);
holder.tvHeader =(TextView中)convertView
.findViewById(R.id.grid_text);
convertView.setTag(保持器); }其他{
支架=(ViewHolder)convertView.getTag();
}
holder.tvHeader.setText(descriptions.get(位置));
Picasso.with(this.context).load(imageUrls.get(位置))到(holder.ivImage)。 键返回按钮=(按钮)convertView.findViewById(R.id.button1);
backButton.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(查看视图){
接下来意向=新意图(背景下,viewmore.class);
context.startActivity(下);
next.putExtra(说明,descriptions.get(位置));
next.putExtra(图片网址,imageUrls.get(位置));
context.startActivity(下);
}
});
返回convertView;
} 私有类ViewHolder {
私人TextView的tvHeader;
私人ImageView的ivImage;
}
}
viewmore.java
公共类viewmore延伸活动{ 私人上下文的背景下;
私人最终名单<串GT;描述;
私人最终名单<串GT; imageUrls; 公共viewmore(上下文C,列表与LT;弦乐>的描述,列表与LT;弦乐> imageUrls){
this.context = C;
this.descriptions =描述;
this.imageUrls = imageUrls;
}
私人动作条动作条; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_viewmore); 字符串描述= getIntent()getStringExtra(说明)。
串图片网址= getIntent()getStringExtra(图片网址); 动作条= getActionBar();
actionBar.setDisplayHomeAsUpEnabled(真);
actionBar.setDisplayShowHomeEnabled(假);
actionBar.setDisplayShowTitleEnabled(真);
actionBar.setDisplayUseLogoEnabled(假); } @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
MenuInflater吹气= getMenuInflater();
inflater.inflate(R.menu.activity_main_actions,菜单); //使用搜索查看搜索关联配置 返回super.onCreateOptionsMenu(菜单);
} @覆盖
公共布尔onOptionsItemSelected(菜单项项){
//将每个动作项目点击适当的行动
开关(item.getItemId()){
案例R.id.action_cart:
返回true;
案例R.id.action_search:
//搜索行动
返回true; 默认:
返回super.onOptionsItemSelected(项目);
}
} @覆盖
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
ViewHolder持有人;
如果(convertView == NULL){
持有人=新ViewHolder();
convertView = LayoutInflater.from(上下文).inflate(
R.layout.activity_viewmore,父母,假);
holder.ivImage =(ImageView的)convertView
.findViewById(R.id.grid_image);
holder.tvHeader =(TextView中)convertView
.findViewById(R.id.grid_text);
convertView.setTag(保持器); }其他{
支架=(ViewHolder)convertView.getTag();
}
holder.tvHeader.setText(descriptions.get(位置));
Picasso.with(this.context).load(imageUrls.get(位置))到(holder.ivImage)。 返回convertView;
} 私有类ViewHolder {
私人TextView的tvHeader;
私人ImageView的ivImage;
}}
您可以使用 intent.putExtra(钥匙,值)
启动<$ C之前的方法$ C> viewmore 活动。
然后在 viewmore
活动,您可以从 getIntent()。获得*额外的(钥匙)$这些数据C $ C>方法。
我爱:
意图下一=新意图(背景下,viewmore.class);
next.putExtra(说明,descriptions.get(位置));
next.putExtra(图片网址,imageUrls.get(位置));
context.startActivity(下);
然后(在 viewmore
活动):
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_viewmore); 字符串描述= getIntent()getStringExtra(说明)。
串图片网址= getIntent()getStringExtra(图片网址); //使用布局activity_viewmore.xml定义其ID获取的TextView
TextView的电视=(的TextView)findViewById(R.id.my_text_view);
tv.setText(介绍); ImageView的ImageView的=(ImageView的)findViewById(R.id.my_image_view);
Picasso.with(本).load(图片网址).into(ImageView的);
}
First im loading the data dynamically to a grid which is in the PMenu.java, then each item has view more button. once I pressed that I want to load the image, name and its amount in the item description view.
Im using a custom grid to load data to the grid in PMenu.java, and I have placed a button in the custom grid, so that it will navigate to viewmore.java.
I want to know once i press the button then how to load the data to viewmore.java file
PMenu.java fragment
GridView grid;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu_grid_main, container, false);
new PMenuAsyncTask(getActivity(), this).execute();
grid = (GridView) view.findViewById(R.id.grid);
return view;
}
@Override
public void onTaskCompleted(JSONArray responseJson) {
try {
List<String> descriptions = new ArrayList<String>();
List<String> imageUrls = new ArrayList<String>();
for (int i = 0; i < responseJson.length(); ++i) {
JSONObject object = responseJson.getJSONObject(i);
if ((object.getString("MainCategoryID")).equals("1")
&& (object.getString("SubCategoryID")).equals("1")) {
Log.i("ImageURL ", object.getString("ImageURL"));
imageUrls.add(object.getString("ImageURL"));
Log.i("Description ", object.getString("Description"));
descriptions.add(object.getString("Description"));
}
}
CustomGrid adapter = new CustomGrid(getActivity(), descriptions,
imageUrls);
grid.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
CustomGrid class
public class CustomGrid extends BaseAdapter {
private Context context;
private final List<String> descriptions;
private final List<String> imageUrls;
public CustomGrid(Context c, List<String> descriptions, List<String> imageUrls) {
this.context = c;
this.descriptions = descriptions;
this.imageUrls = imageUrls;
}
@Override
public int getCount() {
return descriptions.size();
}
@Override
public Object getItem(int position) {
return descriptions.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(
R.layout.fragment_pizza, parent, false);
holder.ivImage = (ImageView) convertView
.findViewById(R.id.grid_image);
holder.tvHeader = (TextView) convertView
.findViewById(R.id.grid_text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvHeader.setText(descriptions.get(position));
Picasso.with(this.context).load(imageUrls.get(position)).into(holder.ivImage);
Button backButton = (Button) convertView.findViewById(R.id.button1);
backButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Intent next = new Intent(context, viewmore.class);
context.startActivity(next);
next.putExtra("description", descriptions.get(position));
next.putExtra("imageUrl", imageUrls.get(position));
context.startActivity(next);
}
});
return convertView;
}
private class ViewHolder {
private TextView tvHeader;
private ImageView ivImage;
}
}
viewmore.java
public class viewmore extends Activity {
private Context context;
private final List<String> descriptions;
private final List<String> imageUrls;
public viewmore(Context c, List<String> descriptions, List<String> imageUrls) {
this.context = c;
this.descriptions = descriptions;
this.imageUrls = imageUrls;
}
private ActionBar actionBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_viewmore);
String description = getIntent().getStringExtra("description");
String imageUrl = getIntent().getStringExtra("imageUrl");
actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
// Associate searchable configuration with the SearchView
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_cart:
return true;
case R.id.action_search:
// search action
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(
R.layout.activity_viewmore, parent, false);
holder.ivImage = (ImageView) convertView
.findViewById(R.id.grid_image);
holder.tvHeader = (TextView) convertView
.findViewById(R.id.grid_text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvHeader.setText(descriptions.get(position));
Picasso.with(this.context).load(imageUrls.get(position)).into(holder.ivImage);
return convertView;
}
private class ViewHolder {
private TextView tvHeader;
private ImageView ivImage;
}
}
You can use intent.putExtra("key", value)
methods before starting the viewmore
Activity.
Then in the viewmore
Activity, you can get these data from the getIntent().get*Extra("key")
methods.
Like:
Intent next = new Intent(context, viewmore.class);
next.putExtra("description", descriptions.get(position));
next.putExtra("imageUrl", imageUrls.get(position));
context.startActivity(next);
Then (in viewmore
Activity):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_viewmore);
String description = getIntent().getStringExtra("description");
String imageUrl = getIntent().getStringExtra("imageUrl");
// Get the TextView using its ID defined in the layout activity_viewmore.xml
TextView tv = (TextView) findViewById(R.id.my_text_view);
tv.setText(description);
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Picasso.with(this).load(imageUrl).into(imageView);
}
这篇关于动态加载数据到一个新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!