Listview什么都没显示

时间:2016-04-21 11:25:30

标签: android listview arraylist

Listview尺寸为10件。直到它在Listview中没有显示任何内容。我在上面的适配器中添加了Item of Item但是无法获得Listview单行。有人请帮我解决我在适配器中的错误吗?

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import bughunters.tashfik.chooseapp.Model.Category;
import bughunters.tashfik.chooseapp.Model.Comment;
import bughunters.tashfik.chooseapp.R;
public class CommentViewAdapter extends BaseAdapter {

private Context context;

private List<Comment> messagesItems;


// public static List<String> imagelist;

public CommentViewAdapter(Context context, List<Comment> navDrawerItems) {
    Toast.makeText(context,"Sixe ids"+String.valueOf(navDrawerItems.size()),Toast.LENGTH_LONG).show();
    this.context = context;
    this.messagesItems = navDrawerItems;

}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {


    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    convertView = mInflater.inflate(R.layout.comments_list_item, null);

    TextView textViewProfileName = (TextView) convertView.findViewById(R.id.textViewProfileName);
    TextView textViewMesssage = (TextView) convertView.findViewById(R.id.textViewMesssage);
    TextView textViewTime = (TextView) convertView.findViewById(R.id.textViewTime);
    Toast.makeText(context,messagesItems.get(position).getUsername(),Toast.LENGTH_LONG).show();
    ImageView imageViewUserProfilePic = (ImageView) convertView.findViewById(R.id.imageViewUserProfilePic);


    textViewProfileName.setText(messagesItems.get(position).getUsername());
    textViewMesssage.setText(messagesItems.get(position).getMessage());
    textViewTime.setText(messagesItems.get(position).getTime());


    return convertView;
}

}

1 个答案:

答案 0 :(得分:1)

替换以下代码: -

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import bughunters.tashfik.chooseapp.Model.Category;
import bughunters.tashfik.chooseapp.Model.Comment;
import bughunters.tashfik.chooseapp.R;
public class CommentViewAdapter extends BaseAdapter {

private Context context;

private List<Comment> messagesItems;


// public static List<String> imagelist;

public CommentViewAdapter(Context context, List<Comment> navDrawerItems) {
    Toast.makeText(context,"Sixe ids"+String.valueOf(navDrawerItems.size()),Toast.LENGTH_LONG).show();
    this.context = context;
    this.messagesItems = navDrawerItems;

}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView=mInflater.inflate(R.layout.comments_list_item, null,true);

    TextView textViewProfileName = (TextView) rowView.findViewById(R.id.textViewProfileName);
    TextView textViewMesssage = (TextView) rowView.findViewById(R.id.textViewMesssage);
    TextView textViewTime = (TextView) rowView.findViewById(R.id.textViewTime);

    ImageView imageViewUserProfilePic = (ImageView) convertView.findViewById(R.id.imageViewUserProfilePic);

    textViewProfileName.setText(messagesItems.get(position).getUsername());
    textViewMesssage.setText(messagesItems.get(position).getMessage());
    textViewTime.setText(messagesItems.get(position).getTime());

    return rowView;
}
}

使用以下代码检查您的MainActivity代码: -

CommentViewAdapter customAdapter = new CommentViewAdapter(getApplicationContext(), levelLists(your ArrayList Put here));
  listview.setAdapter(customAdapter);