2012-01-03 242 views
1

這裏我使用了兩個類dulux,jsonfiles類。我試圖將數據傳遞到jsonfiles使用對象多樂士,但它傳遞空值,這是我的代碼:如何將數據從一個類傳遞到另一個類?

jasonfiles.class.package com.cethar.grid; 
import java.io.InputStream; 
import java.util.Arrays; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Vector; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.R.string; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TextView; 
public class Jsonfiles extends Activity { 
TextView tvdata,tvdata1,tvdata2,tvdata3,tvdata4,tvdata5; 
public static JSONObject jObject; 
String arr_name,arr_row,arr_column,arr_rows,string_filter,string_arrange,string_color,string_produ ct,string_groups,string_video,string_schemes; 
int c1,c2,c3; 
int file_filter,file_arrangement,file_color; 
String v3,v4,v5,v6,v7,v8; 
Vector v =new Vector(); 
public HashMap color_id = new HashMap(); 
HashMap arrange_id = new HashMap(); 
HashMap filter_main = new HashMap(); 
HashMap arrange_name = new HashMap(); 
HashMap product_name = new HashMap(); 
HashMap group_name = new HashMap(); 
HashMap video_id = new HashMap(); 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.json);  
arrangementFile(); 
filterFile(); 
colorFile(); 
string element = Product();  
} 
public String parseFile(int file) 
{ 
try{ 
String x = ""; 
InputStream is = this.getResources().openRawResource(file); 
byte[] buffer = new byte[is.available()]; 
while (is.read(buffer) != -1); 
String jsontext = new String(buffer); 
return(jsontext); 
} catch (Exception e) { 
String Ex=" "; 
return(Ex); 
} 
} 
public void colorFile() { 
try { 
string_color=parseFile(R.raw.colours); 
String z = ""; 
JSONArray JSONArray_color = new JSONArray(string_color); 
z = "JSON parsed.\nThere are [" + JSONArray_color.length() + "]\n\n"; 
int i; 
for (i = 0; i < JSONArray_color.length(); i++) { 
JSONObject JSONObject_color = JSONArray_color.getJSONObject(i); 
HashMap color_elements = new HashMap(); 
color_elements.put("name",JSONObject_color.getString("name")); 
color_elements.put("code",JSONObject_color.getString("code")); 
color_elements.put("red",JSONObject_color.getString("red")); 
color_elements.put("green",JSONObject_color.getString("green")); 
color_elements.put("blue",JSONObject_color.getString("blue")); 
color_elements.put("availability",JSONObject_color.getString("availability")); 
HashMap color_details = new HashMap(); 
color_details.put("detail",color_elements); 
color_id.put(JSONObject_color.getString("id"), color_elements);  
e.printStackTrace(); 
} 
} 
public void arrangementFile() { 
try { 
string_arrange=parseFile(R.raw.arrangement); 
JSONArray JSONArray_arrange = new JSONArray(string_arrange); 
String y =""; 
int i; 
for (i = 0; i < JSONArray_arrange.length(); i++) { 
JSONObject JSONObject_arrange = JSONArray_arrange.getJSONObject(i); 
HashMap arrange_elements = new HashMap(); 
Vector arow = new Vector(); 
Vector acolumn = new Vector(); 
arow.add(JSONObject_arrange.getString("row")); 
acolumn.add(JSONObject_arrange.getString("column")); 
String arowsstr; 
arowsstr = JSONObject_arrange.getString("rows"); 
arowsstr = arowsstr.substring(1, arowsstr.length()-1); 
String[] astrrows = arowsstr.split(","); 
Vector arows = new Vector(Arrays.asList(astrrows)); 
arrange_elements.put("row", arow); 
arrange_elements.put("column", acolumn); 
arrange_elements.put("rows", arows); 
arrange_name.put(JSONObject_arrange.getString("name"), arrange_elements); 
HashMap HashMap_clr = new HashMap(); 
String clr = JSONObject_arrange.getString("column"); 
} 
Vector v =new Vector(); 
v= (Vector)((HashMap) arrange_name.get("RR01")).get("rows"); 
y += v.get(0) + " " + v.get(1)+ " " +v.get(2) + " " +v.get(3); 
} catch (JSONException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
} 
public void filterFile() { 
try { 
string_filter=parseFile(R.raw.filters); 
JSONArray JSONArray_filter = new JSONArray(string_filter); 
int i; 
String x =""; 
for (i = 0; i < JSONArray_filter.length(); i++) { 
JSONObject JSONObject_filter = JSONArray_filter.getJSONObject(i); 
HashMap product_elements = new HashMap(); 
String filter_name = JSONObject_filter.getString("name"); 
String str_stripes; 
str_stripes = JSONObject_filter.getString("stripes"); 
str_stripes = str_stripes.substring(1, str_stripes.length()-1); 
str_stripes = str_stripes.replace("\"", ""); 
String[] strarr_stripes = str_stripes.split(","); 
HashMap HashMap_stripes = new HashMap(); 
List<String> stripesList = Arrays.asList(strarr_stripes); 
for(String fls : stripesList) 
{ 
HashMap_stripes.put(fls, arrange_name.get(fls)); 
} 
filter_main.put(JSONObject_filter.getString("name"),HashMap_stripes); 
} 
String s1 = " "; 
String s2 = " "; 
Vector v1 = ((Vector) ((HashMap) ((HashMap)filter_main.get("reds")).get("RR02")).get("row")); 
Vector v2 = (Vector) ((HashMap)((HashMap)filter_main.get("reds")).get("RR02")).get("column"); 
v= (Vector)((HashMap) ((HashMap) filter_main.get("reds")).get("RR01")).get("rows"); 
x += v.get(0) + " " + v.get(1)+ " " +v.get(2) + " " +v.get(3)+ " " +v.get(4)+ " " +v.get(5) + " " +v.get(6); 
s1 += v1.get(0) + " " ; 
s2 += v2.get(0) + " " ; 
v6=(String) v.get(0).toString(); 
v3= (String) ((HashMap) color_id.get("168")).get("red"); 
v4 =(String) (((HashMap) color_id.get("168")).get("green")); 
v5 =(String) (((HashMap) color_id.get("168")).get("blue")); 
c1= Integer.parseInt(v3); 
c2= Integer.parseInt(v4); 
c3= Integer.parseInt(v5); 
for(i=0;i<=6;i++) 
{ 
} 
} catch (JSONException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
} 
public string Product() { 
string v=null; 
try { 
string_product=parseFile(R.raw.product_list); 
JSONArray JSONArray_product = new JSONArray(string_product); 
String y =""; 
for (int i = 0; i < JSONArray_product.length(); i++) { 
JSONObject JSONObject_product = JSONArray_product.getJSONObject(i); 
HashMap product_elements = new HashMap(); 
product_elements.put("CLEANING_UP",JSONObject_product.getString("CLEANING_UP")); 
product_elements.put("NUMBER_COATS",JSONObject_product.getString("NUMBER_COATS")); 
product_elements.put("PACK_SIZES",JSONObject_product.getString("PACK_SIZES"));  product_elements.put("SHORTDESCRIPTION",JSONObject_product.getString("SHORTDESCRIPTION")); 
product_elements.put("NAME",JSONObject_product.getString("NAME")); 
product_elements.put("DRYING_TIME",JSONObject_product.getString("DRYING_TIME")); 
product_elements.put("SUBSTRATE",JSONObject_product.getString("SUBSTRATE")); 
product_elements.put("LARGEIMAGEURL",JSONObject_product.getString("LARGEIMAGEURL")); 
product_elements.put("APPLICATION_METHOD",JSONObject_product.getString("APPLICATION_METHOD" )); 
product_elements.put("SMALLIMAGE",JSONObject_product.getString("SMALLIMAGE")); 
product_elements.put("RANGE",JSONObject_product.getString("RANGE")); 
product_elements.put("BRAND",JSONObject_product.getString("BRAND")); 
product_elements.put("SEQ",JSONObject_product.getString("SEQ"));  
product_elements.put("COVERAGE",JSONObject_product.getString("COVERAGE")); 
product_elements.put("FINISH",JSONObject_product.getString("FINISH")); 
product_name.put(JSONObject_product.getString("SHORTCODE"),product_elements); 
} 
} catch (JSONException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
return (v); 
} 
public void groupsFile() { 
try { 
string_groups=parseFile(R.raw.availability_groups); 
JSONArray JSONArray_groups= new JSONArray(string_groups); 
String y =""; 
int i; 
for (i = 0; i < JSONArray_groups.length(); i++) { 
JSONObject JSONObject_groups = JSONArray_groups.getJSONObject(i); 
HashMap group_elements = new HashMap(); 
group_elements.put("groups",JSONObject_groups.getString("groups")); 
} 
} catch (JSONException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
} 
public void string_Video() { 
string v=null; 
try { 
string_video=parseFile(R.raw.video_list); 
JSONArray JSONArray_video = new JSONArray(string_video); 
for (int i = 0; i < JSONArray_video.length(); i++) { 
JSONObject JSONObject_video = JSONArray_video.getJSONObject(i); 
HashMap video_elements = new HashMap(); 
video_elements.put("name",JSONObject_video.getString("name")); 
video_elements.put("thumbnailURL",JSONObject_video.getString("thumbnailURL")); 
video_elements.put("URL",JSONObject_video.getString("URL")); 
video_elements.put("desc",JSONObject_video.getString("desc")); 
video_elements.put("previewURL",JSONObject_video.getString("previewURL")); 
video_id.put(JSONObject_video.getString("video_id"),video_elements); 
} 
} catch (JSONException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
} 

這是我的活動類dulux.class

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.ViewGroup; 
import android.widget.LinearLayout; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 
import android.widget.TableRow.LayoutParams; 
public class DuluxColorGridAActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
Jsonfiles js=new Jsonfiles(); 
System.out.println(""+js.c1); 
System.out.println(""+js.c2); 
System.out.println(""+js.c3); 
LinearLayout ll = (LinearLayout) findViewById(R.id.list_parent); 
TableLayout layout = new TableLayout (this); 
layout.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
ViewGroup.LayoutParams.FILL_PARENT)); 
layout.setPadding(8,8,8,8); 
TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 
ViewGroup.LayoutParams.FILL_PARENT, 
1.0f); 
rowLp.setMargins(6, 0, 0,0); 
TableRow.LayoutParams cellLp = new TableRow.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 
ViewGroup.LayoutParams.FILL_PARENT, 
1.0f); 
cellLp.setMargins(6,0, 0, 0); 
for (int f=0; f<=6; f++) { 
js.filterFile(); 
TableRow tr = new TableRow(this); 
tr.setBackgroundColor(Color.BLACK); 
tr.setPadding(0,0, 0,2); 
TableRow.LayoutParams llp = new 
TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); 
llp.setMargins(0, 0, 2, 0);//2px right-margin 
//New Cell 
for (int c1=0; c1<=3; c1++) { 
LinearLayout cell = new LinearLayout(this); 
if(c1==0) 
cell.setBackgroundColor(Color.rgb(js.c1, js.c2, js.c3)); 
else 
cell.setBackgroundColor(Color.rgb(0,243, 0)); 
//cell.setBackgroundColor(Color.WHITE); 
cell.setLayoutParams(llp);//2px border on the right for the cell 
TextView b = new TextView (this); 
b.setText("Sample"); 
b.setTextSize(10.0f); 
b.setHeight(75); 
b.setWidth(107); 
b.setPadding(0, 0, 4, 0); 
cell.addView(b); 
tr.addView(cell,cellLp); 
} // for 
layout.addView(tr,rowLp); 
} // for 
ll.addView(layout); 
//  super.setContentView(layout); 
} 
} 

這是我的示例JSON文件代碼

{"id" : 168, "name" : "Plum Escape", "code" : "12RR 07/229", "red" : 102, "green" : 65, "blue" : 88, "availability" : "g12"} 

RGB顏色不能檢索從一類到另一個類,我怎麼能獲得價值一類到另一個

+1

您的問題的解釋完全不清楚。你可以嘗試擴展它(並清除文本中最糟糕的錯別字,如缺失空格,額外空格等 - 空格和大寫確實有助於可讀性)。 – 2012-01-03 12:13:43

+0

請在發佈問題前進行一些搜索,例如查看問題右側的相關鏈接。 – 2012-01-03 12:14:18

+0

我編輯過這些空間檢查一次,並建議我如何解決這個問題 – NKN 2012-01-03 12:41:31

回答

0

如果您想將值傳遞給另一個Android活動,則可以將其放入一個Intent中,然後在目標活動上調用startActivity。

Intent i = new Intent(this, ClassToSendTo.class); 
i.putExtra("id", id); startActivity(i); 

參考link

第2步:Android; Implementing global state; share data between Activities and across your application

第三步:在靜態setter方法來設置這些調用活動使用static variables

通過這種方式,我們可以隨意更改任何活動中的值,而不管各種活動之間的控制精確流程。

請注意,如果你不關心的應用程序相同的活動(類)的多個副本的實例化這一招只能用,這是最容易實現的

1

使用捆綁的這個。

Sender.java -

Intent i = new Intent(this, Receiver.class); 
Bundle bundle = new Bundle(); 
bundle.putString("id", "100"); // "id" is a key for identifer and "100" is value 
i.putExtras(bundle); 
startActivity(i); 

Receiver.java -

Bundle bundle = getIntent().getExtras(); 
int id = bundle.getString("id"); 
Toast.makeTest(getApplicationContext(), String.valueOf(id), Toast.LENGTH_LONG).show(); 

而且,從開發人員的網站參考Bundle

2

試圖讓這個例子中,它很容易再

打開活動並等待結果,我們可以用這個sintax

Intent newIntent = new Intent(this.getApplicationContext(), ActivityClass2.class); 
startActivityForResult(newIntent, 0); 

ActivityClass2的是,我們需要活動的類名創建並從當前活動中打開它。 startActivityForResult是運行newActivity的一種方法。後來我們就可以有結果通過添加上的當前活動這個功能

protected void onActivityResult(int requestCode, int resultCode, Intent data) 

首先到數據或參數發送到newActivity我們可以利用這個sintax

Bundle bundle = new Bundle(); 
bundle.putString(「param1″, 「test」); 

Intent newIntent = new Intent(this.getApplicationContext(), ActivityClass2.class); 
newIntent.putExtras(bundle); 
startActivityForResult(newIntent, 0); 

在ActivityClass2,我們可以使用閱讀本參數這sintax

Bundle bundle = this.getIntent().getExtras(); 
String param1 = bundle.getString(「param1″); 

要小心使用參數1,因爲它可以爲空,如果我們不把它。

因此,在Android中的兩個活動之間發送或傳遞數據或參數非常容易。最後一件事是爲startActivityForResult返回一個值,我們可以添加此行代碼來關閉ActivityClass2並將焦點返回給opener並將數據發送到opener活動。

Bundle bundle = new Bundle(); 
bundle.putString(「status」, 「OK」); 
Intent mIntent = new Intent(); 
mIntent.putExtras(bundle); 
setResult(RESULT_OK, mIntent); 
finish(); 
1

使用本:

在發件人類

Intent intent = new Intent(getApplicationContext(),Receiver.class);  

intent.putExtra("key_variable", your_variable_HoldingValueToBeSend); 

startActivity(intent); 

在接收器類

Intent intent = getIntent(); 
String get_value = intent.getStringExtra("key_variable"); 

現在你已經發送者類的值發送到您的接收器上課變量"get_value" &您可以在接收器類中使用此值。「

相關問題