无法在 android studio 中设置字符串值

吉什万斯 Vs

这是头等舱

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);
        }

    });

}

}

从我的第二类我将值传递给第一类,但值是在字符串 URL_PRODUCTS 执行之后传递的,但我想在字符串 URL_PRODUCTS 执行之前接收值。如果我添加

Intent intent = getIntent();
name = intent.getStringExtra("name");
tokenno = intent.getStringExtra("tokenno"); 

在私有字符串 URL_PRODUCTS 出现错误之前,有没有办法将 name 和 tokenno 添加到 String URL_PRODUCTS

阿克谢·潘查尔

试试这个方法

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 在您的代码中,您正在从 Intent 中获取值,onCreate()但是您在onCreate()调用之前分配了它们,这是错误的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法在AsyncTask android中的onPostExcecute中将值设置为字符串

来自分类Dev

Android Studio无法识别字符串

来自分类Dev

Android Studio“无法解析符号字符串”

来自分类Dev

Android Studio无法识别字符串

来自分类Dev

无法从函数 Kotlin/Android Studio 返回字符串

来自分类Dev

突然之间,我无法在Android Studio中更改任何字符串

来自分类Dev

Android Studio错误:setText中的字符串文字无法翻译

来自分类常见问题

无法插入Android Studio的值

来自分类Dev

Android Studio 中的字符串等于

来自分类Dev

无法在Android Studio中设置Android SDK路径

来自分类Dev

如何在Kotlin Android Studio中动态设置包含字符串资源ID的变量?

来自分类Dev

Android Studio无法设置特定的语言环境

来自分类Dev

Android Studio 1.1.0无法设置Robolectric

来自分类Dev

Android Studio“无法保存插件设置”

来自分类Dev

无法从Screen Generator Android Studio导出设置

来自分类Dev

Android Studio无法正确设置新项目

来自分类Dev

Android Studio无法设置特定的语言环境

来自分类Dev

无法在Android Studio中创建AVD

来自分类Dev

Android Studio中的OpenCV无法正常工作?

来自分类Dev

无法在Android Studio中升级Gradle版本

来自分类Dev

无法在Linux中启动Android Studio

来自分类Dev

无法在Android Studio中添加依赖项

来自分类Dev

无法在Android Studio中添加活动

来自分类Dev

无法使getLastLocation方法在Android Studio中工作

来自分类Dev

Android Studio预览中的“无法解析资源”

来自分类Dev

无法在Android Studio中添加外部库?

来自分类Dev

无法在Android Studio中打开或开发应用

来自分类Dev

无法解析Android Studio中的库

来自分类Dev

无法让Robotium在Android Studio中工作

Related 相关文章

热门标签

归档