除了使用String之外,如何在列表中显示数据?

时间:2013-09-24 09:21:32

标签: android android-listview

我使用线性布局(如下所示)来显示我想在列表视图中显示的数据。我怎么能实现这个?通过使用下面的代码,我得到空白的listview,不知道为什么! 下面是listview项目

Listview Items

这是我正在使用的代码

public class ListAdapter extends BaseAdapter {

private LayoutInflater mInflater;
private Context context;
private ArrayList<Data> friendListData;

private class ViewHolder {
    public TextView name;
    public ImageView pro_image;

    public TextView comment;

}

public ListAdapter(Context context, ArrayList<Data> friendDataList) {
    // TODO Auto-generated constructor stub
    super();
    this.friendListData = friendDataList;
    this.context = context;

}

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

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

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

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

    ViewHolder holder = null;

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

    if (convertView == null) {

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

        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.name);
        holder.comment = (TextView) convertView.findViewById(R.id.comment);
        holder.pro_image = (ImageView) convertView.findViewById(R.id.avatarPic);
        convertView.setTag(holder);

    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }
    Data data = (Data) getItem(position);

    holder.name.setText(data.getName());
    holder.comment.setText(data.getComment());
    holder.pro_image.setImageResource(data.getPro_image());

    return convertView;
}


public class Data {
String name;
String comment;
int pro_image;

     }
  }

在MainActivity.java中,我正在使用像这样的适配器

    lv = (ListView) findViewById(R.id.ListView);
    friendsDataList = new ArrayList<Data>();
    adapter = new ListAdapter(MainActivity.this, friendsDataList);

    lv.setAdapter(adapter);

7 个答案:

答案 0 :(得分:1)

通过谷歌观看“the world of listView”讲座。

简而言之,您需要做的是:

  1. 创建一个单独的布局xml,用作每行的模板。为每个视图设置一个id,根据数据需要更改其数据。

  2. 扩展BaseAdapter并实施getView()以根据数据中的位置显示项目,并getCount()获取行数。您可以实施getItem()以获取数据中的项目。

    请记住以有效的方式实现getView()方法,如演讲中所示(如果它不为null,则使用convertView,并使用ViewHolder“设计模式”)。

  3. 将您创建的类的新实例设置为listView的适配器。

  4. 就是这样。

    即使在API演示中,也有很多关于如何在互联网上进行此操作的示例。

    了解你在做什么更重要。

答案 1 :(得分:1)

你需要检查你的arraylist大小...

Log.i("====SIZE=======","======>"+friendsDataList.size());  
adapter = new ListAdapter(MainActivity.this, friendsDataList);

在不同的类中声明您的数据。

public class Data {
String name;
String comment;
int pro_image; 
     }

在ArrayList中添加值以用于测试目的,例如..

Data data=new Data();
data.name="nidhi";
data.comment="Android developer";

friendDataList.add(data);

Data data=new Data();
data.name="Hardy";
data.comment="Android developer. Ready to help you.";

friendDataList.add(data);

在此之前:

adapter = new ListAdapter(MainActivity.this, friendsDataList);

答案 2 :(得分:0)

使用BaseAdapter或SimpleAdapter创建自定义适配器并覆盖getView方法。设计一个xml文件,如你想要显示的行,并在getview方法

中对其进行充气

有关使用BaseAdapter clickhere

的详细教程

答案 3 :(得分:0)

将适配器设置为:

      public class AdptrSchedules extends BaseAdapter {
private ArrayList<Model> aList;
private Activity activity;
private LayoutInflater layoutinflater;

public AdptrSchedules(ArrayList<Model> modelValues, Activity activity) {
    super();
    this.aList = modelValues;
    this.activity = activity;

}

@Override
public int getCount() {

    return aList.size();
}

@Override
public Object getItem(int position) {

    return aList.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}

public void setPositionSelected(int position) {

    this.notifyDataSetChanged();
}

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

    layoutinflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewHolder holder = null;
    Model model = aList.get(position);
    // ModelTemp employeeLocalDataModel = aListStudent.get(position);
    if (convertView == null || !(convertView.getTag() instanceof ViewHolder)) {
        convertView = layoutinflater.inflate(R.layout.row, null);
        holder = new ViewHolder();
        holder.txt = (TextView) convertView.findViewById(R.id.row_txt_name);

        holder.imgPhoto = (ImageView) convertView.findViewById(R.id.row_serch_img_photo);

        convertView.setTag(holder);
        convertView.setTag(R.id.row_serch_txt_name, holder.txtName);

        convertView.setTag(R.id.row_serch_img_photo, holder.imgPhoto);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtArtistName.setText("" + model.getNAME());

    holder.txtDay.setText("" + scheduleDay);
    // holder.imgPhoto.setChecked(aListMalls.get(position).isSelected());

    return convertView;
}

class ViewHolder {
    TextView txtName;

    ImageView imgPhoto;
}

   }

答案 4 :(得分:0)

它为我工作

创建list_layout.xml并将其传递给listview适配器

    private static class ListViewAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public ListViewAdapter(Context context) {

        mInflater = LayoutInflater.from(context);

    }

    public int getCount() {
        return ListviewContent.size();
    }

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

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

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

        ListContent holder;

        if (convertView == null) {
            convertView = mInflater
                    .inflate(R.layout.list_layout, null);

             holder = new ListContent();
            holder.text = (TextView) convertView
                    .findViewById(R.id.TextView01);


            convertView.setTag(holder);
        } else {

            holder = (ListContent) convertView.getTag();
        }

        holder.text.setText(ListviewContent.get(position));


        return convertView;

    }

    static class ListContent {
        TextView text;



    }
}

根据需要设计布局,最后将其设置为列表视图

    setListAdapter(new ListViewAdapter(this));

答案 5 :(得分:0)

您可以右键单击UI设计器空间中的listView并选择“预览列表内容”。现在选择要在listView中显示的布局内容。它会让你知道你的内容在listView中的外观。

答案 6 :(得分:-1)

要创建自定义ListView项,您应该扩展一个Adapter类(我建议您使用BaseAdapter类),并在方法getView(...)中实例化您的布局。

一个很好的例子,使用模式ViewHolder(This with ArrayAdapter)

http://www.jmanzano.es/blog/?p=166

另一个是BaseAdapter

http://qtcstation.com/2012/04/a-limitation-with-the-viewholder-pattern-on-listview-adapters/

Aclaration。 ViewHolder模式使得listview变得流畅,因为使用findViewById方法非常非常慢。使用视图,您只能在正确的时刻使用它