2011-04-28 204 views
2

我有一個填充到活動(Main.java)的JSON文件。Android:將額外活動從一項活動傳遞給另一項活動

此活動顯示來自我的JSON條目上URL的3個隨機圖像。

我想要做的是:我有13個不同的條目在我的JSON上,每當我點擊顯示的隨機圖片時,它會轉到另一個包含圖片,標題和描述的活動(ProjectDetail.java)取決於項目I點擊它在JSON上的條目。

我有什麼是使用額外的我不知道如何執行,因爲我使用JSON。我應該在我的Main類中添加到我的top_listener方法中,並且應該將哪些內容添加到我的ProjectDetail類中?

謝謝。

Main.java

public class Main extends Activity { 
      /** Called when the activity is first created. */ 

      ArrayList<Project> prjcts=null; 
      private ImageThreadLoader imageLoader = new ImageThreadLoader(); 
      private final static String TAG = "MediaItemAdapter"; 


      @Override 
      public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.main); 

       prjcts = new ArrayList<Project>(); 
       WebService webService = new WebService("http://liebenwald.spendino.net/admanager/dev/android/projects.json"); 
       Map<String, String> params = new HashMap<String, String>(); 
       params.put("var", ""); 
       String response = webService.webGet("", params); 

       try 
       { 
        Type collectionType = new TypeToken<ArrayList<Project>>(){}.getType(); 
        List<Project> lst= new Gson().fromJson(response, collectionType); 
        for(Project l : lst) 
        { 
         prjcts.add(l); 
         ConstantData.projectsList.add(l); 
        } 
       } 
       catch(Exception e) 
       { 
        Log.d("Error: ", e.getMessage()); 
       } 

       final Button project = (Button) findViewById(R.id.btn_projectslist); 
       final Button infos = (Button) findViewById(R.id.btn_infos); 
       final Button contact = (Button) findViewById(R.id.btn_contact); 
       project.setOnClickListener(project_listener); 
       infos.setOnClickListener(infos_listener); 
       contact.setOnClickListener(contact_listener); 

       ImageView image1; 
       ImageView image2; 
       ImageView image3; 

       try { 
        image1 = (ImageView)findViewById(R.id.top1); 
        image2 = (ImageView)findViewById(R.id.top2); 
        image3 = (ImageView)findViewById(R.id.top3); 
        } catch(ClassCastException e) { 
        Log.e(TAG, "Your layout must provide an image and a text view with ID's icon and text.", e); 
        throw e; 
        } 


        Bitmap cachedImage1 = null; 
        Bitmap cachedImage2 = null; 
        Bitmap cachedImage3 = null; 

        //randomize the index of image entry 

        int max = prjcts.size(); 
        List<Integer> indices = new ArrayList<Integer>(max); 
        for(int c = 0; c < max; ++c) 
        { 
         indices.add(c); 
        } 

        int arrIndex = (int)((double)indices.size() * Math.random()); 
        int randomIndex1 = indices.get(arrIndex); 
        indices.remove(arrIndex); 


        int randomIndex2 = indices.get(arrIndex); 
        indices.remove(arrIndex); 


        int randomIndex3 = indices.get(arrIndex); 
        indices.remove(arrIndex); 



        setImage(cachedImage1, image1, prjcts.get(randomIndex1)); 
        setImage(cachedImage2, image2, prjcts.get(randomIndex2)); 
        setImage(cachedImage3, image3, prjcts.get(randomIndex3)); 

        image1.setOnClickListener(top_listener); 
        image2.setOnClickListener(top_listener); 
        image3.setOnClickListener(top_listener); 
      } 



      public void setImage(Bitmap cachedImage, final ImageView image, Project pro) 
      { 
       //Bitmap cachedImage1 = null; 
       try { 
        cachedImage = imageLoader.loadImage(pro.smallImageUrl, new ImageLoadedListener() 
        { 
         public void imageLoaded(Bitmap imageBitmap) 
         { 
          image.setImageBitmap(imageBitmap); 
          //notifyDataSetChanged();     
         } 
        }); 
       } catch (MalformedURLException e) { 
        Log.e(TAG, "Bad remote image URL: " + pro.smallImageUrl, e); 
       } 
       if(cachedImage != null) { 
        image.setImageBitmap(cachedImage); 
        } 
      } 


      private OnClickListener top_listener = new OnClickListener() { 
       public void onClick(View v) { 
          Intent top = new Intent(Main.this, InfosActivity.class); 
          startActivity(top); 
       } 
       }; 

ProjectDetail.java

public class ProjectDetail extends Activity implements OnClickListener{ 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.project); 

     Button weitersagen = (Button) findViewById(R.id.btn_weitersagen); 
     weitersagen.setOnClickListener(this); 

     Button sms = (Button) findViewById(R.id.btn_sms_spenden); 
     sms.setOnClickListener(this); 

     int position = getIntent().getExtras().getInt("spendino.de.ProjectDetail.position"); 
     Project project = ConstantData.projectsList.get(position); 


     try { 
      ImageView projectImage = (ImageView)findViewById(R.id.project_image); 
      Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(project.bigImageUrl).getContent()); 
      projectImage.setImageBitmap(bitmap); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     TextView project_title = (TextView)findViewById(R.id.txt_project_title); 
     project_title.setText(project.project_title); 

     TextView organization_title = (TextView)findViewById(R.id.txt_organization_title); 
     organization_title.setText(Html.fromHtml("von " +project.organization_title)); 

     TextView project_description = (TextView)findViewById(R.id.txt_project_description); 
     project_description.setText(Html.fromHtml(project.project_description)); 

    } 

我也有這個ConstantData.java,其持有我的JSON性能指標:

public class ConstantData{ 

    public static String project_title = "project title"; 
    public static String organization_title = "organization title"; 
    public static String keyword = "keyword"; 
    public static String short_code = "short code"; 
    public static String project_description = "description"; 
    public static String smallImageUrl = "smallImageUrl"; 
    public static String bigImageUrl = "bigImageUrl"; 
    public static String price= "price"; 
    public static String country= "country"; 



    public static ArrayList<Project> projectsList = new ArrayList<Project>(); 


    public int describeContents() { 
     return 0; 
    } 

    public void writeToParcel(Parcel out, int flags) { 
     out.writeString(project_title); 
     out.writeString(organization_title); 
     out.writeString(keyword); 
     out.writeString(short_code); 
     out.writeString(project_description); 
     out.writeString(smallImageUrl); 
     out.writeString(bigImageUrl); 
     out.writeString(price); 
     out.writeString(country); 
    } 

    public static final Parcelable.Creator<ConstantData> CREATOR 
      = new Parcelable.Creator<ConstantData>() { 
     public ConstantData createFromParcel(Parcel in) { 
      return new ConstantData(in); 
     } 

     public ConstantData[] newArray(int size) { 
      return new ConstantData[size]; 
     } 
    }; 

    private ConstantData(Parcel in) { 
     project_title = in.readString(); 
     organization_title = in.readString(); 
     keyword = in.readString(); 
     short_code = in.readString(); 
     project_description = in.readString(); 
     smallImageUrl = in.readString(); 
     bigImageUrl = in.readString(); 
     price = in.readString(); 
     country = in.readString(); 
    } 
} 

回答

5

通過從Parcelable擴展並實現一些方法(請參閱documentation),可以使類ConstantData可序列化。然後,你可以通過做

intent.putExtra("jsonData", constantDataInstance); 

,並從其他活動獲取它傳遞一個constantData實例作爲一個額外的(在它的onCreate()方法)與

getIntent().getExtras().getParcelable("jsonData"); 

否則你剛剛過去的額外每領域獨立,但它會是一團糟。這種方式不僅更容易閱讀和一切,而且「精心設計」。

+1

我已經編輯我的ConstantData,請看看..然後什麼應該改變constantDataInstance? thx – hectichavana 2011-04-28 15:24:07

+0

對於我所看到的,你將你的json數據放在ConstantData的數組中,這樣你就不會使用它的任何其他實例變量。您應該解析json並將每個對象放入一個變量中。然後,你傳遞給可序列化的constantDataInstance,它攜帶所有的信息。 – ferostar 2011-04-28 16:12:53

+0

我明白了,你能否給我提供一個例子?謝謝 – hectichavana 2011-04-29 07:57:56

2

從一個活動傳遞到另一個信息,當您啓動新的,你做到以下幾點:

Intent top = new Intent(Main.this, InfosActivity.class); 
    Bundle b = new Bundle(); 
    b.putString("key1", "value2"); 
    b.putString("key2", "value2"); 
    b.putString("key3", "value3"); 
    top.putExtras(b); 
    startActivity(top); 

然後在新啓動的活動,在OnCreate()把以下內容:

Bundle b = getIntent().getExtras(); 
    b.get("key1"); 
    b.get("key2"); 
    b.get("key3"); 

這將通過使用您提供的密鑰來獲取以前活動的值。

對於更復雜的對象,你應該擴展Parcelable(可能是你所需要的),然後使用:

b.putParcelable("Key4", yourParcelableObject); 

而在你的onCreate()

b.getParcelable("Key4"); 

我希望這有助於。

+1

第一種方法是通過手動插入值的權利?事情是,我的價值已經在JSON上聲明。例如:「short_code」:「81190」,我相信這和我的ConstantData類有什麼關係,任何想法? – hectichavana 2011-04-28 13:45:50

+0

我真的不明白你在找什麼,對不起。 如果您想傳遞一個完整的ConstantData對象,只需讓您的類擴展Parcelable,然後使用b.putParceable()方法即可。 – NotACleverMan 2011-04-28 13:58:47

+1

thx。假設我需要將額外的(例如key1)設置成TextView到setText,我應該如何將該參數作爲此方法的參數? projtitle.setText(); – hectichavana 2011-04-28 15:00:05

相關問題