Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
887 views
in Technique[技术] by (71.8m points)

java - Dynamically loading data to a new view

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;
        }



}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...