现在做了一个ListView,里面的值是用cursor提取数据库的,现在想点击listview以后页面进行跳转,并且在新的activity里显示被点击的listview的数据,我的listview有很
现在做了一个ListView,里面的值是用cursor提取数据库的,现在想点击listview以后页面进行跳转,并且在新的activity里显示被点击的listview的数据,我的listview有很多列,所以希望数据能分开来放,现在完全没有一点头绪,查了资料也没有
DataListActivity.class
public class DataListActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {
ListView listView;
SQLiteDatabase sqLiteDatabase;
DBHelper dbHelper;
Cursor cursor;
ListDataAdapter listDataAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data_list_layout);
listView = (ListView) findViewById(R.id.list_view);
listDataAdapter = new ListDataAdapter(getApplicationContext(),R.layout.reporter_my_request);
listView.setAdapter(listDataAdapter);
listView.setOnItemClickListener(this);
dbHelper = new DBHelper(getApplicationContext());
sqLiteDatabase = dbHelper.getReadableDatabase();
cursor = dbHelper.getInformation(sqLiteDatabase);
if(cursor.moveToFirst())
{
do {
String id,name,title,department,category;
id = cursor.getString(0);
name = cursor.getString(1);
title= cursor.getString(2);
department=cursor.getString(3);
category=cursor.getString(4);
// description=cursor.getString(4);
DataProvider dataProvider=new DataProvider(id,name,title,department,category);
listDataAdapter.add(dataProvider);
}while (cursor.moveToNext());
}
dbHelper.close();
}
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
Object o =parent.getItemAtPosition(position);
Toast.makeText(this,"",Toast.LENGTH_LONG).show();
Intent intent = new Intent(DataListActivity.this,RequestListInfo.class);
startActivity(intent);
}
}
ListDataAdapter.class
public class ListDataAdapter extends ArrayAdapter {
List list = new ArrayList();
public ListDataAdapter(Context context, int resource) {
super(context, resource);
}
static class LayoutHandler
{
TextView ID,NAME,TITLE,DEPARTMENT,CATEGORY,DESCRIPTION;
}
@Override
public void add(Object object) {
super.add(object);
list.add(object);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutHandler layoutHandler;
if(row == null)
{
LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.reporter_my_request,parent,false);
layoutHandler = new LayoutHandler();
layoutHandler.ID = (TextView) row.findViewById(R.id.text_id);
layoutHandler.NAME = (TextView)row.findViewById(R.id.text_name);
layoutHandler.TITLE = (TextView)row.findViewById(R.id.text_title);
layoutHandler.DEPARTMENT = (TextView)row.findViewById(R.id.text_department);
layoutHandler.CATEGORY = (TextView)row.findViewById(R.id.text_category);
row.setTag(layoutHandler);
}
else
{
layoutHandler = (LayoutHandler)row.getTag();
}
DataProvider dataProvider = (DataProvider)this.getItem(position);
layoutHandler.ID.setText(dataProvider.getId());
layoutHandler.NAME.setText(dataProvider.getName());
layoutHandler.TITLE.setText(dataProvider.getTitle());
layoutHandler.DEPARTMENT.setText(dataProvider.getDepartment());
layoutHandler.CATEGORY.setText(dataProvider.getCategory());
// layoutHandler.DESCRIPTION.setText(dataProvider.getDescription());
return row;
}
}
DataProvider.class
public class DataProvider {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
private String name;
private String title;
private String department;
private String category;
// private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public DataProvider(String id,String name,String title,String department,String category){
this.id = id;
this.name= name;
this.title = title;
this.department= department;
this.category= category;
// this.description = description;
}
}
11 个解决方案
你是不是想传递所点击的对象到另外activity中,类似查看详情功能一样?如果是这样可以考虑EventBus解决
用Intent 把id传过去,然后通过id把数据查出来呢?
先想办法提取值,然后通过Intent 传过去啊,这样不行吗?
解决啦~ 附上代码
public void onItemClick(AdapterView> parent, View view, int position, long id) {
cursor.moveToPosition(position);
String rid = cursor.getString(0);
String rname = cursor.getString(1);
String rtitle = cursor.getString(2);
String rdepartment = cursor.getString(3);
String rcategory = cursor.getString(4);
Toast.makeText(this,rid,Toast.LENGTH_LONG).show();
Intent intent = new Intent(DataListActivity.this,RequestListInfo.class);
intent.putExtra("id",rid);
intent.putExtra("name",rname);
intent.putExtra("title",rtitle);
intent.putExtra("department",rdepartment);
intent.putExtra("category",rcategory);
startActivity(intent);
}
原来是我想的太复杂了- -