自定义适配器不适用于Parse.com的列表视图

时间:2015-08-09 12:04:24

标签: java android listview parse-platform custom-adapter

我正在尝试使用Parse.com中的数据库创建一个Android应用程序。我正在使用自定义适配器来创建列表视图。我没有发现代码有任何错误,但是没有显示listeview。 logcat中也没有任何东西。只是列表视图没有显示出来。

    lv = (ListView)findViewById(R.id.listView);
    mProgress = (ProgressBar)findViewById(R.id.check_progress);
    mProgress.setVisibility(View.VISIBLE);

    ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Sellers");
    query.orderByAscending("Name");
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> parseObjects, ParseException e) {
            if (e == null) {
                studentsList = new ArrayList<Sellers>();
                for (ParseObject ob : parseObjects) {
                    s = new Sellers();

                    s.setName(ob.getString("Name").toString());
                    s.setAddress1(ob.getString("Address1").toString());
                    s.setAddress2(ob.getString("Address2").toString());
                    s.setShopName(ob.getString("ShopName").toString());
                    s.setEmail(ob.getString("Email").toString());
                    s.setPhone(ob.getString("Phone").toString());
                    s.setZipcode(ob.getString("Zipcode").toString());

                    studentsList.add(s);
                }
                adapter = new ListviewAdapter(CheckStatus.this, studentsList);
                lv.setAdapter(adapter);
                mProgress.setVisibility(View.GONE);


            } else {
                mProgress.setVisibility(View.GONE);
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }

        }
    });

这是我调用listview的活动。

public class ListviewAdapter extends BaseAdapter{

private final static String TAG = ListviewAdapter.class.getSimpleName();

private Context activity;
private LayoutInflater inflater = null;
private List<Sellers> sellers;
int layout;

public ListviewAdapter(Context activity, List<Sellers> sellers) {


    this.activity = activity;
    this.sellers = sellers;
    inflater = LayoutInflater.from(activity);
}

@Override
public int getCount() {
    return 0;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

public  class ViewHolder {
    TextView name  ;
    TextView shop ;
    TextView address1 ;
    TextView address2;
    TextView phone;
    TextView email;
    RelativeLayout rl;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    View v =view;
    ViewHolder holder = new ViewHolder();

    if (view == null) {
        LayoutInflater li = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.list_item_layout,null);
        holder.name = (TextView)v.findViewById(R.id.seller_name);
        holder.shop = (TextView)v.findViewById(R.id.shop_name);
        holder.address1 = (TextView)v.findViewById(R.id.address1);
        holder.address2 = (TextView)v.findViewById(R.id.address2);
        holder.phone = (TextView)v.findViewById(R.id.phone);
        holder.email = (TextView)v.findViewById(R.id.emailID);
        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }

    Sellers s = sellers.get(position);


   // String a =  s.Name;
   // Log.d(TAG, a);
    holder.name.setText(s.getName());
    holder.shop.setText(s.getShopName());
    holder.address1.setText(s.getAddress1());
    holder.address2.setText(s.getAddress2());
    holder.phone.setText(s.getPhone());
    holder.email.setText(s.getEmail());

    Log.d("CustomAdapter.class", "CustomAdapter");

    // imageView.setImageDrawable(s.getPic());
    return v;
 }
}

这是自定义适配器。在logcat中没有出现空指针异常。我无法确定列表视图未填充的原因。

2 个答案:

答案 0 :(得分:2)

试试这个;

@Override
public int getCount() {
return sellers.size();
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position:
}

答案 1 :(得分:0)

您必须在getCount()上实现代码,否则将创建项目列表视图的返回编号。