自定义 ListView 未填充 Android 中的项目
Custom ListView not populating with items in Android
我正在使用 JSON 进行解析。单击按钮 JSON 开始解析并填充列表视图。数据来自服务器,但列表视图未填充。代码甚至没有显示任何错误。请检查
public class MainActivity extends AppCompatActivity {
Button start;
ListView listView;
TextView textView;
RequestQueue requestQueue;
String[] deliverName = new String[100];
String[] deliverPrice = new String[100];
int[] deliverImage = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,};
int total;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.button);
//textView = (TextView) findViewById(R.id.textView);
requestQueue = Volley.newRequestQueue(this);
listView = (ListView) findViewById(R.id.listView2);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://deliverit.co.in/test.php?tablename=shakestable",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("products");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject products = jsonArray.getJSONObject(i);
deliverName[i] = products.getString("name");
deliverPrice[i] = products.getString("price");
}
Adapter adapter = new Adapter(getBaseContext(), deliverName, deliverPrice, deliverImage);
listView.setAdapter(adapter);
Log.e("TESTTTT", deliverName[2]);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "ERROR\n" + error.getMessage(), Toast.LENGTH_SHORT).show();
}
}
);
requestQueue.add(jsonObjectRequest);
}
});
}
}
Adapter.java
public class Adapter extends ArrayAdapter<String> {
int[] imgs={};
String[] name={};
String[] price={};
Context c;
LayoutInflater inflater;
public Adapter(Context context, String[] name, String[] price, int[] imgs) {
super(context, R.layout.movies_row_layout);
this.c = context;
this.name = name;
this.price = price;
this.imgs = imgs;
}
public class ViewHolder
{
TextView deliver_name;
TextView deliver_price;
ImageView deliver_image;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.movies_row_layout, null);
}
//View Holder Object
final ViewHolder holder = new ViewHolder();
holder.deliver_name = (TextView) convertView.findViewById(R.id.DELIVERname);
holder.deliver_price = (TextView) convertView.findViewById(R.id.DELIVERprice);
holder.deliver_image = (ImageView) convertView.findViewById(R.id.movie_poster);
//ASSIGN DATA
holder.deliver_image.setImageResource(imgs[position]);
holder.deliver_price.setText(price[position]);
holder.deliver_name.setText(name[position]);
return convertView;
}
}
创建自定义适配器时还需要重写适配器 class 的 getCount()
方法,该方法在 class:
中得到扩展
@Override
public int getCount() {
return name.length; // return data-source size from here
}
实际上,您应该在响应侦听器下方创建队列。
我希望它会起作用。
如果它不起作用,它可能源于您 php 方面。
喜欢
public class MainActivity extends AppCompatActivity {
Button start;
ListView listView;
TextView textView;
RequestQueue requestQueue;
String[] deliverName = new String[100];
String[] deliverPrice = new String[100];
int[] deliverImage = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,};
int total;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.button);
//textView = (TextView) findViewById(R.id.textView);
requestQueue = Volley.newRequestQueue(this);
listView = (ListView) findViewById(R.id.listView2);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://deliverit.co.in/test.php?tablename=shakestable",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("products");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject products = jsonArray.getJSONObject(i);
deliverName[i] = products.getString("name");
deliverPrice[i] = products.getString("price");
}
Adapter adapter = new Adapter(getBaseContext(), deliverName, deliverPrice, deliverImage);
listView.setAdapter(adapter);
Log.e("TESTTTT", deliverName[2]);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "ERROR\n" + error.getMessage(), Toast.LENGTH_SHORT).show();
}
}
);
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonObjectRequest);
}
});
}
}
您从 ArrayAdapter 扩展了适配器,但没有向适配器添加任何项目。此外,您将 3 个不同的数组放入构造函数。我认为,正确的方法是从 BaseAdapter 扩展您的适配器。但是,您可以稍微修改一下构造函数:
public Adapter(Context context, String[] name, String[] price, int[] imgs) {
super(context, R.layout.movies_row_layout,names);
^^^^^^^^^
...
并且您现有的适配器将工作。
我正在使用 JSON 进行解析。单击按钮 JSON 开始解析并填充列表视图。数据来自服务器,但列表视图未填充。代码甚至没有显示任何错误。请检查
public class MainActivity extends AppCompatActivity {
Button start;
ListView listView;
TextView textView;
RequestQueue requestQueue;
String[] deliverName = new String[100];
String[] deliverPrice = new String[100];
int[] deliverImage = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,};
int total;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.button);
//textView = (TextView) findViewById(R.id.textView);
requestQueue = Volley.newRequestQueue(this);
listView = (ListView) findViewById(R.id.listView2);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://deliverit.co.in/test.php?tablename=shakestable",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("products");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject products = jsonArray.getJSONObject(i);
deliverName[i] = products.getString("name");
deliverPrice[i] = products.getString("price");
}
Adapter adapter = new Adapter(getBaseContext(), deliverName, deliverPrice, deliverImage);
listView.setAdapter(adapter);
Log.e("TESTTTT", deliverName[2]);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "ERROR\n" + error.getMessage(), Toast.LENGTH_SHORT).show();
}
}
);
requestQueue.add(jsonObjectRequest);
}
});
}
}
Adapter.java
public class Adapter extends ArrayAdapter<String> {
int[] imgs={};
String[] name={};
String[] price={};
Context c;
LayoutInflater inflater;
public Adapter(Context context, String[] name, String[] price, int[] imgs) {
super(context, R.layout.movies_row_layout);
this.c = context;
this.name = name;
this.price = price;
this.imgs = imgs;
}
public class ViewHolder
{
TextView deliver_name;
TextView deliver_price;
ImageView deliver_image;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.movies_row_layout, null);
}
//View Holder Object
final ViewHolder holder = new ViewHolder();
holder.deliver_name = (TextView) convertView.findViewById(R.id.DELIVERname);
holder.deliver_price = (TextView) convertView.findViewById(R.id.DELIVERprice);
holder.deliver_image = (ImageView) convertView.findViewById(R.id.movie_poster);
//ASSIGN DATA
holder.deliver_image.setImageResource(imgs[position]);
holder.deliver_price.setText(price[position]);
holder.deliver_name.setText(name[position]);
return convertView;
}
}
创建自定义适配器时还需要重写适配器 class 的 getCount()
方法,该方法在 class:
@Override
public int getCount() {
return name.length; // return data-source size from here
}
实际上,您应该在响应侦听器下方创建队列。 我希望它会起作用。 如果它不起作用,它可能源于您 php 方面。
喜欢
public class MainActivity extends AppCompatActivity {
Button start;
ListView listView;
TextView textView;
RequestQueue requestQueue;
String[] deliverName = new String[100];
String[] deliverPrice = new String[100];
int[] deliverImage = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,};
int total;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.button);
//textView = (TextView) findViewById(R.id.textView);
requestQueue = Volley.newRequestQueue(this);
listView = (ListView) findViewById(R.id.listView2);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://deliverit.co.in/test.php?tablename=shakestable",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("products");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject products = jsonArray.getJSONObject(i);
deliverName[i] = products.getString("name");
deliverPrice[i] = products.getString("price");
}
Adapter adapter = new Adapter(getBaseContext(), deliverName, deliverPrice, deliverImage);
listView.setAdapter(adapter);
Log.e("TESTTTT", deliverName[2]);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "ERROR\n" + error.getMessage(), Toast.LENGTH_SHORT).show();
}
}
);
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonObjectRequest);
}
});
}
}
您从 ArrayAdapter 扩展了适配器,但没有向适配器添加任何项目。此外,您将 3 个不同的数组放入构造函数。我认为,正确的方法是从 BaseAdapter 扩展您的适配器。但是,您可以稍微修改一下构造函数:
public Adapter(Context context, String[] name, String[] price, int[] imgs) {
super(context, R.layout.movies_row_layout,names);
^^^^^^^^^
...
并且您现有的适配器将工作。