使用从游标获得的数据用 arrayList<String> 填充 ListView
Populate ListView with an arrayList<String> with data obtained from a cursor
Helo 我正在尝试使用存储在 sqlite 中的数据填充 listView。
在我 select 一个产品之后,我希望该产品的所有参考
像我在图片中画的一样走同一条线。
我可以让 ArrayAdapter 把所有的
记录在同一 xml?
我的代码是这样的:returns所有记录的游标:
public Cursor getAllRows() {
String where = null;
// query the DBAdapter
Cursor cursor = db.query(true, TABLE_NAME, ALL_KEYS, where, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
正在向 arrayList 添加数据:
public ArrayList<String> fromCursorToArrayListString(Cursor c){
ArrayList<String> result = new ArrayList<String>();
c.moveToFirst();
for(int i = 0; i < c.getCount(); i++){
String row_PRODUCT = c.getString(c.getColumnIndex(KEY_PRODUCT));
String row_PRICE = c.getString(c.getColumnIndex(KEY_PRICE));
String row_TYPE = c.getString(c.getColumnIndex(KEY_TYPE));
result.add(row_PRODUCT);
result.add(row_PRICE);
result.add(row_TYPE);
c.moveToNext();
}
return result;
}
我在mainActivity中这样写:
ListView listView = (ListView) findViewById(R.id.new_list_listView);
Cursor cursor = newListDBAdapter.getAllRows();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.custom_product_layout_new_list,R.id.custom_product_layout_new_list_productName,newListDBAdapter.fromCursorToArrayListString(cursor));
listView.setAdapter(arrayAdapter);
您可以使用 SimpleAdapter :
ListView list = (ListView) findViewById(R.id.mylist);
ArrayList<HashMap<String, String>> mylistData =
new ArrayList<HashMap<String, String>>();
String[] columnTags = new String[] {"col1", "col2", "col3"};
int[] columnIds = new int[] {R.id.column1, R.id.column2, R.id.column3};
for(int i=0; i<3; i++)
{
HashMap<String,String> map = new HashMap<String, String>();
//initialize row data
for(int j=0; j<3; j++)
{
map.put(columnTags[j], "row”+i+”col"+j);
}
mylistData.add(map);
}
SimpleAdapter arrayAdapter =
new SimpleAdapter(this, mylistData, R.layout.mylistrow,
columnTags , columnIds);
list.setAdapter(arrayAdapter);
或使用自定义适配器
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
ListView l1;
String[] t1={"video1","video2"};
String[] d1={"lesson1","lesson2"};
int[] i1 ={R.drawable.ic_launcher,R.drawable.ic_launcher};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
l1=(ListView)findViewById(R.id.list);
l1.setAdapter(new dataListAdapter(t1,d1,i1));
}
class dataListAdapter extends BaseAdapter {
String[] Title, Detail;
int[] imge;
dataListAdapter() {
Title = null;
Detail = null;
imge=null;
}
public dataListAdapter(String[] text, String[] text1,int[] text3) {
Title = text;
Detail = text1;
imge = text3;
}
public int getCount() {
// TODO Auto-generated method stub
return Title.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.custom, parent, false);
TextView title, detail;
ImageView i1;
title = (TextView) row.findViewById(R.id.title);
detail = (TextView) row.findViewById(R.id.detail);
i1=(ImageView)row.findViewById(R.id.img);
title.setText(Title[position]);
detail.setText(Detail[position]);
i1.setImageResource(imge[position]);
return (row);
}
}
}
CursorAdapter 适配器对此更可靠。您只需将 Cursor 传递给适配器,这样您就无需维护任何集合。你必须遵循几件事
适配器
public class YourCursorAdapter extends CursorAdapter {
public YourCursorAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, 0);
}
// The newView method is used to inflate a new view and return it,
// you don't bind any data to the view at this point.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.your_item_view, parent, false);
}
// The bindView method is used to bind all data to a given view
// such as setting the text on a TextView.
@Override
public void bindView(View view, Context context, Cursor c) {
// Find fields to populate in inflated template
TextView tvproduct = (TextView) view.findViewById(R.id.tcproduct);
TextView tvPrice = (TextView) view.findViewById(R.id.tvPrice);
TextView tvType = (TextView) view.findViewById(R.id.tvType);
// Extract properties from cursor
String row_PRODUCT = c.getString(c.getColumnIndex(KEY_PRODUCT));
String row_PRICE = c.getString(c.getColumnIndex(KEY_PRICE));
String row_TYPE = c.getString(c.getColumnIndex(KEY_TYPE));
// Populate fields with extracted properties
tvproduct.setText(row_PRODUCT);
tvPrice.setText(row_PRICE);
tvType.setText(row_TYPE);
}
}
从数据库中检索记录
public 游标 getAllRows() {
字符串 where = null;
// query the DBAdapter
Cursor cursor = db.query(true, TABLE_NAME, ALL_KEYS, where, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
现在将光标设置到您的适配器,适配器设置为列表视图
ListView listView = (ListView) findViewById(R.id.new_list_listView);
Cursor cursor = newListDBAdapter.getAllRows();
YourCursorAdapter arrayAdapter = new YourCursorAdapter(this, cursor);
listView.setAdapter(arrayAdapter );
您可以为此使用 SimpleCursorAdapter。
这里是一些例子 (see full example):
public static final String KEY_APPLES = "apples";
public static final String KEY_PRICE = "Price";
public static final String KEY_TYPE = "Type";
private void displayListView() {
Cursor cursor;
SimpleCursorAdapter dataAdapter; //android.support.v4.widget.SimpleCursorAdapter;
//Put your code to get cursor
// The desired columns to be bound
String[] columns = new String[]{
KEY_APPLES,
KEY_PRICE,
KEY_TYPE
};
// the XML defined views which the data will be bound to
int[] to = new int[]{
R.id.apples,
R.id.price,
R.id.type
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.country_info,
cursor,
columns,
to,
0);
ListView listView = (ListView) findViewById(R.id.listView);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
Helo 我正在尝试使用存储在 sqlite 中的数据填充 listView。
在我 select 一个产品之后,我希望该产品的所有参考
像我在图片中画的一样走同一条线。
我可以让 ArrayAdapter 把所有的 记录在同一 xml?
我的代码是这样的:returns所有记录的游标:
public Cursor getAllRows() {
String where = null;
// query the DBAdapter
Cursor cursor = db.query(true, TABLE_NAME, ALL_KEYS, where, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
正在向 arrayList 添加数据:
public ArrayList<String> fromCursorToArrayListString(Cursor c){
ArrayList<String> result = new ArrayList<String>();
c.moveToFirst();
for(int i = 0; i < c.getCount(); i++){
String row_PRODUCT = c.getString(c.getColumnIndex(KEY_PRODUCT));
String row_PRICE = c.getString(c.getColumnIndex(KEY_PRICE));
String row_TYPE = c.getString(c.getColumnIndex(KEY_TYPE));
result.add(row_PRODUCT);
result.add(row_PRICE);
result.add(row_TYPE);
c.moveToNext();
}
return result;
}
我在mainActivity中这样写:
ListView listView = (ListView) findViewById(R.id.new_list_listView);
Cursor cursor = newListDBAdapter.getAllRows();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.custom_product_layout_new_list,R.id.custom_product_layout_new_list_productName,newListDBAdapter.fromCursorToArrayListString(cursor));
listView.setAdapter(arrayAdapter);
您可以使用 SimpleAdapter :
ListView list = (ListView) findViewById(R.id.mylist);
ArrayList<HashMap<String, String>> mylistData =
new ArrayList<HashMap<String, String>>();
String[] columnTags = new String[] {"col1", "col2", "col3"};
int[] columnIds = new int[] {R.id.column1, R.id.column2, R.id.column3};
for(int i=0; i<3; i++)
{
HashMap<String,String> map = new HashMap<String, String>();
//initialize row data
for(int j=0; j<3; j++)
{
map.put(columnTags[j], "row”+i+”col"+j);
}
mylistData.add(map);
}
SimpleAdapter arrayAdapter =
new SimpleAdapter(this, mylistData, R.layout.mylistrow,
columnTags , columnIds);
list.setAdapter(arrayAdapter);
或使用自定义适配器
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
ListView l1;
String[] t1={"video1","video2"};
String[] d1={"lesson1","lesson2"};
int[] i1 ={R.drawable.ic_launcher,R.drawable.ic_launcher};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
l1=(ListView)findViewById(R.id.list);
l1.setAdapter(new dataListAdapter(t1,d1,i1));
}
class dataListAdapter extends BaseAdapter {
String[] Title, Detail;
int[] imge;
dataListAdapter() {
Title = null;
Detail = null;
imge=null;
}
public dataListAdapter(String[] text, String[] text1,int[] text3) {
Title = text;
Detail = text1;
imge = text3;
}
public int getCount() {
// TODO Auto-generated method stub
return Title.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.custom, parent, false);
TextView title, detail;
ImageView i1;
title = (TextView) row.findViewById(R.id.title);
detail = (TextView) row.findViewById(R.id.detail);
i1=(ImageView)row.findViewById(R.id.img);
title.setText(Title[position]);
detail.setText(Detail[position]);
i1.setImageResource(imge[position]);
return (row);
}
}
}
CursorAdapter 适配器对此更可靠。您只需将 Cursor 传递给适配器,这样您就无需维护任何集合。你必须遵循几件事
适配器
public class YourCursorAdapter extends CursorAdapter {
public YourCursorAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, 0);
}
// The newView method is used to inflate a new view and return it,
// you don't bind any data to the view at this point.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.your_item_view, parent, false);
}
// The bindView method is used to bind all data to a given view
// such as setting the text on a TextView.
@Override
public void bindView(View view, Context context, Cursor c) {
// Find fields to populate in inflated template
TextView tvproduct = (TextView) view.findViewById(R.id.tcproduct);
TextView tvPrice = (TextView) view.findViewById(R.id.tvPrice);
TextView tvType = (TextView) view.findViewById(R.id.tvType);
// Extract properties from cursor
String row_PRODUCT = c.getString(c.getColumnIndex(KEY_PRODUCT));
String row_PRICE = c.getString(c.getColumnIndex(KEY_PRICE));
String row_TYPE = c.getString(c.getColumnIndex(KEY_TYPE));
// Populate fields with extracted properties
tvproduct.setText(row_PRODUCT);
tvPrice.setText(row_PRICE);
tvType.setText(row_TYPE);
}
}
从数据库中检索记录
public 游标 getAllRows() { 字符串 where = null;
// query the DBAdapter
Cursor cursor = db.query(true, TABLE_NAME, ALL_KEYS, where, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
现在将光标设置到您的适配器,适配器设置为列表视图
ListView listView = (ListView) findViewById(R.id.new_list_listView);
Cursor cursor = newListDBAdapter.getAllRows();
YourCursorAdapter arrayAdapter = new YourCursorAdapter(this, cursor);
listView.setAdapter(arrayAdapter );
您可以为此使用 SimpleCursorAdapter。
这里是一些例子 (see full example):
public static final String KEY_APPLES = "apples";
public static final String KEY_PRICE = "Price";
public static final String KEY_TYPE = "Type";
private void displayListView() {
Cursor cursor;
SimpleCursorAdapter dataAdapter; //android.support.v4.widget.SimpleCursorAdapter;
//Put your code to get cursor
// The desired columns to be bound
String[] columns = new String[]{
KEY_APPLES,
KEY_PRICE,
KEY_TYPE
};
// the XML defined views which the data will be bound to
int[] to = new int[]{
R.id.apples,
R.id.price,
R.id.type
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.country_info,
cursor,
columns,
to,
0);
ListView listView = (ListView) findViewById(R.id.listView);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}