这里我试图将SQLite数据库中的数据提取到自定义列表视图中,但它无法正常工作。它没有从数据库中获取数据。你可以帮助我,我的代码错了。感谢
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.AddressBook.MESSAGE";
private ListView lv;
List<Listcollection> collectionlist;
DBHelper mydb;
Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb = new DBHelper(this);
lv = (ListView) findViewById(R.id.listView1);
collectionlist = mydb.getList();
lv.setAdapter(new ViewAdapter());
// adding it to the list view.
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int id_To_Search = arg2 + 1;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(),
com.example.addressbook.Display.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
}
public class ViewAdapter extends BaseAdapter {
LayoutInflater mInflater;
public ViewAdapter() {
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return collectionlist.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
}
final TextView nameText = (TextView) convertView
.findViewById(R.id.lvname);
nameText.setText("Name : " + collectionlist.get(position).getName());
final TextView ageText = (TextView) convertView
.findViewById(R.id.lvdate);
ageText.setText("Age : " + collectionlist.get(position).getDate());
return convertView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.item1:
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", 0);
Intent intent = new Intent(getApplicationContext(),
com.example.addressbook.Display.class);
intent.putExtras(dataBundle);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public boolean onKeyDown(int keycode, KeyEvent event) {
if (keycode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
}
return super.onKeyDown(keycode, event);
}
}
答案 0 :(得分:1)
试试这个
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);
mydb = new DBHelper(this);
lv.setAdapter(new ViewAdapter(mydb.getList()));
// adding it to the list view.
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int id_To_Search = arg2 + 1;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(),
com.example.addressbook.Display.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
}
public class ViewAdapter extends BaseAdapter {
LayoutInflater mInflater;
List<Listcollection> collectionlist;
public ViewAdapter(List<Listcollection> c) {
collectionlist = c;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return collectionlist.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
}
final TextView nameText = (TextView) convertView
.findViewById(R.id.lvname);
nameText.setText("Name : " + collectionlist.get(position).getName());
final TextView ageText = (TextView) convertView
.findViewById(R.id.lvdate);
ageText.setText("Age : " + collectionlist.get(position).getDate());
return convertView;
}
}
表示同时删除全局变量集合列表