我想创建一个列表视图,其中包含可排序的图像和文本。我已经完成了使用图像和文本创建自定义列表视图。请注意,文本的值来自我的数据库。此外,我已经完成了对文本进行排序,但我无法对图像的位置进行排序。
你能给我一些考虑因素来实现文本和图像的分类吗?到目前为止,这是我尝试过的:
static int[] imgs = {
R.drawable.dinaretreat, // 0
R.drawable.cobterrace, // 1
R.drawable.ventassostreet, // 2
R.drawable.summerhillblvddrouin, // 3
R.drawable.todmanstreetdrouin
};
String[] text, loc, price;
private DBHelper dbHelper;
MyCustomAdapter adapter;
// TODO displayRecords
private void displayRecords() {
checkDatabaseConnection();
text = dbHelper.getAll();
price = dbHelper.getAllPrices();
loc = dbHelper.getAllLocations();
adapter = new MyCustomAdapter(imgs, text, loc, price);
lv.setAdapter(adapter);
}
class MyCustomAdapter extends BaseAdapter
{
String[] data_tvName;
String[] data_tvLocation;
String[] data_tvPrice;
int[] data_image;
MyCustomAdapter() {
data_tvName = null;
data_tvLocation = null;
data_tvPrice = null;
data_image = null;
}
MyCustomAdapter(int[] image, String[] house, String[] location, String[] price) {
data_tvName = house;
data_tvLocation = location;
data_tvPrice = price;
data_image = image;
}
public int getCount() {
return data_tvName.length;
}
public String getItem(int position) {
return null;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.listrow, null);
textview1 = (TextView) row.findViewById(R.id.tvName);
textview2 = (TextView) row.findViewById(R.id.tvLocation);
textview3 = (TextView) row.findViewById(R.id.tvPrice);
ImageView imageview = (ImageView) row.findViewById(R.id.image);
imageview.setScaleType(ImageView.ScaleType.FIT_XY);
textview1.setText(data_tvName[position]);
Spanned strFormattedLocation = Html.fromHtml("<b>Location: </b>");
Spanned strFormattedPrice = Html.fromHtml("<b>Price: </b>");
textview2.setText(strFormattedLocation + data_tvLocation[position]);
String strPrice = strFormattedPrice + "$" + (new DecimalFormat("#,###.00")).format(Integer.parseInt(data_tvPrice[position])) ;
textview3.setText(strPrice);
imageview.setImageResource(data_image[position]);
return (row);
}
}
DBHelper.class
public String[] sortLocationAsc(){
Cursor localCursor =
this.myDataBase.query(DB_TABLE, new String[] {
KEY_ID, KEY_HOUSE, KEY_PRICE, KEY_LOCATION },
null, null, null, null,
KEY_LOCATION + " ASC");
String[] array = new String[localCursor.getCount()];
int i = 0;
while(localCursor.moveToNext()){
String uname = localCursor.getString(localCursor.getColumnIndex(DBHelper.KEY_LOCATION));
array[i] = uname;
i++;
}
return array;
}
我不知道这有什么问题,或者我忘记了什么。非常感谢您的帮助。非常感谢。
答案 0 :(得分:1)
好的,这是一个如何实现目标的例子。
最好的方法是创建一个自定义对象来保存不同的字符串和每行的图像。
此对象可能如下所示:
public class MyCustomObject {
private int image;
private String house;
private String location;
private String price;
public MyCustomObject(int image, String house, String location, String price) {
this.image = image;
this.house = house;
this.location = location;
this.price = price;
}
public int getImage() {
return image;
}
public String getHouse() {
return house;
}
public String getLocation() {
return location;
}
public String getPrice() {
return price;
}
}
现在到DBHelper
班。您应该获取String[]
List
个,而不是为每种类型提取一个MyCustomObject
。我选择List
而不是数组,因为这些更容易处理,并且应该(在大多数情况下)使用而不是更原始的数组。 : - )
将DBHelper.sortLocationAsc()
重命名为getAllMyCustomObjects()
,看起来像这样:
public List<MyCustomObject> getAllMyCustomObjects() {
Cursor localCursor = this.myDataBase.query(DB_TABLE, new String[] {
KEY_ID, KEY_HOUSE, KEY_PRICE, KEY_LOCATION }, null, null,
null, null, KEY_LOCATION + " ASC");
String house = "";
String price = "";
String location = "";
int image = 0;
List<MyCustomObject> listOfObjects = new ArrayList<MyCustomObject>();
while (localCursor.moveToNext()) {
house = localCursor.getString(localCursor.getColumnIndex(DBHelper.KEY_HOUSAE));
price = localCursor.getString(localCursor.getColumnIndex(DBHelper.KEY_PRICE));
location = localCursor.getString(localCursor.getColumnIndex(DBHelper.KEY_LOCATION));
image = localCursor.getInt(localCursor.getColumnIndex(DBHelper.KEY_IMAGE));
listOfObjects.add(new MyCustomObject(image, house, location, price));
}
return listOfObjects;
}
现在,在创建自定义Adapter
时,我已经对其进行了一些更改,因此只需要List
MyCustomObject
个而不是数组。
您会在getView
方法中注意到我从List
的{{1}}获取了一个项,而不是从每个不同的数组中获取。通过这种方式,可以更轻松地控制哪些元素属于一起,并且如果不同的数组长度不同,您将无法获得奇怪的行为。
新的MyCustomObject
看起来像这样:
MyCustomAdapter
然后您将class MyCustomAdapter extends BaseAdapter {
List<MyCustomObject> myListOfCustomObjects;
MyCustomAdapter(List<MyCustomObject> customObjects) {
this.myListOfCustomObjects = customObjects;
}
public int getCount() {
return myListOfCustomObjects.size();
}
public String getItem(int position) {
return null;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.listrow, null);
textview1 = (TextView) row.findViewById(R.id.tvName);
textview2 = (TextView) row.findViewById(R.id.tvLocation);
textview3 = (TextView) row.findViewById(R.id.tvPrice);
ImageView imageview = (ImageView) row.findViewById(R.id.image);
imageview.setScaleType(ImageView.ScaleType.FIT_XY);
textview1.setText(myListOfCustomObjects.get(position).getHouse());
Spanned strFormattedLocation = Html.fromHtml("<b>Location: </b>");
Spanned strFormattedPrice = Html.fromHtml("<b>Price: </b>");
textview2.setText(strFormattedLocation + myListOfCustomObjects.get(position).getLocation());
String strPrice = strFormattedPrice
+ "$"
+ (new DecimalFormat("#,###.00")).format(Integer.parseInt(myListOfCustomObjects.get(position).getPrice()));
textview3.setText(strPrice);
imageview.setImageResource(myListOfCustomObjects.get(position).getImage());
return row;
}
}
中的适配器设置为:
displayRecords()
所以关键是创建一个自定义对象来保存数据。
上面的代码可能无法运行/编译 - 我只是在文本编辑器中编写它并且根本没有测试它,但现在你希望能够清楚地看到问题是如何形成的解决了: - )
另外:如果您的数据库中有大量数据,我建议您考虑在后台线程中填充private void displayRecords() {
checkDatabaseConnection();
List<MyCustomObject> list = dbHelper.getAllMyCustomObjects();
adapter = new MyCustomAdapter(list);
lv.setAdapter(adapter);
}
- 例如,您可以使用ListView
来执行此操作此
在调用[AsycnTask][1]
的{{1}}时,请查看ViewHolder
模式。