以下MainActivity.java
仅包含几个if/else
块:
// 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);
CustomAdapter.java:
public class CustomAdapter extends ArrayAdapter<DataModel> {
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;
}
}
对于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
这里是崩溃转储:
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)
提前致谢!我确实学到了很多,并且乐在其中!感谢支持!
答案 0 :(得分:0)
是CustomAdapter extends ArrayAdapter
。
使用CustomAdapter extends ArrayAdapter<DataModel>
这是我想的问题,请发布你的102行。
答案 1 :(得分:0)
CustomAdapter extends ArrayAdapter
装置
CustomAdapter extends ArrayAdapter <Object>
这样做,它会起作用
CustomAdapter extends ArrayAdapter <DataModel>
答案 2 :(得分:0)
错误发生在第102行。我移动了那个和其他一些注释掉的行在同一区域中(实际上是文件的底部)并且我确保CustomAdapter扩展了ArrayAdapter到位,做了一个干净的它正在工作!我现在将所有注释掉的行移到NotePad ++上!非常感谢。真的很喜欢从这个板上学习。