填充从ListArray一个自定义的ListView [英] Populating a custom ListView from ListArray

查看:139
本文介绍了填充从ListArray一个自定义的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

 公共类Person {

    公共字符串名称;
    公共字符串描述;

    公板(字符串名称,字符串描述){
        this.name =名称;
        this.description =描述;
    }

    公共字符串的getName()
    {
        返回this.name;
    }

    公共字符串getDescription()
    {
        返回this.description;
    }
}
 

然后我有一个的ArrayList<人> 有一些这方面的数据。

我想要做的就是填充我的ListView什么,我做了什么,我希望它看起来像一个(坏)小样:

在看这个网上的一些信息,我已经做出了新的布局,我已经把中等TextView中,用小的TextView在它之下,但现在我很困惑我怎么可以链接,在与ListView的我MainActivity,然后填充它从我的的ArrayList和其中的数据;人>

解决方案

首先,你需要自定义布局为您行:

/res/layout/my_row_layout.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>

<的TextView
    机器人:ID =@ + ID / textView_name
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文本=中文字
    机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

<的TextView
    机器人:ID =@ + ID / textView_description
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文本=TextView的/>
 

后,您将需要一个ArrayAdapter:

 公共类MyListAdapter扩展ArrayAdapter<人> {

私人上下文的背景下;
私人的ArrayList<人> allPersons;

私人LayoutInflater mInflater;
私人布尔mNotifyOnChange = TRUE;

公共MyListAdapter(上下文的背景下,ArrayList的<人> mPersons){
    超(背景下,R.layout.my_row_layout);
    this.context =背景;
    this.allPersons =新的ArrayList<人>(mPersons);
    this.mInflater = LayoutInflater.from(上下文);
}

@覆盖
公众诠释getCount将(){
    返回allPersons .size();
}

@覆盖
公众人物的getItem(INT位置){
    返回allPersons获得(位置);
}

@覆盖
众长getItemId(INT位置){
    // TODO自动生成方法存根
    返回的位置;
}

@覆盖
公众诠释为getPosition(个人项目){
    返回allPersons .indexOf(项目);
}

@覆盖
公众诠释getViewTypeCount(){
    返回1; //类型的号码+ 1 !!!!!!!!
}

@覆盖
公众诠释getItemViewType(INT位置){
    返回1;
}


@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
    最后ViewHolder持有人;
    整型= getItemViewType(位置);
    如果(convertView == NULL){
        持有人=新ViewHolder();
        开关(类型){
        情况1:
            convertView = mInflater.inflate(R.layout.my_row_layout,父母,假);
            holder.name =(TextView中)convertView.findViewById(R.id.textview_name);
            holder.description =(TextView中)convertView.findViewById(R.id.textview_description);
            打破;
        }
        convertView.setTag(保持器);
    } 其他 {
        支架=(ViewHolder)convertView.getTag();
    }
    holder.name.setText(allPersons.get(位置).getName());
    holder.description.setText(allPersons.get(位置).getDescription());
    holder.pos =位置;
    返回convertView;
}

@覆盖
公共无效notifyDataSetChanged(){
    super.notifyDataSetChanged();
    mNotifyOnChange =真;
}

公共无效setNotifyOnChange(布尔notifyOnChange){
    mNotifyOnChange = notifyOnChange;
}


// ---------------为每一行静态视图----------- //
     静态类ViewHolder {

         TextView的名称;
         TextView的描述;
         INT POS机; //存储列表中的项的位置
     }
}
 

在你的活动,你可以这样做:

 公共类SecondActivity扩展ListActivity {

    私人的ArrayList<人>人;
    私人MyListAdapter mAdapter;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        //填写者的数组列表

        this.mAdapter =新MyListAdapter(这一点,人);
        setListAdapter(mAdapter);

    }

}
 

最后:

/res/layout/activity_main.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent>

<的ListView
    机器人:ID =@机器人:ID /列表
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_centerHorizo​​ntal =真正的>
< / ListView控件>
 

注意ListView控件的ID:如果要扩展你的活动作为ListActivity,列表中的ID必须是 @android:ID /列表

我希望这可以帮助您!

I have the following class:

public class Person {

    public String name;
    public String description;

    public Board(String name, String description) {
        this.name = name;
        this.description = description;
    }

    public String getName()
    {
        return this.name;
    }

    public String getDescription()
    {
        return this.description;
    }
}

I then have an ArrayList<Person> with some of this data.

What I want to do is populate my ListView, I have made a (bad) mockup of what I want it to look like:

After looking at some information about this online, I have made a new layout where I have put a Medium TextView, with a Small TextView underneath it, but now I am confused to how I can link that in with the ListView on my MainActivity and then populate it with data from my ArrayList<Person>.

解决方案

First you need a custom layout for your row:

/res/layout/my_row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView_description"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

After, you will need an ArrayAdapter:

public class MyListAdapter extends ArrayAdapter<Person> {

private Context context;
private ArrayList<Person> allPersons;

private LayoutInflater mInflater;
private boolean mNotifyOnChange = true;

public MyListAdapter(Context context, ArrayList<Person> mPersons) {
    super(context, R.layout.my_row_layout);
    this.context = context;
    this.allPersons = new ArrayList<Person>(mPersons);
    this.mInflater = LayoutInflater.from(context);
}

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

@Override
public Person getItem(int position) {
    return allPersons .get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public int getPosition(Person item) {
    return allPersons .indexOf(item);
}

@Override
public int getViewTypeCount() {
    return 1; //Number of types + 1 !!!!!!!!
}

@Override
public int getItemViewType(int position) {
    return 1;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    int type = getItemViewType(position);
    if (convertView == null) {
        holder = new ViewHolder();
        switch (type) {
        case 1:
            convertView = mInflater.inflate(R.layout.my_row_layout,parent, false);
            holder.name = (TextView) convertView.findViewById(R.id.textview_name);
            holder.description = (TextView) convertView.findViewById(R.id.textview_description);
            break;
        }
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.name.setText(allPersons.get(position).getName());
    holder.description.setText(allPersons.get(position).getDescription());
    holder.pos = position;
    return convertView;
}

@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
    mNotifyOnChange = true;
}

public void setNotifyOnChange(boolean notifyOnChange) {
    mNotifyOnChange = notifyOnChange;
}


//---------------static views for each row-----------//
     static class ViewHolder {

         TextView name;
         TextView description;
         int pos; //to store the position of the item within the list
     }
}

In your activity, you can do this:

public class SecondActivity extends ListActivity {

    private ArrayList<Person> persons;
    private MyListAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //fill the arraylist of persons

        this.mAdapter = new MyListAdapter(this, persons);
        setListAdapter(mAdapter);

    }

}

Finally:

/res/layout/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" >
</ListView>

Attention to the id of the ListView: if you are extending your activity as ListActivity, the id of the list must be @android:id/list.

I hope this help you!

这篇关于填充从ListArray一个自定义的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆