android中使用listview显示sqlite中的数据 100
想实现个主界面可以添加数据,可以跳转到listview界面,然后listview是显示数据库中的数据,现在添加数据没问题,绑定适配器弄了好几天还是没搞懂,麻烦谁能帮忙告诉...
想实现个主界面可以添加数据,可以跳转到listview界面,然后listview是显示数据库中的数据,现在添加数据没问题,绑定适配器弄了好几天还是没搞懂,麻烦谁能帮忙告诉下怎么下,最好能告诉下根据这个代码在哪写代码,写什么代码。查了好几天都没看懂。
展开
展开全部
package com.it.db;
import java.util.List;
import com.it.dao.PersonDao;
import com.it.domain.Person;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ListView lv;
private List<Person> persons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PersonDao dao=new PersonDao(this);
persons=dao.findAll();
lv=(ListView) findViewById(R.id.lv);
lv.setAdapter(new MyAdapter());
lv.setOnItemClickListener(new OnItemClickListener() {
//点击的数据 传到上一个activity
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Person mperson=persons.get(position);
String number=mperson.getNumber();
Intent data=new Intent();
data.putExtra("number", number);
setResult(0, data);
//点击 即关闭此activity
finish();
}
});
}
private class MyAdapter extends BaseAdapter{
private static final String TAG = "MyAdapter";
/**
* 控制listview里有多少个条目
*/
@Override
public int getCount() {
// TODO Auto-generated method stub
return persons.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 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
/**
* 采用inflater显示
*/
Person person=persons.get(position);
View view=View.inflate(MainActivity.this, R.layout.list_item, null);
TextView tv_id=(TextView) view.findViewById(R.id.tv_id);
tv_id.setText("id:"+person.getId());
TextView tv_name=(TextView) view.findViewById(R.id.tv_name);
tv_name.setText("姓名:"+person.getName());
TextView tv_number=(TextView) view.findViewById(R.id.tv_number);
tv_number.setText("电话:"+person.getNumber());
return view;
}
/**
* 不用inflater
*
Log.i(tag, "位置"+position); //看效果
TextView tv=new TextView(getApplicationContext());
tv.setTextSize(20);
tv.setTextColor(Color.BLACK);
//每个位置上的条目
Person person=persons.get(position);
tv.setText(person.toString());
return tv;
}*/
}
}
import java.util.List;
import com.it.dao.PersonDao;
import com.it.domain.Person;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ListView lv;
private List<Person> persons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PersonDao dao=new PersonDao(this);
persons=dao.findAll();
lv=(ListView) findViewById(R.id.lv);
lv.setAdapter(new MyAdapter());
lv.setOnItemClickListener(new OnItemClickListener() {
//点击的数据 传到上一个activity
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Person mperson=persons.get(position);
String number=mperson.getNumber();
Intent data=new Intent();
data.putExtra("number", number);
setResult(0, data);
//点击 即关闭此activity
finish();
}
});
}
private class MyAdapter extends BaseAdapter{
private static final String TAG = "MyAdapter";
/**
* 控制listview里有多少个条目
*/
@Override
public int getCount() {
// TODO Auto-generated method stub
return persons.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 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
/**
* 采用inflater显示
*/
Person person=persons.get(position);
View view=View.inflate(MainActivity.this, R.layout.list_item, null);
TextView tv_id=(TextView) view.findViewById(R.id.tv_id);
tv_id.setText("id:"+person.getId());
TextView tv_name=(TextView) view.findViewById(R.id.tv_name);
tv_name.setText("姓名:"+person.getName());
TextView tv_number=(TextView) view.findViewById(R.id.tv_number);
tv_number.setText("电话:"+person.getNumber());
return view;
}
/**
* 不用inflater
*
Log.i(tag, "位置"+position); //看效果
TextView tv=new TextView(getApplicationContext());
tv.setTextSize(20);
tv.setTextColor(Color.BLACK);
//每个位置上的条目
Person person=persons.get(position);
tv.setText(person.toString());
return tv;
}*/
}
}
2015-04-08
展开全部
String context=cus.getString(cus.getColumnIndex("context"));
注册事件就在里面得到那个button然后setOnclickListen;
注册事件就在里面得到那个button然后setOnclickListen;
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询