我的自定义列表视图没有显示任何数据,我尝试重新编码适配器和其他所有内容但没有骰子。
它甚至没有显示任何错误,这就是让我疯狂的原因哈哈。任何人都可以帮我调试我的代码。
这是我的活动:
private ListView listView;
private ArrayList<String> orderName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_orders_page);
orderName = new ArrayList<>();
ListView listView = (ListView) findViewById(R.id.listViewOrder);
viewOrderAdapter adapter = new viewOrderAdapter(viewOrdersPage.this, orderName);
listView.setAdapter(adapter);
getBoxInformation();
}
private void getBoxInformation() {
final ProgressDialog loading = ProgressDialog.show(this, "GETTING BOX INFROMATION", "Processing Request...", false, false);
String url = DATA_URL + textUsername.getText().toString().trim();
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
showJSON(response);
loading.dismiss();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(viewOrdersPage.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response) {
String name = "";
try {
jsonObject = new JSONObject(response);
result = jsonObject.getJSONArray(JSON_ARRAY);
order = result.getJSONObject(0);
name = order.getString(TAG_ORDERNAME);
JSONArray jsonarray = new JSONArray(name);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
names = jsonobject.getString("name");
//THE TOAST DISPLAYS THE TEXT SO I KNOW THE CODE WORKS, EXCEPT THE LISTVIEW
Toast.makeText(viewOrdersPage.this, names, Toast.LENGTH_LONG).show();
orderName.add(names);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
这是Adapter类:
public class viewOrderAdapter extends BaseAdapter {
private Context context;
private ArrayList<String> orderName;
public viewOrderAdapter(Context context, ArrayList<String> orderName){
this.context = context;
this.orderName = orderName;
}
@Override
public int getCount() {
return orderName.size();
}
@Override
public Object getItem(int position) {
return orderName.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final View listView;
final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listView = inflater.inflate(R.layout.view_orders_item, null);
TextView name = (TextView) listView.findViewById(R.id.orderNames);
name.setText(orderName.get(position));
return listView;
}
}
提前感谢您的帮助!
答案 0 :(得分:2)
问题:网络请求需要一些时间,但是当列表中没有数据时您正在创建适配器,因此您需要告诉适配器数据发生了变化
将onCreate
引用移到private ArrayList<String> orderName;
private viewOrderAdapter adapter ; // adapter reference
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_orders_page);
orderName = new ArrayList<>();
ListView listView = (ListView) findViewById(R.id.listViewOrder);
adapter = new viewOrderAdapter(viewOrdersPage.this, orderName);
listView.setAdapter(adapter);
getBoxInformation();
}
adapter.notifyDataSetChanged()
然后在showJSON
函数
adapter
或
您可以在listview
功能结束时showJSON
上创建并设置 <!DOCTYPE html>
<html>
<body>
<h2>JFinding all even numbers</h2>
<p>Please input a starting number, ending number and step number:</p>
Starting Number: <input id="start">
Ending Number: <input id="end">
Step by Number: <input id="step">
<button type="button" onclick="myFunction()">Submit</button>
<p id="demo"></p>
<script>
function myFunction() {
var start, end, step, text;
var result = [];
// Get the value of the start number with start var
start = parseInt(document.getElementById("start").value);
// Get the value of the end number with end var
end = parseInt(document.getElementById("end").value);
// Get the value of the step number with step var
step = parseInt(document.getElementById("step").value);
console.log('start', start);
console.log('end', end);
console.log('step', step);
// If start, end, or step is Not a Number or step is not even or start is greater than end or step is greater than end or start and step are equal to end
if (isNaN(start) || isNaN(end) || isNaN(step) || step % 2 !== 0 || start > end || step > end || step === end || start === end) {
//display text for invalid input
text = "Input not valid";
//display text to html page
document.getElementById("demo").innerHTML = text;
//else create a loop to find even numbers
} else {
if(start % 2 === 0){
for(var i = start; i <= end; i += step){
result.push(i);
}
} else {
start += 1;
for(var i = start; i <= end; i += step){
result.push(i);
}
}
//sent the result to the html page
document.getElementById("demo").innerHTML = result;
}
console.log(result);
}
</script>
</body>
</html>
答案 1 :(得分:1)
这是活动:
private ListView listView;
private ArrayList<String> orderName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_orders_page);
orderName = new ArrayList<>();
ListView listView = (ListView) findViewById(R.id.listViewOrder);
getBoxInformation();
}
private void setDataInList(){
viewOrderAdapter adapter = new viewOrderAdapter(viewOrdersPage.this, orderName);
listView.setAdapter(adapter);
}
private void getBoxInformation() {
final ProgressDialog loading = ProgressDialog.show(this, "GETTING BOX INFROMATION", "Processing Request...", false, false);
String url = DATA_URL + textUsername.getText().toString().trim();
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
showJSON(response);
loading.dismiss();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(viewOrdersPage.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response) {
String name = "";
try {
jsonObject = new JSONObject(response);
result = jsonObject.getJSONArray(JSON_ARRAY);
order = result.getJSONObject(0);
name = order.getString(TAG_ORDERNAME);
JSONArray jsonarray = new JSONArray(name);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
names = jsonobject.getString("name");
//THE TOAST DISPLAYS THE TEXT SO I KNOW THE CODE WORKS, EXCEPT THE LISTVIEW
Toast.makeText(viewOrdersPage.this, names, Toast.LENGTH_LONG).show();
orderName.add(names);
}
if(orderName.size()>0){
setDataInList();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
这是适配器类:
public class viewOrderAdapter extends BaseAdapter {
private Context context;
private ArrayList<String> orderName;
public viewOrderAdapter(Context context, ArrayList<String> orderName){
this.context = context;
this.orderName = orderName;
}
@Override
public int getCount() {
return orderName.size();
}
@Override
public Object getItem(int position) {
return orderName.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View listView;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listView = inflater.inflate(R.layout.view_orders_item, null);
TextView name = (TextView) listView.findViewById(R.id.orderNames);
name.setText(orderName.get(position));
return listView;
}