私がやってきたことはcustomadapterについてです。リストビューから選択したアイテムを取得したいのですが、アダプターはカスタムです。私のリストビューには、画像、タイトル、キャプションがあります。選択したアイテムを取得したいのですが、私の場合はタイトルだけです。それ、どうやったら出来るの?
これが私がこれまでに試したことです:
@Override
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
String strHouseName = "house_name";
String strHousePrice = "house_price";
//String selectedFromList =(String) (lv.getItemAtPosition(pos));
Intent i;
switch(pos){
case 0:
i = new Intent(this, ViewHouse.class);
i.putExtra(strHouseName, "5 Dina Retreat, Carrum Downs");
i.putExtra(strHousePrice, "$300,000.00");
startActivity(i);
break;
case 1:
i = new Intent(this, ViewHouse.class);
i.putExtra(strHouseName, "Lot 941 Cob Terrace, Clyde North");
i.putExtra(strHousePrice, "$800,000.00");
startActivity(i);
break;
case 2:
i = new Intent(this, ViewHouse.class);
i.putExtra(strHouseName, "Lot 932 Ventasso Street, Clyde");
i.putExtra(strHousePrice, "$1,000,000.00");
startActivity(i);
break;
case 3:
i = new Intent(this, ViewHouse.class);
i.putExtra(strHouseName, "Lot 515 Summerhill Blvd, Drouin");
i.putExtra(strHousePrice, "$300,000.00");
startActivity(i);
break;
case 4:
i = new Intent(this, ViewHouse.class);
i.putExtra(strHouseName, "Lot 17 Todman Street, Drouin (Option 2)");
i.putExtra(strHousePrice, "$550,000.00");
startActivity(i);
break;
case 5:
i = new Intent(this, ViewHouse.class);
i.putExtra(strHouseName, "Lot 32 Aqueduct Road, Langwarrin");
i.putExtra(strHousePrice, "$600,000.00");
startActivity(i);
break;
case 6:
i = new Intent(this, ViewHouse.class);
i.putExtra(strHouseName, "Units 2-7, 269 North Road, Langwarin");
i.putExtra(strHousePrice, "$450,000.00");
startActivity(i);
break;
case 7:
i = new Intent(this, ViewHouse.class);
i.putExtra(strHouseName, "U 39, 40-60 Potts Road, Langwarrin");
i.putExtra(strHousePrice, "$1,300,000.00");
startActivity(i);
break;
case 8:
i = new Intent(this, ViewHouse.class);
i.putExtra(strHouseName, "Lot 1 McClenaghan Place, Pakenham");
i.putExtra(strHousePrice, "$2,000,000.00");
startActivity(i);
break;
case 9:
i = new Intent(this, ViewHouse.class);
i.putExtra(strHouseName, "Lot 2 McClenaghan Place, Pakenham");
i.putExtra(strHousePrice, "$300,000.00");
startActivity(i);
break;
case 10:
i = new Intent(this, ViewHouse.class);
i.putExtra(strHouseName, "Lot 3 McClenaghan Place, Pakenham");
i.putExtra(strHousePrice, "$900,000.00");
startActivity(i);
break;
case 11:
i = new Intent(this, ViewHouse.class);
i.putExtra(strHouseName, "Lot 4 McClenaghan Place, Pakenham");
i.putExtra(strHousePrice, "$600,000.00");
startActivity(i);
break;
case 12:
i = new Intent(this, ViewHouse.class);
i.putExtra(strHouseName, "Lot 5 McClenaghan Place, Pakenham");
i.putExtra(strHousePrice, "$1,200,000.00");
startActivity(i);
break;
case 13:
i = new Intent(this, ViewHouse.class);
i.putExtra(strHouseName, "Lot 7 McClenaghan Place, Pakenham");
i.putExtra(strHousePrice, "$700,000.00");
startActivity(i);
break;
case 14:
i = new Intent(this, ViewHouse.class);
i.putExtra(strHouseName, "Lot 11 McClenaghan Place, Pakenham");
i.putExtra(strHousePrice, "$500,000.00");
startActivity(i);
break;
case 15:
i = new Intent(this, ViewHouse.class);
i.putExtra(strHouseName, "Lot 12 McClenaghan Place, Pakenham");
i.putExtra(strHousePrice, "$3,000,000.00");
startActivity(i);
break;
case 16:
i = new Intent(this, ViewHouse.class);
i.putExtra(strHouseName, "Lot 532 Summerhill Blvd, Drouin");
i.putExtra(strHousePrice, "$300,000.00");
startActivity(i);
break;
case 17:
i = new Intent(this, ViewHouse.class);
i.putExtra(strHouseName, "Lot 17 Ajax Street, Drouin");
i.putExtra(strHousePrice, "$300,000.00");
startActivity(i);
break;
case 18:
i = new Intent(this, ViewHouse.class);
i.putExtra(strHouseName, "Lot 133 Mountainview Blvd, Cranbourne North");
i.putExtra(strHousePrice, "$300,000.00");
startActivity(i);
break;
case 19:
i = new Intent(this, ViewHouse.class);
i.putExtra(strHouseName, "Lot 531 Summerhill Blvd, Drouin");
i.putExtra(strHousePrice, "$300,000.00");
startActivity(i);
break;
default: Log.d(strHouseName, "No such house name available");
}
}
class MyCustomAdapter extends BaseAdapter
{
String[] data_text1;
String[] data_text2;
int[] data_image;
MyCustomAdapter() {
data_text1 = null;
data_text2 = null;
data_image = null;
}
MyCustomAdapter(int[] image, String[] house, String[] price) {
data_text1 = house;
data_text2 = price;
data_image = image;
}
public int getCount() {
return data_text1.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);
TextView textview1 = (TextView) row.findViewById(R.id.text1);
TextView textview2 = (TextView) row.findViewById(R.id.text2);
ImageView imageview = (ImageView) row.findViewById(R.id.image);
imageview.setScaleType(ImageView.ScaleType.FIT_XY);
textview1.setText(data_text1[position]);
textview2.setText("$" + (new DecimalFormat("#,###.00")).format(Double.parseDouble(data_text2[position])) );
imageview.setImageResource(data_image[position]);
return (row);
}
}
とりあえず、その位置から取得するだけですが、問題は、リストがソートされている場合はどうなるかということです。正しい選択項目を取得するにはどうすればよいですか? 何か案は?よろしくお願いします。ありがとう。