首页 > 解决方案 > 为什么我的公共变量显示为“'this' is not available”?

问题描述

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

我已经阅读了其他一些关于此的文章,但我一生都无法弄清楚为什么当我使用调试器时我的变量显示为“'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 对象。

标签: javaandroid

解决方案


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

创建inventoryList一个字段并删除您尝试设置的两个字符串字段

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


推荐阅读