列表视图正在运行。但我想为此数据列表添加自定义列表视图。我从预填充数据库中检索数据。这是我试过的。我在活动课上遇到错误。
CustomListViewAdapter
public class CustomListViewAdapter extends ArrayAdapter<Organization> {
Context context;
public CustomListViewAdapter(Context context, int layout,
List<Organization> items) {
super(context, layout, items);
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
TextView txtTitle;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Organization rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.single_row_item, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.org_name);
convertView.setTag(holder);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtTitle.setText(rowItem.getTitle());
return convertView;
}}
Bean Class
public class Organization {
private String title;
public Organization(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
} }
活动类
public class ContactView extends Activity {
private ListView listView;
private ListView listView1;
List<Organization> rowItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
this.listView = (ListView) findViewById(R.id.listView);
DBAccess databaseAccess = DBAccess.getInstance(this);
databaseAccess.open();
List<String> quotes = databaseAccess.getQuotes(getIntent().getStringExtra("ID_EXTRA"));
databaseAccess.close();
listView = (ListView) findViewById(R.id.listView);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.single_row_item, quotes);
listView.setAdapter(adapter);
}}
错误我正在
Error:(81, 41) error: constructor CustomListViewAdapter in class CustomListViewAdapter cannot be applied to given types;
必需:Context,int,List 发现:ContactView,int,List 原因:实际参数List无法通过方法调用转换转换为List
答案 0 :(得分:0)
在databaseAccess.close();
List<Organization> rowItem=new ArrayList<Organization>();
for(String quote:quotes){
Organization temp=new Organization(quote);
rowItem.add(temp);
}
listView = (ListView) findViewById(R.id.listView);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.single_row_item, rowItem);
listView.setAdapter(adapter);
您当前代码的问题是CustomListViewAdapter
的构造函数期望第三个参数为List
类型Organization
,但您传递List
类型String
}。