public class NumbersActivity extends AppCompatActivity {
private MediaPlayer mMediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_list);
ArrayList<Words> numbers = new ArrayList<Words>();
numbers.add(new Words("one","lutti",R.drawable.number_one));
numbers.add(new Words("two","otiiko",R.drawable.number_two));
numbers.add(new Words("three","tolookosu",R.drawable.number_three));
numbers.add(new Words("four","oyyisa",R.drawable.number_four));
numbers.add(new Words("five","massokka",R.drawable.number_five));
numbers.add(new Words("six","temmokka",R.drawable.number_six));
numbers.add(new Words("seven","kenekaku",R.drawable.number_seven));
numbers.add(new Words("eight","kawinta",R.drawable.number_eight));
numbers.add(new Words("nine","wo’e",R.drawable.number_nine));
numbers.add(new Words("ten","na’aacha",R.drawable.number_ten));
WordAdapter adapter = new WordAdapter(this, numbers, R.color.category_numbers);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast display = Toast.makeText(NumbersActivity.this,"it is some text",Toast.LENGTH_LONG);
display.show();
}
});
}
}的ListView與setItem onClickListner方法
代碼是沒有錯誤,但我沒能獲得敬酒消息,請單擊不應用內工作,我們應該修改或額外添加任何現有的代碼
下面是有關WordAdapter類的代碼
public class WordAdapter extends ArrayAdapter<Words> {
/** Resource ID for the background color for this list of words */
private int mColorResourceId;
/**
* Create a new {@link WordAdapter} object.
*
* @param context is the current context (i.e. Activity) that the adapter is being created in.
* @param words is the list of {@link Words}s to be displayed.
* @param colorResourceId is the resource ID for the background color for this list of words
*/
public WordAdapter(Context context, ArrayList<Words> words, int colorResourceId) {
super(context, 0, words);
mColorResourceId = colorResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Words wordsTranslation = getItem(position);
// Find the TextView in the list_item.xml layout with the ID version_name
TextView englishTextView = (TextView) listItemView.findViewById(R.id.englishWord);
englishTextView.setText(wordsTranslation.getDefaultEnglish());
TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwokWord);
miwokTextView.setText(wordsTranslation.getTranslatedMiwok());
// Find the ImageView in the list_item.xml layout with the ID image.
ImageView sideImage = (ImageView) listItemView.findViewById(R.id.imageView);
// Check if an image is provided for this word or not
if (wordsTranslation.hasImage()) {
// If an image is available, display the provided image based on the resource ID
sideImage.setImageResource(wordsTranslation.getImage());
// Make sure the view is visible
sideImage.setVisibility(View.VISIBLE);
} else {
sideImage.setVisibility(View.GONE);
}
View textContainer = listItemView.findViewById(R.id.text_container);
int color = ContextCompat.getColor(getContext(), mColorResourceId);
textContainer.setBackgroundColor(color);
return listItemView;
}
}
發表您的Android佈局 –
如果你嘗試了所有的答案,人民給了你,不工作..嘗試清理你的項目,卸載你的設備中的應用程序,並再次編譯(併發布你的android佈局順便說一句) – Robert