Android 无法为自定义阵列适配器充气
我使用下面的代码获得三个字符串和一个整数,以使用自定义数组适配器和自定义类在列表视图中创建文本视图Android 无法为自定义阵列适配器充气,android,listview,time,Android,Listview,Time,我使用下面的代码获得三个字符串和一个整数,以使用自定义数组适配器和自定义类在列表视图中创建文本视图 package com.shyam.android.tamillanguagelearningapp; /** * Created by MANI on 12/8/2016. */ // it caontains two tralatio words as // declaration of the class public cla
package com.shyam.android.tamillanguagelearningapp;
/**
* Created by MANI on 12/8/2016.
*/
// it caontains two tralatio words as
// declaration of the class
public class Word {
/** english translation*/
// state of word class
private String mEnglishTranslation;
/** tamil translation*/
private String mTamilTranslation;
/** tamil number*/
private String mTamilNumber;
/** english number*/
private int mEnglishNumber;
// constrauctor to tke two words
public Word(String englishTranslation,String tamilTranslation ,String tamilNumber,int englishNumber){
mEnglishTranslation = englishTranslation;
mTamilTranslation = tamilTranslation;
mTamilNumber = tamilNumber;
mEnglishNumber = englishNumber;
}
// methods of the class
public String getmEnglishTranslation(){
return mEnglishTranslation;
}
public String getmTamilTranslation(){
return mTamilTranslation;
}
public String getmTamilNumber(){
return mTamilNumber;
}
public int getmEnglishNumber(){
return mEnglishNumber;
}
}
end****
//custome array adaptor file
package com.shyam.android.tamillanguagelearningapp;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by MANI on 12/8/2016.
*/
public class WordAdapter extends ArrayAdapter<Word> {
/**
* This is our own custom constructor (it doesn't mirror a superclass constructor).
* The context is used to inflate the layout file, and the list is the data we want
* to populate into the lists.
*
//* @param context The current context. Used to inflate the layout file.
//* @param words A List of AndroidFlavor objects to display in a list
*/
public WordAdapter(Activity context, ArrayList<Word> words) {
// Here, we initialize the ArrayAdapter's internal storage for the context and the list.
// the second argument is used when the ArrayAdapter is populating a single TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
// going to use this second argument, so it can be any value. Here, we used 0.
super(context, 0, words);
}
@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);
}
// Get the {@link AndroidFlavor} object located at this position in the list
Word currentWord = getItem(position);
// Find the TextView in the list_item.xml layout with the ID version_name
TextView tamilTextView = (TextView) listItemView.findViewById(R.id.tamil_text_view);
// Get the version name from the current AndroidFlavor object and
// set this text on the name TextView
tamilTextView.setText(currentWord.getmTamilTranslation());
// Find the TextView in the list_item.xml layout with the ID version_number
TextView englsihTextView = (TextView) listItemView.findViewById(R.id.english_text_view);
// Get the version number from the current AndroidFlavor object and
// set this text on the number TextView
englsihTextView.setText(currentWord.getmEnglishTranslation());
// Find the TextView in the list_item.xml layout with the ID version_number
TextView tamilNumber = (TextView) listItemView.findViewById(R.id.tamil_number_text_view);
// Get the version number from the current AndroidFlavor object and
// set this text on the number TextView
tamilNumber.setText(currentWord.getmTamilNumber());
// Find the TextView in the list_item.xml layout with the ID version_number
TextView englishNumber = (TextView) listItemView.findViewById(R.id.english_number_text_view);
// Get the version number from the current AndroidFlavor object and
// set this text on the number TextView
englishNumber.setText(currentWord.getmEnglishNumber());
// Find the ImageView in the list_item.xml layout with the ID list_item_icon
// ImageView iconView = (ImageView) listItemView.findViewById(R.id.list_item_icon);
// Get the image resource ID from the current AndroidFlavor object and
// set the image to iconView
// iconView.setImageResource(currentAndroidFlavor.getImageResourceId());
// Return the whole list item layout (containing 2 TextViews and an ImageView)
// so that it can be shown in the ListView
return listItemView;
}
}
活动文件
这对我不起作用。请在这方面指导我。改变
englishNumber.setText(currentWord.getmEnglishNumber());
进入
文本视图设置文本
public final void setText(@StringRes int resid) {
setText(getContext().getResources().getText(resid));
}
使用通用适配器感谢它对我有用,谢谢你。我将继续我的应用程序开发。再次感谢你。
englishNumber.setText(currentWord.getmEnglishNumber()+"");
public final void setText(@StringRes int resid) {
setText(getContext().getResources().getText(resid));
}