gpt4 book ai didi

java - 自定义 ArrayAdapter 中无法访问的语句

转载 作者:行者123 更新时间:2023-11-29 08:34:53 25 4
gpt4 key购买 nike

我的自定义 ArrayAdapter 中存在错误。 Android Studio 一直在 View listItemView = convertView; 下划线,并显示 unreachable statement

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;
}

}

它使用我的 Word 类新创建的对象:

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;
}

}

并从此 Activity 调用:

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);
}
}

最佳答案

你在方法声明之后直接有一个return语句,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;

该方法中的任何内容都没有被执行,您可能并不想放入第一个返回值。

关于java - 自定义 ArrayAdapter 中无法访问的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44915536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com