这是我的homepage.xml
- 其中放置了列表视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical"
tools:context="com.andapps.azaz.e_recommender.HomePage">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Category:"
android:textStyle="bold|italic"
android:layout_marginLeft="15sp"
android:layout_marginRight="14sp"
android:textColor="@color/textColor"
/>
<Spinner
android:id="@+id/spcategorieshomepage"
android:layout_width="match_parent"
android:layout_height="35sp"
android:layout_marginRight="14sp"
android:spinnerMode="dropdown"
android:textColor="@color/textColor"
>
</Spinner>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/llhome"
android:gravity="center"
android:layout_marginLeft="15sp"
android:layout_marginRight="14sp">
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
</LinearLayout>
还有我的customlist.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product ID:"
android:textStyle="bold|italic"
android:layout_marginLeft="15sp"
android:layout_marginRight="14sp"
android:textColor="@color/textColor"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold|italic"
android:layout_marginLeft="15sp"
android:layout_marginRight="14sp"
android:textColor="@color/textColor"
android:id="@+id/tvproductid"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product Name:"
android:textStyle="bold|italic"
android:layout_marginLeft="15sp"
android:layout_marginRight="14sp"
android:textColor="@color/textColor"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold|italic"
android:layout_marginLeft="15sp"
android:layout_marginRight="14sp"
android:textColor="@color/textColor"
android:id="@+id/tvproductname"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product Rating:"
android:textStyle="bold|italic"
android:layout_marginLeft="15sp"
android:layout_marginRight="14sp"
android:textColor="@color/textColor"
/>
<RatingBar
android:id="@+id/ratingBar"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15sp"
android:layout_marginRight="14sp"
android:max="5"
android:numStars="5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold|italic"
android:layout_marginLeft="15sp"
android:layout_marginRight="14sp"
android:textColor="@color/textColor"
android:id="@+id/tvproductdesc"
/>
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/imageView"
android:layout_marginLeft="15sp"
android:layout_marginRight="14sp"
/>
</LinearLayout>
我的homepage.java
包含使用AsyncTask
使用REST API并填充数据数组并将其发送到customAdapter.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
lv=(ListView) findViewById(R.id.listView);
Bundle emailbundle=getIntent().getExtras();
email=emailbundle.get("email").toString();
initializer();
addingItemstoSpinner();
}
protected void onPostExecute(String result) {
try {
jsonArray = new JSONArray(result);
} catch (JSONException e) {
e.printStackTrace();
}
len= jsonArray.length();
pdLoading.dismiss();
if (len != 0) {
try {
String [] productIDListArr= new String[len];
String [] productNameListArr= new String[len];
String [] productDescListArr= new String[len];
String [] productImagesArr= new String[len];
String [] productStarsArr= new String[len];
for (int i=0;i<len;i++)
{
objectInArray = jsonArray.getJSONObject(i);
productIDListArr[i]=(objectInArray.getString("productId"));
productNameListArr[i]=(objectInArray.getString("productName"));
productDescListArr[i]=(objectInArray.getString("stars"));
productImagesArr[i]=(objectInArray.getString("productId"));
productStarsArr[i]=(objectInArray.getString("stars")+" out of 5");
if(i==len-1)
{
createdAt=objectInArray.getString("createdAt");
}
}
lv.setAdapter(new CustomAdapter(HomePage.this, productIDListArr,productNameListArr,productDescListArr,productImagesArr,productStarsArr,email));
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Toast.makeText(getApplicationContext(), "Server is down please try again later", Toast.LENGTH_LONG).show();
}
我的customAdapter.java
package com.andapps.azaz.e_recommender;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
/**
* Created by M.Azaz on 30-Mar-17.
*/
public class CustomAdapter extends BaseAdapter {
String [] productIDListArr,productNameListArr,productDescListArr,productImagesArr,productStarsArr;
Context context;
String email;
private static LayoutInflater inflater=null;
public CustomAdapter(HomePage homePage, String[] productIDList, String[] productNameList, String[] productDescList, String[] productImages, String[] productStars,String emailMain) {
// TODO Auto-generated constructor stub
productIDListArr=productIDList;
productNameListArr=productNameList;
productDescListArr=productDescList;
productImagesArr=productImages;
productStarsArr=productStars;
context=homePage;
email=emailMain;
inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return productIDListArr.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return productIDListArr[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tvproductname, tvproductid, tvproductdesc;
ImageView imageView;
RatingBar ratingBar;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.activity_custom_list, null);
holder.tvproductname=(TextView) rowView.findViewById(R.id.tvproductname);
holder.tvproductid=(TextView) rowView.findViewById(R.id.tvproductid);
holder.tvproductdesc=(TextView) rowView.findViewById(R.id.tvproductdesc);
holder.imageView=(ImageView) rowView.findViewById(R.id.imageView);
holder.ratingBar=(RatingBar) rowView.findViewById(R.id.ratingBar);
holder.tvproductname.setText(productNameListArr[position]);
holder.tvproductid.setText(productIDListArr[position]);
holder.tvproductdesc.setText(productDescListArr[position]);
holder.ratingBar.setRating(Integer.parseInt(productStarsArr[position]));
Picasso.with(context).load(context.getString(R.string.imageip) + productImagesArr[position]+".jpg").into(holder.imageView);
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+productIDListArr[position], Toast.LENGTH_LONG).show();
Intent intent = new Intent("com.andapps.azaz.e_recommender.RATINGS");
intent.putExtra("tag",productIDListArr[position]);
intent.putExtra("email", email);
context.startActivity(intent);
}
});
return rowView;
}
}
但是当我运行代码时,它实际上会调用customAdapter.java
,但是在调用getCount()
customAdapter
之后它就没有调用getView
并且应用程序终止。
还有一件事......如何将更多项目更新到现有列表?
答案 0 :(得分:0)
当productIDListArr为null时,您的app可能会在getCount方法中崩溃。因此,在返回count之前,在getCount方法中添加if条件。
例如:
public int getCount(){
if(productIDListArr==null){
return 0;
}else{
return productIDListArr.length;
}
}
提示:创建一个用于保存响应数据的类,并将该模型类的列表传递给适配器,并且每次数据更新时都会更改该列表并调用适配器的notify方法。
例如:
Class Item{
public String productIDListArr;
public String productNameListArr;
public String productDescListArr;
public String productImagesArr;
public String productStarsArr;
}
ArrayList<Item> items=new ArrayList<Item>();
for (int i=0;i<len;i++)
{
objectInArray = jsonArray.getJSONObject(i);
Item item=new Item();
item.productIDListArr=(objectInArray.getString("productId"));
.......
}
CustomAdapter extend BaseAdapter{
ArrayList<Item> items;
public CustomAdapter(ArrayList<Item> items){
this.items=items;
}
public int getCount(){
return items==null?0:items.size()
}