ListView出现问题 [英] Trouble with ListView
问题描述
我的主要活动和ListView出现问题,我绝对不了解它的工作原理...
I have an issue with my Main Activity and a ListView and I absolutly don't understand how it works...
好!这就是我想要的:
期望
这意味着我需要一个项目列表:TextViews
It means I need a list of item : TextViews
此列表的外侧还有一个简单的按钮.我不希望该按钮成为列表的一部分.
And OUTSIDE of this list a simple button. I don't want the button to be part of the list.
但是,这就是我所拥有的:
But here is what I have :
现实
如您所见,在我列表的每个孩子中,我都有我的文字和我的按钮:为什么?
As you can see, in every child of my list i have my text AND my button : Why ??
我的代码Main:
public class MainMenu extends AppCompatActivity implements IRemDeck, IRemCard {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
ListView listView = findViewById(R.id.MainDecksList);
AdapterDeck adpDeck;
ArrayList<Deck> myDecks = new ArrayList<>();
for (int i = 0; i<5;i++){
myDecks.add(new Deck("Deck " + i));
}
adpDeck = new AdapterDeck(this, 0, myDecks);
listView.setAdapter(adpDeck);
}
我的适配器(用于ListView):
My Adapter (for the ListView) :
public AdapterDeck(@NonNull Context context, int resource, @NonNull ArrayList<Deck> decks) {
super(context, resource, decks);
this.decks = decks;
this.context = context;
}
@Override
public int getCount() {
return decks.size();
}
@Override
public Deck getItem(int pos) {
return decks.get(pos);
}
@Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable.
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.activity_main_menu, null);
}
//Handle TextView and display string from your list
TextView listItemText = view.findViewById(R.id.MainDeckNom);
listItemText.setText(decks.get(position).getNom());
return view;
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainMenu">
<TextView
android:id="@+id/MainDeckNom"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/MainDecksList"
android:layout_width="363dp"
android:layout_height="426dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/MainDecksList" />
</android.support.constraint.ConstraintLayout>
如果有人可以向我解释如何解决我的问题(以及最终如何解决),我将不胜感激!
If someone could explain to me how to solve my problem (and how it works eventually) I would be very grateful !
非常感谢:)
推荐答案
listview必须具有自己的布局,因此您需要创建一个仅包含listview的新布局,并在getView()
方法中将其用作您的一行listView
listview has to have its own layout, so you need to create a new layout that just contains listview and in getView()
method use it as a row of your listView
1-创建一个新的row_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainMenu">
<TextView
android:id="@+id/MainDeckNom"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
2-更改此行
view = inflater.inflate(R.layout.activity_main_menu, null);
到
view = inflater.inflate(R.layout.row_listview, null);
这篇关于ListView出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!