如何访问值超出主类的?我想打印elementarytxtview价值 在主类的参数,但在参数我想在参数传递elementarytextview价值的其他活动打印空值
公共类HomeMenu延伸活动{
的ImageButton imgNews,imgContact,imgSetting;
ListView控件listMainMenu;
ListView的Middleschoollist,HighSchoollist,Atipicalschoollist;
字符串状态;
firstscreenadapter MMA;
字符串SelectMenuAPI;
字符串SelectMenuAPI2;
字符串URL1;
字符串初等;
//串高;
字符串消息;
TextView的Elementarytxt,Middletxt,Hightxt,Atypicaltxt;
静态的ArrayList<龙> CATEGORY_ID =新的ArrayList<龙>();
静态的ArrayList<字符串> CATEGORY_NAME =新的ArrayList<字符串>();
字符串elementarytxtview;
@覆盖
保护无效的onCreate(包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 =新firstscreenadapter(本);
如果(!Utils.isNetworkAvailable(HomeMenu.this)){
Toast.makeText(HomeMenu.this,无网络可用,
Toast.LENGTH_SHORT).show();
}
如果(!Utils.isUserOnline(本)){
Toast.makeText(这一点,无网络,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(真正的);
Middleschoollist.setExpanded(真正的);
HighSchoollist.setExpanded(真正的);
Atipicalschoollist.setExpanded(真正的);
Toast.makeText(这一点,elementarytxtview,Toast.LENGTH_SHORT).show();
listMainMenu.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图<>为arg0,查看ARG1,
INT位置,长ARG3){
// TODO自动生成方法存根
意图iMenuList =新的意图(HomeMenu.this,
SecondStep.class);
iMenuList.putExtra(CATEGORY_NAME,xxx.xxx.xxxx /〜学校/ index.php文件
/ API /指数/ getschools毫克=+ Category_name.get(位置)+&放大器; SL =+ elementarytxtview);
startActivity(iMenuList);
}
});
parseJSONData();
}
无效clearData(){
Category_ID.clear();
Category_name.clear();
}
公共无效parseJSONData(){
SelectMenuAPI = Utils.Homemenu2;
SelectMenuAPI2 = Utils.Homemenu;
clearData();
尝试 {
HttpClient的客户端=新DefaultHttpClient();
HttpConnectionParams
.setConnectionTimeout(client.getParams(),15000);
HttpConnectionParams.setSoTimeout(client.getParams(),15000);
HttpUriRequest请求=新HTTPGET(SelectMenuAPI);
HTT presponse响应= client.execute(要求);
InputStream的atomInputStream = response.getEntity()的getContent()。
的BufferedReader在=新的BufferedReader(新的InputStreamReader(
atomInputStream));
串线;
字符串str =;
而((行= in.readLine())!= NULL){
STR + =行;
}
HttpClient的客户端2 =新DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client2.getParams(),
15000);
HttpConnectionParams.setSoTimeout(client2.getParams(),15000);
HttpUriRequest请求2 =新HTTPGET(SelectMenuAPI2);
HTT presponse响应2 = client2.execute(请求2);
InputStream的atomInputStream2 = response2.getEntity()的getContent()。
的BufferedReader IN2 =新的BufferedReader(新的InputStreamReader(
atomInputStream2));
字符串2号线;
字符串str2的=;
而((2号线= in2.readLine())!= NULL){
STR2 + = 2号线;
}
JSONObject的json3 =新的JSONObject(STR2);
//消息= json2.getString(信息);
状态= json3.getString(状态);
如果(status.equals(1)){
JSONArray school2 = json3.getJSONArray(数据);
的String [] MVAL =新的String [school2.length()];
的for(int i = 0; I< school2.length();我++){
MVAL [I] =
school2.getJSONObject(我).getString(标题);
Elementarytxt.setText(MVAL [0]);
Middletxt.setText(MVAL [1]);
Hightxt.setText(MVAL [2]);
Atypicaltxt.setText(MVAL [3]);
}
elementarytxtview = MVAL [0];
}
JSONObject的json2 =新的JSONObject(STR);
//消息= json2.getString(信息);
状态= json2.getString(状态);
如果(status.equals(1)){
// JSONObject的数据= json.getJSONObject(数据);
JSONArray学校= json2.getJSONArray(数据);
的for(int i = 0; I< school.length();我++){
的JSONObject对象= school.getJSONObject(我);
//
Category_ID.add(的Long.parseLong(object.getString(ID)));
Category_ID.add((长)一);
Category_name.add(object.getString(标题));
}
} 其他 {
Toast.makeText(此,信息,Toast.LENGTH_SHORT).show();
}
}赶上(MalformedURLException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
// IOConnect = 1;
e.printStackTrace();
}赶上(JSONException E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
}
解决方案
我不是100%肯定你的意思。
但是,如果你想类之间方便地访问变量,你应该做的字符串elementarytxtview;
公共静态
因此:公共静态字符串elementarytxtview;
这也就意味着,基本上是在变量
是共享的,可以各阶层之间使用。只需通过编写 HomeMenu.elementarytxtview;你的情况
how to access value which is out of main class? i want to print value of elementarytxtview in main class in parameter but is print null value of in parameter i want to pass elementarytextview value in parameter to other activity
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();
}
}
}
解决方案
I'm not a 100% sure of what you mean.
But if you want to access variables easily between classes, you should make the String elementarytxtview;
public static
Thereby: public static String elementarytxtview;
What that means is basically that the variable
is shared and can be used between all classes. Just by writing HomeMenu.elementarytxtview;
in your case.