这是我的带适配器的主班
ListView listView;
Bean bean;
ArrayList<Bean> arrayList;
ArrayAdapter<Bean> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_item);
arrayList = new ArrayList<>();
bean= new Bean("demo");
arrayList.add(bean);
arrayAdapter= new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(arrayAdapter); }
这是我的Bean类
public class Bean {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Bean(String name)
{
this.name=name;
}
}
列表在我的应用程序中显示如下:
请帮助我解决此问题的过程很费时间,但仍然是相同的错误
答案 0 :(得分:0)
为您的Bean类覆盖toString方法。
答案 1 :(得分:0)
要完全自定义显示数据的方式,可以覆盖ArrayAdapter定义来创建数据。
自定义布局:item_bean.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<!-- Here, you can display index for example -->
<TextView
android:id="@+id/tvIndex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:style="@style/styleIndex" />
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:style="@style/styleName" /> <!-- You can use your own style -->
</LinearLayout>
自定义适配器:BeansAdapter
public class BeansAdapter extends ArrayAdapter<Bean> {
public UsersAdapter(Context context, ArrayList<Bean> beans) {
super(context, 0, beans);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Bean bean = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_bean, parent, false);
}
// Lookup view for data population
TextView tvIndex = (TextView) convertView.findViewById(R.id.tvIndex);
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
// Populate the data into the template view using the data object
tvIndex.setText(position);
tvName.setText(bean.getName());
// Return the completed view to render on screen
return convertView;
}
}