我使用下面的代码获取三个字符串和一个整数,以使用自定义数组适配器和自定义类在列表视图中创建文本视图
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;
}
}
活动文件
package com.shyam.android.tamillanguagelearningapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import java.util.ArrayList;
public class NumbersActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_list);
// createing string array to store words
//String[] words = new String[10];
// storing data
// words[0 ]= "One";
// words[1]= "Two";
// words[2]= "Three";
// words[3]= "Four";
// words[4]= "Five";
// words[5]= "Six";
// words[6]= "Seven";
// words[7]= "Eight";
// words[8]= "Nine";
// words[9]= "Ten";
// Log.v("NumbersActivity","the number in place of words 0 is = " + words[0]);
//Log.v("NumbersActivity","the number in place of words 1 is = " + words[1]);
// create an arrary list
ArrayList<Word> words = new ArrayList<Word>();
// add data to array lsit
// words.add("One");
words.add(new Word("Zero","சுழியம்","௦",0));
words.add(new Word("One","ஒன்று","௧",1));
words.add(new Word("Two","இரண்டு","௨",2));
words.add(new Word("Three","மூன்று","௩",3));
words.add(new Word("Four","நான்கு","௪",4));
words.add(new Word("Five","ஐந்து","௫",5));
words.add(new Word("Six","ஆறு","௬",6));
words.add(new Word("Seven","ஏழு","௭",7));
words.add(new Word("Eight","எட்டு","௮",8));
words.add(new Word("Nine","ஒன்பது","௯",9));
words.add(new Word("Ten","பத்து","௰",10));
//words.add("Two");
// words.add("Three");
//words.add("Four");
//words.add("Five");
// words.add("Six");
// words.add("Seven");
// words.add("Eight");
// words.add("Nine");
// words.add("Ten");
// Log.v("NumbersActivity"," The number in place of words 0 is = " + words.get(0));
// Log.v("NumbersActivity"," The number in place of words 6 is = " + words.get(5));
//Log.v("NumbersActivity"," The number in place of words 10 is = " + words.get(9));
// /creating thre view child and add data and displaing it and adding to the paretn
// LinearLayout rootView = (LinearLayout)findViewById(R.id.rootView);
//TextView wordview = new TextView(this);
//wordview.setText(words.get(0));
// rootView.addView(wordview);
//using loops to do the above code
// int index = 0;
// while (index<words.size()){
// create anew lik text view that display the wordview
//and the view as a child to the rootview
// TextView wordview = new TextView(this);
//wordview.setText(words.get(index));
// rootView.addView(wordview);
//update the counter variable
// index = index + 1;
// using for loop to do the above code
//for(int index = 0 ; index < words.size(); index ++){
// create the text view
//TextView wordview = new TextView(this);
//set the text to be word at the current index
// wordview.setText(words.get(index));
// add theis textview as another child tothe root view of this layout
//rootView.addView(wordview);
//}
WordAdapter adapter =
new WordAdapter(this,words);
ListView listView =(ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
}
它不适合我。请指导我。
答案 0 :(得分:0)
更改
englishNumber.setText(currentWord.getmEnglishNumber());
到
englishNumber.setText(currentWord.getmEnglishNumber()+"");
的TextView#的setText
public final void setText(@StringRes int resid) {
setText(getContext().getResources().getText(resid));
}