我有三个文件的Word Adapter.java是Array适配器的子类和list_item.xml这是我的列表项目布局和word_list.java这是我的列表创建的地方并为它设置了一个适配器,最后是word_list.xml,这是列表的充气位置。我从github复制了AndroidFlavourAdapter代码,并对其进行了修改,以适应我的适配器。请帮助,我是Android新手,我已经完成了它甚至在不同的建议中检查了不同的帖子,但是非正常工作,当我点击word_list.xml活动时,应用程序只会崩溃,这是要显示列表的地方。对于这个没有组织的帖子,我也很抱歉,对于stackoverflow也是新的。谢谢。自定义ListView不适用于适配器(android)
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"
/>
什么错误它给你?把日志跟踪请 – joao86
您的ListView xml看起来很尴尬。 – RonTLV
@faisalsaah:发布您的错误 – Jeevanandhan