2017-09-04 7 views
0

私は3つのファイルを持っています.Word Adapter.javaはArrayアダプタのサブクラスであり、list_item.xmlは私のリストアイテムのレイアウトであり、word_list.javaは私のリストが作成される場所ですそれにアダプターがセットされていて、最後にword_list.xmlがあり、リストが膨らんでいます。私はgithubのAndroidFlavourAdapterコードをコピーして、それを自分のアダプターに合わせて修正しました。助けてください、Im new to android、以上、さらに別の提案でこのサイトの別の投稿をチェックしましたが、非は機能していますが、リストが表示される場所であるword_list.xmlアクティビティをクリックするとアプリがクラッシュするだけです。そして、私が持っているこのunorganizedポストのために残念、またstackoverflowに新しい。ありがとうございました。カスタムリストはアダプタ(アンドロイド)で動作しません

public class WordAdapter extends ArrayAdapter<Words> { 
 

 

 
    /** 
 
    * This is a 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 currentWord A List of both english and hausa objects to display in a list 
 
    */ 
 

 
    public WordAdapter(Activity context, ArrayList<Words> currentWord) { 
 
     // 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 , currentWord); 
 
    } 
 

 

 

 
    @NonNull 
 
    /** 
 
    * Provides a view for an AdapterView (ListView, GridView, etc.) 
 
    * 
 
    * @param position The position in the list of data that should be displayed in the 
 
    *     list item view. 
 
    * @param convertView The recycled view to populate. 
 
    * @param parent The parent ViewGroup that is used for inflation. 
 
    * @return The View for the position in the AdapterView. 
 
    */ 
 
    @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.word_list,parent,false); 
 
     } 
 

 

 

 
     // Get the {@link word} object located at this position in the list 
 
     Words currentWord = getItem(position); 
 

 
     // Find the TextView in the list_item.xml layout with the ID version_name 
 
     TextView hausaTextView = (TextView) listItemView.findViewById(R.id.haus_view); 
 
     // Find the TextView in the list_item.xml layout with the ID version_number 
 
     TextView englishTextView = (TextView) listItemView.findViewById(R.id.englis_view); 
 
     // Get the version name from the current AndroidFlavor object and 
 
     // set this text on the name TextView 
 
     hausaTextView.setText(currentWord.getHausa()); 
 

 
     // Get the version number from the current AndroidFlavor object and 
 
     // set this text on the number TextView 
 
     englishTextView.setText(currentWord.getDefault()); 
 

 

 
     // Return the whole list item layout (containing 2 TextViews and an ImageView) 
 
     // so that it can be shown in the ListView 
 
     return listItemView; 
 
    } 
 
}

public class word_list extends AppCompatActivity { 
 

 
    // list of words for both english and hausa 
 
    ArrayList<Words> hausaList = new ArrayList<Words>(); 
 

 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.word_list); 
 
     // add words to the arrayList object hausaList with constructor that has both english and hausa words as parameters 
 
     hausaList.add(new Words("one", "daya")); 
 
     hausaList.add(new Words("two", "biyu")); 
 
     hausaList.add(new Words("three", "uku")); 
 
     hausaList.add(new Words("four", "hudu")); 
 
     hausaList.add(new Words("five", "biyar")); 
 
     hausaList.add(new Words("six", "shida")); 
 
     hausaList.add(new Words("seven", "bakwai")); 
 
     hausaList.add(new Words("eight", "takwas")); 
 
     hausaList.add(new Words("nine", "tara")); 
 
     hausaList.add(new Words("ten", "goma")); 
 
     hausaList.add(new Words("eleven", "sha daya")); 
 
     hausaList.add(new Words("twelve", "sha biyu")); 
 

 
     ListAdapter myAdapter = new WordAdapter(this, hausaList); 
 
     ListView myListView = (ListView) findViewById(R.id.rootView); 
 
     myListView.setAdapter(myAdapter); 
 

 
    } 
 
}

<?xml version="1.0" encoding="utf-8"?> 
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:id="@+id/list_itemz" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:orientation="vertical" 
 
    android:padding="16dp"> 
 

 

 
    <TextView 
 
     android:id="@+id/haus_view" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     tools:text="daya" /> 
 

 
    <TextView 
 
     android:id="@+id/englis_view" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     tools:text="one" /> 
 

 
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?> 
 
<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:orientation="vertical" 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:paddingTop="16dp" 
 
    android:paddingBottom="16dp" 
 
    android:paddingRight="16dp" 
 
    android:paddingLeft="16dp" 
 
    android:id="@+id/rootView" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    />
あなたが実際にあなたのリストの各行に使用するレイアウトを含むXMLファイルを膨らま に必要WordAdapterクラスのあなたのGetViewメソッドで

+0

それはあなたをどのようなエラーを与えるのでしょうか?ログトレースを入れてください – joao86

+0

あなたのListView xmlは厄介なようです。 – RonTLV

+0

@faisalsaah:エラーを投稿する – Jeevanandhan

答えて

0

は、この行を変更してください:

listItemView = LayoutInflater.from(getContext()).inflate(R.layout.word_list,parent,false); 

で:

listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false); 
+0

神。私がどれほど幸せであるかを伝えることはできません。それは私を夢中にしています。 – faisalsash

関連する問題