gpt4 book ai didi

java - 为什么我的公共(public)变量显示为 "' 这'不可用”?

转载 作者:行者123 更新时间:2023-12-02 09:56:10 25 4
gpt4 key购买 nike

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

我已经阅读了一些关于此的其他文章,但我一生都无法弄清楚为什么当我使用调试器时我的变量显示为“'this'不可用”。

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

}

当我尝试将两个公共(public)字符串的值设置为空时,我一生都无法弄清楚为什么它不允许我为变量设置值从 JSON 对象读取。

最佳答案

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

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

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

关于java - 为什么我的公共(public)变量显示为 "' 这'不可用”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55994514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com