我在公共类中声明了字符串st1,st2,st3..st7。我在受保护的onCreate方法中使用JSON从MySQL数据库中获取价值。现在,我想将其传递给字符串st1,st2,st3..st7,以便将其传递给在公共类中声明的字符串seat。因此,我可以在下面的onCreate方法中使用它以作进一步使用。
public class SeatBookingTwo extends AppCompatActivity implements View.OnClickListener {
ViewGroup layout;
private Context mContext;
private Activity mActivity;
private String mJSONURLString = "http://paolosfa......";
String st1 ="", st2="" , st3="" ,st4="" ,st5="" , st6="" ,st7="" ;
String seats = st1 + "" + st2 + "" + st3 +"" + '/' + "" + st4 + "" + st5 + "" + st6 + "" + st7;
List<TextView> seatViewList = new ArrayList<>();
int seatSize = 100;
int seatGaping = 10;
int STATUS_AVAILABLE = 1;
int STATUS_BOOKED = 2;
int STATUS_RESERVED = 3;
String selectedIds = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seat_booking_two);
layout = findViewById(R.id.layoutSeat);
seats = "/" + seats;
mContext = getApplicationContext();
mActivity = SeatBookingTwo.this;
RequestQueue requestQueue = Volley.newRequestQueue(mContext);
// Initialize a new JsonArrayRequest instance
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
Request.Method.POST,
mJSONURLString,
null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray seat) {
// Do something with response
//mTextView.setText(response.toString());
// Process the JSON
try{
// Loop through the array elements
for(int i=0;i<seat.length();i++){
// Get current json object
JSONObject student = seat.getJSONObject(i);
// Get the current student (json object) data
st1 = student.getString("st1");
st2 = student.getString("st2");
st3 = student.getString("st3");
st4 = student.getString("st4");
st5 = student.getString("st5");
st6 = student.getString("st6");
st7 = student.getString("st7");
}
}catch (JSONException e){
e.printStackTrace();
}
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
}
}
);
// Add JsonArrayRequest to the RequestQueue
requestQueue.add(jsonArrayRequest);
LinearLayout layoutSeat = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutSeat.setOrientation(LinearLayout.VERTICAL);
layoutSeat.setLayoutParams(params);
layoutSeat.setPadding(8 * seatGaping, 8 * seatGaping, 8 * seatGaping, 8 * seatGaping);
layout.addView(layoutSeat);
LinearLayout layout = null;
int count = 0;
for (int index = 0; index < seats.length(); index++) {
if (seats.charAt(index) == '/') {
layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
layoutSeat.addView(layout);
} else if (seats.charAt(index) == '1') {
count++;
TextView view = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(seatSize, seatSize);
layoutParams.setMargins(seatGaping, seatGaping, seatGaping, seatGaping);
view.setLayoutParams(layoutParams);
view.setPadding(0, 0, 0, 2 * seatGaping);
view.setId(count);
view.setGravity(Gravity.CENTER);
view.setBackgroundResource(R.drawable.ic_seats_booked);
view.setTextColor(Color.WHITE);
view.setTag(STATUS_BOOKED);
view.setText(count + "");
view.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 9);
layout.addView(view);
seatViewList.add(view);
view.setOnClickListener(this);
} else if (seats.charAt(index) == '2') {
count++;
TextView view = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(seatSize, seatSize);
layoutParams.setMargins(seatGaping, seatGaping, seatGaping, seatGaping);
view.setLayoutParams(layoutParams);
view.setPadding(0, 0, 0, 2 * seatGaping);
view.setId(count);
view.setGravity(Gravity.CENTER);
view.setBackgroundResource(R.drawable.ic_seats_book);
view.setText(count + "");
view.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 9);
view.setTextColor(Color.BLACK);
view.setTag(STATUS_AVAILABLE);
layout.addView(view);
seatViewList.add(view);
view.setOnClickListener(this);
} else if (seats.charAt(index) == '3') {
count++;
TextView view = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(seatSize, seatSize);
layoutParams.setMargins(seatGaping, seatGaping, seatGaping, seatGaping);
view.setLayoutParams(layoutParams);
view.setPadding(0, 0, 0, 2 * seatGaping);
view.setId(count);
view.setGravity(Gravity.CENTER);
view.setBackgroundResource(R.drawable.ic_seats_reserved);
view.setText(count + "");
view.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 9);
view.setTextColor(Color.WHITE);
view.setTag(STATUS_RESERVED);
layout.addView(view);
seatViewList.add(view);
view.setOnClickListener(this);
} else if (seats.charAt(index) == '_') {
TextView view = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(seatSize, seatSize);
layoutParams.setMargins(seatGaping, seatGaping, seatGaping, seatGaping);
view.setLayoutParams(layoutParams);
view.setBackgroundColor(Color.TRANSPARENT);
view.setText("");
layout.addView(view);
}
}
}
@Override
public void onClick(View view) {
if ((int) view.getTag() == STATUS_AVAILABLE) {
if (selectedIds.contains(view.getId() + ",")) {
selectedIds = selectedIds.replace(+view.getId() + ",", "");
view.setBackgroundResource(R.drawable.ic_seats_book);
} else {
selectedIds = selectedIds + view.getId() + ",";
view.setBackgroundResource(R.drawable.ic_seats_selected);
}
} else if ((int) view.getTag() == STATUS_BOOKED) {
Toast.makeText(this, "Seat " + view.getId() + " is Booked", Toast.LENGTH_SHORT).show();
} else if ((int) view.getTag() == STATUS_RESERVED) {
Toast.makeText(this, "Seat " + view.getId() + " is Reserved", Toast.LENGTH_SHORT).show();
}
}
}
最佳答案
我的建议是不要创建String seats = st1 + "" + st2 + "" + st3 +"" + '/' + "" + st4 + "" + st5 + "" + st6 + "" + st7;
只是声明
String seats = "";
然后在onResponse do
seats = st1 + "" + st2 + "" + st3 +"" + '/' + "" + st4 + "" + st5 + "" + st6 + "" + st7;
我会做上面的事情,并在需要的任何地方使用它,并且还记得字符串是不可变的