2015-08-09 171 views
1

我想創建一個使用Parse.com數據庫的Android應用程序。我正在使用自定義適配器來創建一個列表視圖。我沒有發現代碼有任何錯誤,但是這個listeview沒有顯示出來。 logcat中也沒有任何內容。只是列表視圖不顯示。自定義適配器不工作從Parse.com列表視圖

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中沒有顯示空指針異常。我無法確定爲什麼listview沒有被填充。

回答

2

試試這個;

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

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

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

你必須在getCount()方法上實現你的代碼,因爲item listview的返回數將被創建。

相關問題