dimanche 19 avril 2015

String cannot be cast to Object

I have a class in which i declare this:



public class Comune implements Serializable {
public String id;
public String name;

public Comune(String id, String name) {
this.id = id;
this.name = name;
}

@Override
public String toString() {
return name;
}

}


then another class like:



public class Comuni {

public static final Comune[] comuni = {
new Comune("A093|AGUGLIARO|1","AGUGLIARO"),
new Comune("A154|ALBETTONE|1","ALBETTONE"),
---
---
};
}


then i have a fragment with 2 spinners and a button. When i click in the spinner (is an autocompletetextview where appears the spinner in there) it shows the list of "Comune" and i can choose the item i want. When i have the two values of the two autocompletetextview and click in the button i pass in the second screen passing with a putExtra the 2 values of the spinners like this:



Intent intent = new Intent(getActivity(), SceltaFermateActivity.class);
intent.putExtra(SceltaFermateActivity.EXTRA_COMUNE_PARTENZA, itemPartenze);
intent.putExtra(SceltaFermateActivity.EXTRA_COMUNE_ARRIVO, itemArrivi);
startActivity(intent);


I tried to implement an sqlite in which i am able to save the 2 values of the spinner in a listview so i can show the "last result". To retrieve the values from the listview i use the cursor like:



lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Cursor cursor = (Cursor) parent.getItemAtPosition(position);
String item_partenza = cursor.getString(cursor.getColumnIndex(mdb.KEY_PARTENZA));
String item_arrivo = cursor.getString(cursor.getColumnIndex(mdb.KEY_ARRIVO));


/*String item = String.valueOf(item_id) + " : " + item_partenza +"\n"+item_arrivo;
Toast.makeText(getActivity(), item, Toast.LENGTH_LONG).show();*/
}
});


and it works! The problem is that i would on click on the listview item pass the two values directly so on Click it will open the second screen. But if i pass



Intent intent = new Intent(getActivity(), SceltaFermateActivity.class);
intent.putExtra(SceltaFermateActivity.EXTRA_COMUNE_PARTENZA, item_partenza);
intent.putExtra(SceltaFermateActivity.EXTRA_COMUNE_ARRIVO, item_arrivo);
startActivity(intent);


it crashes because it says: String cannot be cast solutions? Maybe a way to put the strings in the AutoCompleteTextView when i click on the item?


Aucun commentaire:

Enregistrer un commentaire