Android : DataModel cannot be cast to java.lang.String

on Sunday, October 12, 2014


Here is the MainActivity.java including only a couple of the if/else blocks:



// Simple array with a list of languages
String[] languages = {"Arabic", "Armenian", "Cantonese", "Cherokee",
"Czech", "Dutch", "English", "French", "Gaelic", "German",
"Greek", "Haitian Creole", "Hawaiian", "Italian", "Japanese", "Korean",
"Mandarin", "Navajo", "Philippines Tagalog", "Polish", "Portuguese", "Romanian",
"Russian", "Spanish", "Swedish", "Vietnamese"};

// Simple array with a list of the spelling for Merry Christmas in language
final String[] merrychristmas = {"Eid Melad Majeed", "Shnorhavor Surb Tsnund",
"Sing-daan Faai-lohk", "ulihelisdi hunadetiyisgvi", "veselé vánoce",
"vrolijk kerstfeest", "Merry Christmas!", "joyeux Noël", "Nollaig Shona Duit",
"frohe Weihnacten", "Kalá Christoúgenna", "Jwaye Nwèl", "Mele Kalikimaka",
"Buon Natale", "Merīkurisumasu", "jilgohun-christmas-bonoseyo",
"Shèndàn kuàilè", "Nizhónígo Késhmish Da'doohleel",
"Mąligayang Pasko", "Wesołych Świąt", "Feliz Natal", "Crăcuin fericit",
"schastlivogo Rozhdestva", "Feliz Navidad", "god jul", "Chúc Mừng Giáng Sinh"};

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

rowItems = new ArrayList<DataModel>();
for (int i = 0; i < languages.length; i++) {
DataModel item = new DataModel(languages[i]);
rowItems.add(item);
}

listView = (ListView) findViewById(R.id.listView1);
CustomAdapter adapter = new CustomAdapter(this, R.layout.item_row, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);

}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
LinearLayout layout = new LinearLayout(MainActivity.this);
layout.setBackgroundResource(R.drawable.circle);
TextView tv = new TextView(MainActivity.this);
// set the TextView properties
tv.setTextColor(Color.WHITE);
tv.setTextSize(20);
tv.setPaddingRelative(22, 108, 0, 0);
tv.setGravity(Gravity.FILL);
tv.setText(merrychristmas[position]);
layout.addView(tv);
Toast toast = new Toast(MainActivity.this);

if (position == 0) {
Toast.makeText(getApplicationContext(),
merrychristmas[position], Toast.LENGTH_LONG);
toast.setView(layout);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
_player = MediaPlayer.create(MainActivity.this, R.raw.arabic);
_player.start();
}
if (position == 1) {
Toast.makeText(getApplicationContext(),
merrychristmas[position], Toast.LENGTH_LONG);
// set the layout as the toast view
toast.setView(layout);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
_player = MediaPlayer.create(MainActivity.this, R.raw.armenian);
_player.start();
}
if (position == 2) {
Toast.makeText(getApplicationContext(),
merrychristmas[position], Toast.LENGTH_LONG);


And the CustomAdapter.java: public class CustomAdapter extends ArrayAdapter {



Context context;

/****** custom adapter constructor ******/
public CustomAdapter(Context context, int resourceId, List<DataModel> data) {
super(context, resourceId, data);
this.context = context;
}

/****** create a holder class to contain inflated xml file elements ******/

private class ViewHolder {
public TextView txtlanguage;
}

/**
* *** create each ListView row *****
*/
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder = null;
DataModel rowItem = getItem(position);

LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

if (convertView == null) {
/****** Inflate item_row file for each row ******/
convertView = mInflater.inflate(R.layout.item_row, null);
/****** ViewHolder object to contain item_row.xml file elements ******/
holder = new ViewHolder();
holder.txtlanguage = (TextView) convertView.findViewById(R.id.text1);
/****** set holder with LayoutInflater ******/
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();

/****** set model values in Holder elements ******/
holder.txtlanguage.setText(rowItem.getLanguage());
/****** set itemClickListener for LayoutInflater for each row ******/
return convertView;
}
}


For DataModel.java: public class DataModel {



private String language;

public DataModel(String language) {
this.language = language;
}

/****** get methods ******/
public String getLanguage() {
return this.language;
}
/****** set methods ******/
public void setLanguage(String language) {
this.language = language;
}

} // end class DataModel


Here's the crash dump: 10-12 08:53:01.135 1134-1134/com.blogspot.joyouslybeingjoy.merrychristmas W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb3a86ba8) 10-12 08:53:01.155 1134-1134/com.blogspot.joyouslybeingjoy.merrychristmas E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.blogspot.joyouslybeingjoy.merrychristmas, PID: 1134 java.lang.ClassCastException: com.blogspot.joyouslybeingjoy.merrychristmas. DataModel cannot be cast to java.lang.String at com.blogspot.joyouslybeingjoy.merrychristmas.MainActivity. onItemClick(MainActivity.java:102) at android.widget.AdapterView.performItemClick(AdapterView.java:299) at android.widget.AbsListView.performItemClick(AbsListView.java:1113) at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904) at android.widget.AbsListView$3.run(AbsListView.java:3638) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)


Thanks in advance! I sure am learning a lot and having fun doing it! Appreciate the support!


0 comments:

Post a Comment