我有以下类(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;
}
}
然后我有一个 ArrayList<Person>
使用其中的一些数据。
我想做的是填充我的 ListView,我已经制作了一个我希望它看起来像的(坏的)模型:
在网上查看了有关此的一些信息后,我做了一个新布局,我在其中放置了一个中型 TextView,在其下方有一个小型 TextView,但现在我很困惑如何将它与我的 ListView 链接起来MainActivity,然后用我的 ArrayList<Person>
中的数据填充它.
最佳答案
首先,您需要为您的行自定义布局:
/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
}
}
在您的 Activity 中,您可以这样做:
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:如果您将 Activity 扩展为 ListActivity,则列表的 id 必须是 @android:id/list
。
希望对你有帮助!
关于android - 从 ListArray 填充自定义 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14013833/