Java Android类型不匹配帮助。是否将ArrayList转换为List?
好的。。这可能很难理解 应用程序将联系人保存到名为Contacts的SQLite数据库 ContactActivity应加载到SQLite数据库中,并创建一个自定义ListView,允许通过切换按钮选择多个联系人 我下面的示例创建了一个允许多个选择的自定义LV,它有一个本地联系人类。我在从外部DAO联系人类转换到本地联系人类时遇到问题 Contact.java DAO类:Java Android类型不匹配帮助。是否将ArrayList转换为List?,java,android,sqlite,Java,Android,Sqlite,好的。。这可能很难理解 应用程序将联系人保存到名为Contacts的SQLite数据库 ContactActivity应加载到SQLite数据库中,并创建一个自定义ListView,允许通过切换按钮选择多个联系人 我下面的示例创建了一个允许多个选择的自定义LV,它有一个本地联系人类。我在从外部DAO联系人类转换到本地联系人类时遇到问题 Contact.java DAO类: package dto; /** * This DTO describes a Contact that can cre
package dto;
/**
* This DTO describes a Contact that can create and respond to a Survey
* @author Barnitek, Bosticsa
*
*/
public class Contact {
private int id;
private String firstName;
private String lastName;
private String phoneNumber;
public Contact(){
}
public Contact(int id, String firstName, String lastName, String phoneNumber){
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
}
public Contact(String firstName, String lastName, String phoneNumber){
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
}
public int getID(){
return id;
}
public void setID(int id){
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
ContactActivity类:
package edu.uc.pollyo;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import dao.PollyoBaseDAO;
/**
*
* @author Barnitek
*
* This activity builds a custom list view that lists an array of names
* and phone numbers with checkable boxes. When a box is checks the
* name+number from that row is added to a string that will then be
* returned to the MainActivity and placed in the contacts text view.
*
*/
public class ContactActivity extends MainActivity {
private ListView mainListView;
private ArrayAdapter<Contact> listAdapter;
private List<Contact> contactList;
private ArrayList<dto.Contact> contactArrayList;
public static String listContacts = "";
PollyoBaseDAO mydb;
// when activity is created
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
//contactList = new ArrayList<dto.Contact>();
mydb = new PollyoBaseDAO(this);
contactArrayList = mydb.getAllContacts();
//**************PROBBLEM HERE***************
// The problem is: Type mismatch: cannot convert from ArrayList<Contact> to
// List<ContactActivity.Contact>
contactList = contactArrayList;
// Set our custom array adapter as the ListView's adapter.
listAdapter = new ContactArrayAdapter( getApplicationContext(), contactList);
// Find the ListView resource.
mainListView = (ListView) findViewById(R.id.lvContactView);
mainListView.setAdapter(listAdapter);
// When item is tapped, toggle checked properties of CheckBox and
// Contact.
mainListView
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View item,
int position, long id) {
Contact contact = listAdapter.getItem(position);
contact.toggleChecked();
ContactViewHolder viewHolder = (ContactViewHolder) item
.getTag();
viewHolder.getCheckBox()
.setChecked(contact.isChecked());
// build a string of selected contacts when one is
// checked
listContacts += contact.getFirstName() + " " + contact.getLastName() + " | ";
System.out.println("listContacts: (ContactActivity) "
+ listContacts);
}
});
mainListView.setAdapter(listAdapter);
}
// Holds contact data
private static class Contact {
private String firstName = "";
private String lastName = "";
private boolean checked = false;
public Contact(){};
public Contact(String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
public Contact (String firstName, String lastName, boolean checked){
this.firstName = firstName;
this.lastName = lastName;
this.checked = checked;
}
public String getFirstName() {
return firstName;
}
public String getLastName(){
return lastName;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public String toString() {
return firstName;
}
public void toggleChecked() {
checked = !checked;
}
}
// Holds child views for each row
private static class ContactViewHolder {
private CheckBox checkBox;
private TextView textView;
public ContactViewHolder(){};
public ContactViewHolder(TextView textView, CheckBox checkBox) {
this.checkBox = checkBox;
this.textView = textView;
}
public CheckBox getCheckBox() {
return checkBox;
}
public TextView getTextView() {
return textView;
}
public void setTextView(TextView textView){
this.textView = textView;
}
}
/** Custom adapter for displaying an array of Contact objects. */
private static class ContactArrayAdapter extends ArrayAdapter<Contact> {
private LayoutInflater inflater;
public ContactArrayAdapter(Context context, List<Contact> contactList) {
super(context, R.layout.simplerow, R.id.rowTextView, contactList);
// Cache the LayoutInflate to avoid asking for a new one each time.
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// contact to display
Contact contact = (Contact) this.getItem(position);
// The child views in each row.
CheckBox checkBox;
TextView textView;
// Create a new row view
if (convertView == null) {
convertView = inflater.inflate(R.layout.simplerow, parent);
// Find the child views.
textView = (TextView) convertView
.findViewById(R.id.rowTextView);
checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox01);
// Optimization: Tag the row with it's child views, so we don't
// have to
// call findViewById() later when we reuse the row.
convertView.setTag(new ContactViewHolder(textView, checkBox));
// If CheckBox is toggled, update the contact it is tagged with.
checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Contact contact = (Contact) cb.getTag();
contact.setChecked(cb.isChecked());
}
});
}
// Reuse existing row view
else {
// Because we use a ViewHolder, we avoid having to call
// findViewById().
ContactViewHolder viewHolder = (ContactViewHolder) convertView
.getTag();
checkBox = viewHolder.getCheckBox();
textView = viewHolder.getTextView();
}
// Tag the CheckBox with the Contact it is displaying, so that we
// can
// access the contact in onClick() when the CheckBox is toggled.
checkBox.setTag(contact);
// Display contact data
checkBox.setChecked(contact.isChecked());
textView.setText(contact.getFirstName() + " " + contact.getLastName());
return convertView;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.help:
showHelp();
return true;
case R.id.about:
showAbout();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// launch the about activity
private void showAbout() {
Intent about = new Intent(this, AboutActivity.class);
startActivity(about);
}
// launch the help activity
private void showHelp() {
Intent help = new Intent(this, HelpActivity.class);
startActivity(help);
}
// when the user clicks OK, send the list of selected contacts back
public void onOkClicked(View v) {
Intent output = new Intent();
output.putExtra("selectedContacts", listContacts);
setResult(Activity.RESULT_OK, output);
finish();
}
// when the user clicks New Contact, start a new activity to add a new
// contact to the list
public void onNewContactClicked(View v) {
Intent newContact = new Intent(this, AddContactActivity.class);
startActivity(newContact);
}
}
包edu.uc.pollyo;
导入java.util.ArrayList;
导入java.util.List;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.CheckBox;
导入android.widget.ListView;
导入android.widget.TextView;
导入dao.PollyoBaseDAO;
/**
*
*@作者巴尼泰克
*
*此活动将生成一个自定义列表视图,其中列出一个名称数组
*和带有复选框的电话号码。当一个盒子被选中时
*将该行中的名称+数字添加到一个字符串中,然后
*返回到MainActivity并放置在contacts文本视图中。
*
*/
公共类ContactActivity扩展了MainActivity{
私有ListView主ListView;
专用阵列适配器列表适配器;
私人名单联系人名单;
私人ArrayList联系人ArrayList;
公共静态字符串listContacts=“”;
PollyoBaseDAO mydb;
//创建活动时
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
//contactList=新的ArrayList();
mydb=新的PollyoBaseDAO(本);
contactArrayList=mydb.getAllContacts();
//**************这里的问题***************
//问题是:类型不匹配:无法从ArrayList转换为
//名单
contactList=contactArrayList;
//将自定义阵列适配器设置为ListView的适配器。
listAdapter=new ContactArrayAdapter(getApplicationContext(),contactList);
//查找ListView资源。
mainListView=(ListView)findViewById(R.id.lvContactView);
mainListView.setAdapter(listAdapter);
//点击项目时,切换复选框和的选中属性
//联系方式。
主列表视图
.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父项、视图项、,
内部位置,长id){
Contact=listAdapter.getItem(位置);
contact.toggleChecked();
ContactViewHolder viewHolder=(ContactViewHolder)项目
.getTag();
viewHolder.getCheckBox()
.setChecked(contact.isChecked());
//创建选定联系人时,生成一个选定联系人的字符串
//检查
listContacts+=contact.getFirstName()+“”+contact.getLastName()+“|”;
System.out.println(“列表联系人:(联系人活动)”
+列表联系人);
}
});
mainListView.setAdapter(listAdapter);
}
//保存联系人数据
私有静态类联系人{
私有字符串firstName=“”;
私有字符串lastName=“”;
私有布尔值选中=false;
公共联系(){};
公共联系人(字符串firstName,字符串lastName){
this.firstName=firstName;
this.lastName=lastName;
}
公共联系人(字符串名、字符串名、布尔值已选中){
this.firstName=firstName;
this.lastName=lastName;
this.checked=checked;
}
公共字符串getFirstName(){
返回名字;
}
公共字符串getLastName(){
返回姓氏;
}
公共布尔值已检查(){
退货检查;
}
公共void setChecked(布尔值已选中){
this.checked=checked;
}
公共字符串toString(){
返回名字;
}
public void toggleChecked(){
选中=!选中;
}
}
//保存每行的子视图
私有静态类ContactViewHolder{
私有复选框;
私有文本视图文本视图;
public ContactViewHolder(){};
公共联系人视图持有者(文本视图文本视图,复选框){
this.checkBox=复选框;
this.textView=textView;
}
公共复选框getCheckBox(){
返回复选框;
}
公共文本视图getTextView(){
返回文本视图;
}
公共void setTextView(TextView TextView){
this.textView=textView;
}
}
/**用于显示联系人对象数组的自定义适配器*/
私有静态类ContactArrayAdapter扩展了ArrayAdapter{
私人充气机;
公共联系人阵列适配器(上下文、列表联系人列表){
super(上下文、R.layout.simplerow、R.id.rowTextView、联系人列表);
//缓存LayoutFlate以避免每次请求新的LayoutFlate。
充气器=充气器。从(上下文);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//要显示的联系人
Contact Contact=(Contact)this.getItem(position);
//每行中的子视图。
复选框;
文本视图文本视图;
//创建新的行视图
if(convertView==null){
convertView=充气机。充气(R.layout.simplerow,父级);
//查找子视图。
textView=(textView)convertView
.findViewById(R.id.rowTextView);
复选框=(复选框)convertView.findVi
public ArrayList<Contact> getAllContacts()
{
ArrayList<Contact> array_list = new ArrayList<Contact>();
String selectQuery = "SELECT * FROM " + CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery( selectQuery, null );
if (res.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(res.getString(0)));
contact.setFirstName(res.getString(1));
contact.setLastName(res.getString(2));
contact.setPhoneNumber(res.getString(3));
// Adding contact to list
array_list.add(contact);
} while (res.moveToNext());
}
// return contact list
return array_list;
}
private List<ContactActivity.Contact> transformListFromDB(List<dto.Contact> listFromDb) {
List<ContactActivity.Contact> result = new ArrayList<ContactActivity.Contact>();
for (dto.Contact con : listFromDb) {
result.add(new ContactActivity.Contact(con.firstName, con.lastName);
}
return result;
}