2015-02-10 59 views
0

第一即時動態加載數據到一個網格,其是在PMenu.java,那麼每一個項具有更多的按鈕。一旦我按下,我想要加載圖像,名稱及其在項目描述視圖中的數量。動態加載數據到一個新的視圖

林使用自定義網格將數據加載到在PMenu.java網格,我已經放置在自定義網格的按鈕,這樣它會導航到viewmore.java。

我想知道當我按下按鈕,然後如何將數據加載到viewmore.java文件

PMenu.java片段

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類

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



} 
+0

你想在viewmore.java加載按鈕點擊什麼? - 如果整個數組值,然後通過使用intent.intent.putStringArrayListExtra(name,value)傳遞它們; – chiru 2015-02-10 10:22:13

回答

1

在開始viewmore活動之前,您可以使用intent.putExtra("key", value)方法。

然後在viewmore活動,你可以從getIntent().get*Extra("key")方法這些數據。

像:

Intent next = new Intent(context, viewmore.class); 
next.putExtra("description", descriptions.get(position)); 
next.putExtra("imageUrl", imageUrls.get(position)); 
context.startActivity(next); 

然後(在viewmore活動):

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

ü可以檢查我更新的問題,我已經發布viewmore.java文件,在即時得到一個錯誤----公共查看getView(最終詮釋的立場,觀點convertView,ViewGroup以及母公司){ - 說「的方法getView( int,View,ViewGroup)必須重寫或實現一個超類型方法「 – 2015-02-10 10:57:13

+0

此消息告訴你''活動'中不存在'getView'方法。但它存在於'BaseAdapter'中,就像你在'CustomGrid extends BaseAdapter'中使用它一樣。 你應該看看這個Android的樣本:https://developer.android.com/training/basics/firstapp/starting-activity.html – Gorcyn 2015-02-10 11:08:18

+0

該教程幫我將內容加載到其他頁面,但即時通訊掙扎加載它確切的文本視圖 – 2015-02-10 11:39:01

相關問題