2017-07-02 65 views
0

我的适配器未设置为列表视图。 logcat中没有错误,并且列表中有数据。适配器未设置为列表视图android

 lv = (ListView) vi.findViewById(R.id.list); 


     // Get a reference to our posts 
     final FirebaseDatabase database = FirebaseDatabase.getInstance(); 
     DatabaseReference ref = database.getReference().child("chatusers"); 
     urlList = new ArrayList<List>(); 

     final ProgressDialog Dialog = new ProgressDialog(getActivity()); 
     Dialog.setMessage("Please wait..."); 
     Dialog.show(); 
// Attach a listener to read the data at our posts reference 
     ref.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       String chatwithname, foodname, foodimage; 
       for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) { 


        Log.d("chatwithname", messageSnapshot.child("chatwithname").getValue().toString()); 
        chatwithname = messageSnapshot.child("chatwithname").getValue().toString(); 
        foodname = messageSnapshot.child("foodname").getValue().toString(); 

        foodimage = messageSnapshot.child("foodimage").getValue().toString(); 

        Log.d("chatusers", chatwithname + "" + foodname + "" + foodimage); 
        List list1 = Arrays.asList(chatwithname, foodname, foodimage); 
        urlList.add(list1); 
        Log.d("urlList", urlList.toString()); 
       } 
       chatusersAdapter chat = new chatusersAdapter(
         getActivity(),urlList); 

       lv.setAdapter(chat); 
       Dialog.hide(); 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 
       System.out.println("The read failed: " + databaseError.getCode()); 
      } 
     }); 

我的适配器类

public class chatusersAdapter extends ArrayAdapter<List> { 
    private final Context context; 
    List<List> DialogList = new ArrayList<List>(); 


    public chatusersAdapter(Context context, List<List> values) { 
     super(context, R.layout.chatusers_list, values); 
     this.context = context; 
     this.DialogList = values; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     Log.d("getview","getview"); 

     final int i=position; 

     List dialog = DialogList.get(i); 
     Log.d("chatwithusername", dialog.get(0).toString()); 

     ViewHolder holder; 
     ImageView imageViews; 
     if (convertView == null) { 
      LayoutInflater layoutInflator = LayoutInflater.from(getContext()); 
      convertView = layoutInflator.inflate(R.layout.chatusers_list, null); 
      holder = new ViewHolder(); 

      holder.chat_with_user = (TextView) convertView.findViewById(R.id.chat_with_user); 
      holder.item_name = (TextView) convertView.findViewById(R.id.item_name); 

      holder.img_product = (ImageView) convertView.findViewById(R.id.img_product); 


      convertView.setTag(holder); 
     } 

     holder = (ViewHolder) convertView.getTag(); 
     holder.chat_with_user.setText(dialog.get(0).toString()); 
     holder.item_name.setText(dialog.get(1).toString()); 
     Glide.with(context).load(dialog.get(2).toString()).into(holder.img_product); 

     return convertView; 
    } 
    @Override 
    public int getCount() { 
     Log.d("asifa",String.valueOf(DialogList.size())); 
     return DialogList.size(); 
    } 
    static class ViewHolder { 

     ImageView img_product; 
     TextView item_name; 
     TextView chat_with_user; 
    } 


} 

清单显示在日志中,有数据列表,但它不是设置为列表视图。问题出在这里

`chatusersAdapter chat = new chatusersAdapter(getActivity(),urlList); lv.setAdapter(chat);` 

该适配器未被调用。 有人可以帮忙吗?

+0

你应该尝试设置一些断点,以检查是否真的被称为onDataChange,它它进入for循环,如果urlList实际上是被与填充数据,如果你的适配器里面调用了getView。这就是我要做的。 – Tharkius

+0

已填充urlList。但getView不被调用。 – asifa

回答

0

你应该实现这样的适配器的getCount()方法:

@Override 
public int getCount() { 
    return DialogList.size(); 
} 
+0

即使在添加getCount方法之后,它也不工作。这里的问题是chatusersAdapter chat = new chatusersAdapter( getActivity(),urlList); lv.setAdapter(chat); – asifa