为什么我的公共变量显示为“'this'is not available”?

时间:2019-05-05 17:20:25

标签: java android

我正在尝试用数据库中的数据填充列表视图,但不允许我分配字符串变量。

我已经阅读了其他文章,但是我一生都无法弄清楚为什么我的变量显示为“'this'is not available”(当我使用调试器时)。

public class InventoryActivity extends AppCompatActivity
{
private RecyclerView varRecyclerView;
private RecyclerView.Adapter varAdapter;
private RecyclerView.LayoutManager varLayoutManager;

private static String URL_FindInventory = "MyPHPFile";

//IM TRYING TO SET THESE TWO VARIABLES
public String itemOneName, itemOneEffect;

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

    String characterID = getIntent().getStringExtra("characterID");

    ArrayList<LayoutItem> inventoryList = new ArrayList<>();

    FindInventory(characterID);

    inventoryList.add(new LayoutItem(R.drawable.ic_add_circle, itemOneName, itemOneEffect));
    inventoryList.add(new LayoutItem(R.drawable.ic_add_circle, "Item Two Name", "Item Two's Effect"));

    varRecyclerView = findViewById(R.id.recyclerView);
    varRecyclerView.setHasFixedSize(true);
    varLayoutManager = new LinearLayoutManager(this);
    varAdapter = new LayoutAdapter(inventoryList);

    varRecyclerView.setLayoutManager(varLayoutManager);
    varRecyclerView.setAdapter(varAdapter);
}


private void FindInventory(final String characterID)
{
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_FindInventory,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response)
                {
                    try
                    {
                        JSONObject jsonObject = new JSONObject(response);

                        String result = jsonObject.getString("result");

                        if (result != null)
                        {
                            JSONArray jsonArray = jsonObject.getJSONArray("result");

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

           //IM TRYING TO USE THESE TWO VARIABLES TO SET THE PUBLIC ONES.
                                String itemName = object.getString("Name").trim(); // this has a value of "Cap of Thinking"
                                String itemEffect = object.getString("Effect").trim(); // this has a value of "Helps the user to think +2 Intelligence"

                                itemOneName = itemName;  // THIS IS SHOWN AS "ItemOneName = 'this' is not available "
                                itemOneEffect = itemEffect; // THIS IS SHOWN AS "ItemOneEffect = 'this' is not available "

                            }

                        }
                        else if ((result.equals("error")))
                        {
                            Toast.makeText(InventoryActivity.this, "Cannot find Inventory", Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e)
                    {
                        e.printStackTrace();
                        Toast.makeText(InventoryActivity.this, "Exception Error " + e.toString(), Toast.LENGTH_LONG).show();

                    }
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(InventoryActivity.this, "Error " + error.toString(), Toast.LENGTH_LONG).show();
                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("characterid", characterID);

            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

}

当我尝试将2个公共字符串的值设置为null时,我一生都无法弄清为什么不允许我在那里为变量设置值从JSON对象读取。

2 个答案:

答案 0 :(得分:1)

它们为空,因为在将项目添加到列表后,您的Web请求就会发生。

设置inventoryList一个字段并删除您要设置的两个字符串字段

将两个inventoryList.add方法移到onResponse中,然后需要通知RecyclerView适配器需要显示新数据

答案 1 :(得分:0)

它们为null的原因是因为当编译器在两行以下执行时(我们将其称为行1和行2):

FindInventory(characterID);//line 1

inventoryList.add(new LayoutItem(R.drawable.ic_add_circle, itemOneName, itemOneEffect));//line 2

-在第1行,该方法被异步执行(这意味着它不会阻塞第2行的执行,第2行将在第1行之后或之前执行)。这会导致变量itemOneName和itemOneEffect为null,因为第2行是在第1行之前执行的,请记住第1行和第2行是在并行或同时执行的。

要解决此问题:

-您必须执行以下操作:

inventoryList.add(new LayoutItem(R.drawable.ic_add_circle, itemOneName, itemOneEffect));
inventoryList.add(new LayoutItem(R.drawable.ic_add_circle, "Item Two Name", "Item Two's Effect"));

...and other dependencies

在onResponse()中调用这些行之后:

String itemName = object.getString("Name").trim(); // this has a value of "Cap of Thinking"
String itemEffect = object.getString("Effect").trim();