这是头等舱
package com.example.jishwanth.three.students_file;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.EditText;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.jishwanth.three.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class Students_list extends AppCompatActivity {
//this is the JSON Data URL
//make sure you are using the correct ip else it will not work
String name;
String tokenno;
private final String URL_PRODUCTS = "http://10.42.0.1/PHP/student_view.php?first_name="+name+"&token_no="+tokenno;
//a list to store all the products
List<Product> productList;
//the recyclerview
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.student_in);
Intent intent = getIntent();
name = intent.getStringExtra("name");
tokenno = intent.getStringExtra("tokenno");
//getting the recyclerview from xml
recyclerView = findViewById(R.id.recylcerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//initializing the productlist
productList = new ArrayList<>();
//this method will fetch and parse json
//to display it in recyclerview
loadProducts();
}
private void loadProducts() {
/*
* Creating a String Request
* The request type is GET defined by first parameter
* The URL is defined in the second parameter
* Then we have a Response Listener and a Error Listener
* In response listener we will get the JSON response as a String
* */
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
//converting the string to json array object
JSONArray array = new JSONArray(response);
//traversing through all the object
for (int i = 0; i < array.length(); i++) {
//getting product object from json array
JSONObject product = array.getJSONObject(i);
//adding the product to product list
productList.add(new Product(
product.getString("first_name"),
product.getString("token_no"),
product.getString("entered_out_time"),
product.getString("approved_from"),
product.getString("reason"),
product.getString("image"),
product.getString("entered_in_time"),
product.getString("s_date")
));
}
//creating adapter object and setting it to recyclerview
v_ProductsAdapter adapter = new v_ProductsAdapter(Students_list.this, productList);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}
}
这是第二类
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.jishwanth.three.R;
import com.example.jishwanth.three.students_file.Students_list;
public class Student_view extends AppCompatActivity {
Button button;
EditText editText1,editText2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.student_view);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button6);
editText1=findViewById(R.id.editText8);
editText2=findViewById(R.id.editText9);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Students_list.class);
intent.putExtra("name", editText1.getText().toString());
intent.putExtra("tokenno", editText2.getText().toString());
startActivity(intent);
}
});
}
}
从我的第二个类iam传递值到第一个类,但值在String URL_PRODUCTS执行后传递,但我想在字符串URL_PRODUCTS执行之前接收值。如果我添加
Intent intent = getIntent();
name = intent.getStringExtra("name");
tokenno = intent.getStringExtra("tokenno");
这个私有字符串之前URL_PRODUCTS我得到错误是否有任何方法可以将名称和tokenno添加到字符串URL_PRODUCTS
答案 0 :(得分:0)
试试这种方式
package com.example.jishwanth.three.students_file;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.EditText;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.jishwanth.three.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class Students_list extends AppCompatActivity {
//this is the JSON Data URL
//make sure you are using the correct ip else it will not work
String name;
String tokenno;
private String URL_PRODUCTS;
//a list to store all the products
List<Product> productList;
//the recyclerview
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.student_in);
Intent intent = getIntent();
name = intent.getStringExtra("name");
tokenno = intent.getStringExtra("tokenno");
URL_PRODUCTS = "http://10.42.0.1/PHP/student_view.php?first_name="+name+"&token_no="+tokenno;
//getting the recyclerview from xml
recyclerView = findViewById(R.id.recylcerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//initializing the productlist
productList = new ArrayList<>();
//this method will fetch and parse json
//to display it in recyclerview
loadProducts();
}
private void loadProducts() {
/*
* Creating a String Request
* The request type is GET defined by first parameter
* The URL is defined in the second parameter
* Then we have a Response Listener and a Error Listener
* In response listener we will get the JSON response as a String
* */
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
//converting the string to json array object
JSONArray array = new JSONArray(response);
//traversing through all the object
for (int i = 0; i < array.length(); i++) {
//getting product object from json array
JSONObject product = array.getJSONObject(i);
//adding the product to product list
productList.add(new Product(
product.getString("first_name"),
product.getString("token_no"),
product.getString("entered_out_time"),
product.getString("approved_from"),
product.getString("reason"),
product.getString("image"),
product.getString("entered_in_time"),
product.getString("s_date")
));
}
//creating adapter object and setting it to recyclerview
v_ProductsAdapter adapter = new v_ProductsAdapter(Students_list.this, productList);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}
}
在onCreate()
方法中创建您的URL_PRODUCTS。
在您的代码中,您从onCreate()
中的intent中获取值,但是在调用onCreate()
之前您正在分配它们,这是错误的。