2013-08-22 79 views
-2

如何訪問超出主類的值?我想在參數打印在主類elementarytxtview 的價值,但在參數打印空值,我想在參數elementarytextview值傳遞給其他活動如何從主要方法向內部打印主值方法

 public class HomeMenu extends Activity { 
ImageButton imgNews, imgContact, imgSetting; 
ListView listMainMenu; 
ListView Middleschoollist, HighSchoollist, Atipicalschoollist; 
String status; 
firstscreenadapter mma; 
String SelectMenuAPI; 
String SelectMenuAPI2; 
String url1; 
String elementry; 

// String High; 
String message; 
TextView Elementarytxt, Middletxt, Hightxt, Atypicaltxt; 
static ArrayList<Long> Category_ID = new ArrayList<Long>(); 
static ArrayList<String> Category_name = new ArrayList<String>(); 
String elementarytxtview; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.firstscreenfinal); 

    ExpandableHeightListView listMainMenu = (ExpandableHeightListView) 
    findViewById(R.id.listMainMenu11); 
    ExpandableHeightListView Middleschoollist = (ExpandableHeightListView) 
    findViewById(R.id.listMainMenu22); 
    ExpandableHeightListView HighSchoollist = (ExpandableHeightListView) 
    findViewById(R.id.listMainMenu33); 
    ExpandableHeightListView Atipicalschoollist = (ExpandableHeightListView) 
     findViewById(R.id.listMainMenu44); 

    Elementarytxt = (TextView) findViewById(R.id.Elementaryschool); 
    Middletxt = (TextView) findViewById(R.id.MiddleSchool); 
    Hightxt = (TextView) findViewById(R.id.HighSchool); 
    Atypicaltxt = (TextView) findViewById(R.id.AtipicalSchool); 

    mma = new firstscreenadapter(this); 

    if (!Utils.isNetworkAvailable(HomeMenu.this)) { 
     Toast.makeText(HomeMenu.this, "NO NETWORK Available", 
       Toast.LENGTH_SHORT).show(); 
    } 

    if (!Utils.isUserOnline(this)) { 
     Toast.makeText(this, "No NETWORK", Toast.LENGTH_LONG).show(); 
    } 

    url1 = "http://198.57.208.46/~school/ajax.php?action=get_school"; 


    listMainMenu.setAdapter(mma); 
    Middleschoollist.setAdapter(mma); 
    HighSchoollist.setAdapter(mma); 
    Atipicalschoollist.setAdapter(mma); 


    listMainMenu.setExpanded(true); 
    Middleschoollist.setExpanded(true); 
    HighSchoollist.setExpanded(true); 
    Atipicalschoollist.setExpanded(true); 
    Toast.makeText(this, elementarytxtview, Toast.LENGTH_SHORT).show(); 
    listMainMenu.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> arg0, View arg1, 
       int position, long arg3) { 
      // TODO Auto-generated method stub 
      Intent iMenuList = new Intent(HomeMenu.this, 
      SecondStep.class); 
iMenuList.putExtra("category_name",xxx.xxx.xxxx/~school/index.php 
    /api/index/getschools?mg="+ Category_name.get(position)+ "&sl="+elementarytxtview); 

      startActivity(iMenuList); 

     } 
    }); 

    parseJSONData(); 
} 

void clearData() { 
    Category_ID.clear(); 
    Category_name.clear(); 

} 

public void parseJSONData() { 

    SelectMenuAPI = Utils.Homemenu2; 
    SelectMenuAPI2 = Utils.Homemenu; 

    clearData(); 

    try { 

     HttpClient client = new DefaultHttpClient(); 
     HttpConnectionParams 
       .setConnectionTimeout(client.getParams(), 15000); 
     HttpConnectionParams.setSoTimeout(client.getParams(), 15000); 
     HttpUriRequest request = new HttpGet(SelectMenuAPI); 
     HttpResponse response = client.execute(request); 
     InputStream atomInputStream = response.getEntity().getContent(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(
       atomInputStream)); 

     String line; 
     String str = ""; 
     while ((line = in.readLine()) != null) { 
      str += line; 
     } 

     HttpClient client2 = new DefaultHttpClient(); 
     HttpConnectionParams.setConnectionTimeout(client2.getParams(), 
       15000); 
     HttpConnectionParams.setSoTimeout(client2.getParams(), 15000); 
     HttpUriRequest request2 = new HttpGet(SelectMenuAPI2); 
     HttpResponse response2 = client2.execute(request2); 
     InputStream atomInputStream2 = response2.getEntity().getContent(); 
     BufferedReader in2 = new BufferedReader(new InputStreamReader(
       atomInputStream2)); 

     String line2; 
     String str2 = ""; 
     while ((line2 = in2.readLine()) != null) { 
      str2 += line2; 
     } 

     JSONObject json3 = new JSONObject(str2); 
     // message = json2.getString("message"); 
     status = json3.getString("status"); 
     if (status.equals("1")) { 

      JSONArray school2 = json3.getJSONArray("data"); 

      String[] mVal = new String[school2.length()]; 

      for (int i = 0; i < school2.length(); i++) { 
       mVal[i] = 
      school2.getJSONObject(i).getString("title"); 

       Elementarytxt.setText(mVal[0]); 

       Middletxt.setText(mVal[1]); 
       Hightxt.setText(mVal[2]); 
       Atypicaltxt.setText(mVal[3]); 

      } 

      elementarytxtview = mVal[0]; 
     } 

     JSONObject json2 = new JSONObject(str); 
     // message = json2.getString("message"); 
     status = json2.getString("status"); 
     if (status.equals("1")) { 

      // JSONObject data = json.getJSONObject("data"); 

      JSONArray school = json2.getJSONArray("data"); 

      for (int i = 0; i < school.length(); i++) { 
       JSONObject object = school.getJSONObject(i); 

       // 
     Category_ID.add(Long.parseLong(object.getString("id"))); 
       Category_ID.add((long) i); 
       Category_name.add(object.getString("title")); 

      } 

     } else { 

      Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); 
     } 

    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     // IOConnect = 1; 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

     } 
+0

如何從主要方法獲取字符串值到主要方法裏面??? – user2699824

+0

敬酒應該在settext()後面textview。 –

+1

也許,在顯示吐司之前調用'parseJSONData()'?並且_please_學習如何格式化代碼。 – Vikram

回答

0

我不是100%確定你的意思。

但是,如果你想要類之間輕鬆訪問的變量,你應該做的String elementarytxtview;public static

因此:public static String elementarytxtview;

它的意思基本上是在variable是共享的,可所有的類之間使用。只需在您的案例中寫入HomeMenu.elementarytxtview;即可。

+0

我想打印vlaue從出主類的 {} GET 這裏值以下表格主要方法 – user2699824

+0

這並不真正使任何意義。如果你想執行諸如'System.out.println(「」);'來打印/調試信息,你需要將它們放在main或其他方法中。只要將它放在你的方法中就可以調用你想要的信息。 – Vallentin

+0

檢查我更新的代碼,我想通過elementarytextview參數值傳遞給其他活動 – user2699824

1

elementarytxtview沒有初始化你試圖通過展示它之前Toast。起初你需要在其中加入一些價值。

+0

我初始化在頂部String elementarytxtview; – user2699824

+0

@ user2699824您剛剛聲明瞭'elementarytxtview'變量,但未初始化。嘗試在調用parseJSONData()後顯示'Toast'消息。 – Angelo

+1

它不是初始化它只是宣佈。 –

0

您在onCreate方法中顯示的敬酒方法在parseJSONData()方法之前運行。您尚未初始化String elementarytxtview,因此它只會顯示空值,因爲您只聲明變量。