2017-07-05 5 views
0

私のカスタムアレイアダプタにはバグがありますが、なぜAndroidスタジオがこのコードの第28行に下線を引いているのかわかりません。 "View listItemView = convertView;" ...到達不能の文カスタムアレイアダプタのバグ:o/

package com.example.android.miwok; 

import android.app.Activity; 
import android.support.annotation.NonNull; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 

import java.util.ArrayList; 

public class WordAdapter extends ArrayAdapter<Word> { 

    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){ 
      return super.getView(position, convertView, 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 defaultTextView = (TextView) listItemView.findViewById(R.id.default_text_view); 
      // Get the version name from the current AndroidFlavor object and 
      // set this text on the name TextView 
      defaultTextView.setText(currentWord.getDefaultTranslation()); 

      // Find the TextView in the list_item.xml layout with the ID version_number 
      TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view); 
      // Get the version number from the current AndroidFlavor object and 
      // set this text on the number TextView 
      miwokTextView.setText(currentWord.getMiwokTranslation()); 

      // 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.example.android.miwok; 

public class Word { 

    /** Default translation for the word */ 
    private String mDefaultTranslation; 

    /** Miwok translation for the word */ 
    private String mMiwokTranslation; 

    /** 
    * Create a new Word object. 
    * 
    * @param defaultTranslation is the word in a language that the user is already familiar with 
    *       (such as English) 
    * @param miwokTranslation is the word in the Miwok language 
    */ 
    public Word(String defaultTranslation, String miwokTranslation) { 
     mDefaultTranslation = defaultTranslation; 
     mMiwokTranslation = miwokTranslation; 
    } 

    /** 
    * Get the default translation of the word. 
    */ 
    public String getDefaultTranslation() { 
     return mDefaultTranslation; 
    } 

    /** 
    * Get the Miwok translation of the word. 
    */ 
    public String getMiwokTranslation() { 
     return mMiwokTranslation; 
    } 

} 

をし、この活動から呼び出されます。

package com.example.android.miwok; 

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.activity_numbers); 

     // Create a list of words 
     ArrayList<Word> words = new ArrayList<Word>(); 
     words.add(new Word("one", "lutti")); 
     words.add(new Word("two", "otiiko")); 
     words.add(new Word("three", "tolookosu")); 
     words.add(new Word("four", "oyyisa")); 
     words.add(new Word("five", "massokka")); 
     words.add(new Word("six", "temmokka")); 
     words.add(new Word("seven", "kenekaku")); 
     words.add(new Word("eight", "kawinta")); 
     words.add(new Word("nine", "wo’e")); 
     words.add(new Word("ten", "na’aacha")); 

     // Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The 
     // adapter knows how to create list items for each item in the list. 
     WordAdapter adapter = new WordAdapter(this, words); 

     // Find the {@link ListView} object in the view hierarchy of the {@link Activity}. 
     // There should be a {@link ListView} with the view ID called list, which is declared in the 
     // activity_numbers.xml layout file. 
     ListView listView = (ListView) findViewById(R.id.list); 

     // Make the {@link ListView} use the {@link WordAdapter} we created above, so that the 
     // {@link ListView} will display list items for each {@link Word} in the list. 
     listView.setAdapter(adapter); 
    } 
} 

はそれらがすべての必要なファイルです願っています。 提案ありがとうございました

+1

?実行可能スニペットのスクリプト要素としてのコードへのリンク?どのようにコードを見るはずですか? –

+0

sry、im here here ...しかし、Run code snippetをクリックすると、コード全体がかなり良く見えます;) –

+1

ええ、私は外部サイトにリンクするスニペットを実行しようとしています。 –

答えて

2

メソッド宣言の直後にreturn文がありますが、返された後は何も起こりません。

@Override 
public View getView(int position, View convertView, ViewGroup parent){ 
    //You might want to put your code here 
    return super.getView(position, convertView, parent); 
    // Nothing can happen beyond this point 

    // Check if the existing view is being reused, otherwise inflate the view 
    View listItemView = convertView; 

この方法では何も実行されていません。おそらく、最初の復帰をするつもりはありません。

+0

ところで、 'return'や' throw new Exception() '文を超えるコード行には到達できません。 – Kramer

0

これは動作します...本当に

public class WordAdapter extends ArrayAdapter<Word> { 

    /** 
    * 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 Word}s to be displayed. 
    * @param //colorResourceId is the resource ID for the background color for this list of words 
    */ 
    public WordAdapter(Activity context, ArrayList<Word> words) { 
     super(context, 0, words); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // Check if an 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 defaultTextView = (TextView) listItemView.findViewById(R.id.default_text_view); 
      // Get the version name from the current AndroidFlavor object and 
      // set this text on the name TextView 
      defaultTextView.setText(currentWord.getDefaultTranslation()); 
+0

返品を削除する –

関連する問題