Android 如何在不按任何按钮的情况下将所有联系人显示到版面
我是android的新手,尤其是sqlite,所以请耐心听我说 我想问,我如何从android DB中检索所有数据并将其显示到android布局,而不按任何按钮 以下是我在数据库类中的代码:Android 如何在不按任何按钮的情况下将所有联系人显示到版面,android,sqlite,listview,Android,Sqlite,Listview,我是android的新手,尤其是sqlite,所以请耐心听我说 我想问,我如何从android DB中检索所有数据并将其显示到android布局,而不按任何按钮 以下是我在数据库类中的代码: // Getting All Contacts public List<Contact> getAllContacts() { List<Contact> contactList = new ArrayList<Contact>(); String se
// Getting All Contacts
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
String selectQuery = "SELECT * FROM " + TABLE_CS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setId(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setTime(Integer.parseInt(cursor.getString(2)));
contactList.add(contact);
} while (cursor.moveToNext());
}
return contactList;
}
这当然会产生错误,因为我正在从静态上下文调用非静态方法。
我应该如何从MainActivity调用GetAllContacts
编辑:
这是我的客户适配器
public class CustomCursor extends CursorAdapter {
public CustomCursor(Context context, Cursor c) {
super(context, c);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// when the view will be created for first time,
// we need to tell the adapters, how each item will look
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.single_row_item, parent, false);
return retView;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// here we are setting our data
// that means, take the data from the cursor and put it in views
TextView textViewPersonName = (TextView) view.findViewById(R.id.tv_person_name);
textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
TextView textViewPersonPIN = (TextView) view.findViewById(R.id.tv_person_pin);
textViewPersonPIN.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2))));
}
}
您应该这样做:
listView = (ListView) findViewById(R.id.list_data);
ArrayList<Contact> mList = new ArrayList<>();
mList = DB.getAllContacts();
还要确保dba是如图所示的DAO对象
编辑:
以下是基本适配器的示例:
public class ListAdapter extends BaseAdapter {
private ArrayList<Contact> list = new ArrayList<>();
private Context context;
public ListAdapter(Context context, ArrayList<Contact> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
private static class Holder {
ImageView img; // whatever you have here
TextView tv;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.item_layout_here, parent, false);
holder.img = (ImageView) convertView.findViewById(R.id.someid);
holder.tv = (TextView) convertView.findViewById(R.id.yourid);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
//post your code here whatever you want to show
return convertView;
}
我不知道为什么要用Handler。在MainActivity中,您只需执行以下操作:
listView = (ListView) findViewById(R.id.list_data);
customAdapter = new CustomCursor(MainActivity.this, DB.getAllContacts());
listView.setAdapter(customAdapter);
如果您想直接从DB访问getAllContacts,则必须将其定义为静态,否则您可以创建DB的实例它会给我相同的错误,因为我使用静态上下文中的非静态方法尝试在没有处理程序的情况下执行此操作;我不知道你为什么需要它。我给了我更多的错误,但我认为问题不在于处理程序,它在这里:mList=DB.getAllContacts;上面写的是关于静电的。关于DAO,我不知道它到底是什么,但我认为这里没有必要,感谢您在DB.getAllContacts之前尝试思考;您是不是像->DB=newdatabaseclassthis//contextYes但是我输入了一个错误,现在我只有一个错误:我应该把android.database.Cursor放在哪里,我在这行中有java.Uti.ArrayList customAdapter=new customcursormorinactivity.this,mList;。此外,我认为我必须将我的列表getAllContact设置为ArrayList GetAllContacts如果我将getAllContact设置为静态,我会得到以下错误java.Uti.List
public class ListAdapter extends BaseAdapter {
private ArrayList<Contact> list = new ArrayList<>();
private Context context;
public ListAdapter(Context context, ArrayList<Contact> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
private static class Holder {
ImageView img; // whatever you have here
TextView tv;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.item_layout_here, parent, false);
holder.img = (ImageView) convertView.findViewById(R.id.someid);
holder.tv = (TextView) convertView.findViewById(R.id.yourid);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
//post your code here whatever you want to show
return convertView;
}
listView = (ListView) findViewById(R.id.list_data);
customAdapter = new CustomCursor(MainActivity.this, DB.getAllContacts());
listView.setAdapter(customAdapter);