I want to create a listview with image and text that is sortable. I'm done creating the custom listview with image and text. Note that the values of the texts come from my database. Also I'm done with sorting the text but I can't sort the position of image.
Can you give me some considerations to achieve the sorting of both text and image? So far this is what I have tried:
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;
}
I don't know what's wrong with this or have I forgotten something. Your help is much appreciated. Thanks a lot.
int image, String house, String location, String price. I'll try to write an answer a little later today, to illustrate how you can do it.