从List Array填充自定义ListView

时间:2012-12-23 19:03:39

标签: android listview

我有以下课程:

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;
    }
}

然后我有ArrayList<Person>一些这些数据。

我想要做的是填充我的ListView,我已经制作了一个(坏)模型,我希望它看起来像:

在看了一些关于这个在线的信息之后,我做了一个新的布局,我放了一个Medium TextView,下面有一个Small TextView,但是现在我很困惑我怎么能用我的ListView链接它MainActivity,然后使用我ArrayList<Person>中的数据填充它。

3 个答案:

答案 0 :(得分:8)

首先,您需要为您的行添加自定义布局:

<强> /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" />

之后,您需要一个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
     }
}

在您的活动中,您可以执行以下操作:

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);

    }

}

最后:

<强> /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>

注意ListView的id:如果您将活动扩展为ListActivity,则列表的ID必须为@android:id/list

我希望这能帮到你!

答案 1 :(得分:1)

为此,您需要执行以下步骤:

1。首先创建一个行布局表单ListView行并将其放在res/layout文件夹

2. :通过扩展ArrayAdapter

为ListView创建自定义适配器

您可以看到这些为ListView创建自定义适配器的教程:

http://www.ezzylearning.com/tutorial.aspx?tid=1763429

http://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/

答案 2 :(得分:0)

如果要使用recyclerView进行此操作,请遵循以下步骤

1)使用RecyclerView创建布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent" >

     <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

2)创建一行ItemView

创建视图row.xml

3)创建适配器

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder> {

private Context context;
private List<Person> list;

public ListAdapter (Context context, List<Person> list)
{
    this.context = context;
    this.list = list;
}


@Override
public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(context).inflate(R.layout.row,parent,false);
    return new ListViewHolder(view);
}

@Override
public void onBindViewHolder(ListViewHolder holder, final int position) {

    final Person person = list.get(position);
    holder.name.setText(person.getName());
    holder.name.setText(person.getDesc());

}

@Override
public int getItemCount() {
    return list.size();
}

class ListViewHolder extends RecyclerView.ViewHolder
{
    private TextView name;
    private ImageView desc;

    public ListViewHolder (View itemView) {
        super(itemView);
        name= (TextView)itemView.findViewById(R.id.name);
        desc= (ImageView)itemView.findViewById(R.id.desc);
    }
 }
}

4)将RecyclerView集成到您的活动中

public class YourActivity extends AppcompatActivity{

private List<Person> persons;
private YourAdapter adapter;
private RecyclerView rv;

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

    rv = (RecyclerView)findViewById(R.id.rv);
    person = // Get your List
    adapter = new YourAdapter(this, persons);
    setListAdapter(mAdapter);
    LinearLayoutManager llm = new LinearLayoutManager(YourActivity.this);
    rv.setLayoutManager(llm);
    rv.setAdapter(adapter);

}

}