我已经使用BaseAdapter创建了一个自定义ListView。当我开始活动时,我看到一个没有条目的空ListView。
这是Adapter
班级:
public class SpeiseplanListAdapter extends BaseAdapter {
private ArrayList<SpeiseplanEntry> data;
private Context context;
public SpeiseplanListAdapter(ArrayList<SpeiseplanEntry> data, Context context) {
this.data = data;
this.context = context;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View entry = inflater.inflate(R.layout.layout_list_entry, parent, false);
TextView day = (TextView) entry.findViewById(R.id.text_day);
TextView date = (TextView) entry.findViewById(R.id.text_date);
TextView firstDish = (TextView) entry.findViewById(R.id.text_first_dish);
TextView secondDish = (TextView) entry.findViewById(R.id.text_second_dish);
SpeiseplanEntry current = data.get(position);
day.setText(current.getDay());
date.setText(current.getDate());
firstDish.setText(current.getFirstDish());
secondDish.setText(current.getSecondDish());
return entry;
}
}
OnCreate()
方法:
protected void onCreate(Bundle savedInstanceState) {
...
ListView listView = (ListView) findViewById(R.id.list_speiseplan);
SpeiseplanListAdapter adapter = new SpeiseplanListAdapter(entries, getApplicationContext());
listView.setAdapter(adapter);
...
}
单行布局:
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="120dp">
<TextView
android:text="MO"
android:layout_width="wrap_content"
android:layout_height="35dp" android:id="@+id/text_day" android:fontFamily="monospace"
android:typeface="monospace" android:textSize="30sp"
android:textColor="@color/colorPrimary"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="24dp" android:layout_marginLeft="28dp"
app:layout_constraintLeft_toLeftOf="parent" android:layout_marginStart="28dp"/>
<TextView
android:layout_width="250dp"
android:layout_height="38dp"
android:id="@+id/text_first_dish"
app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="16dp"
app:layout_constraintLeft_toRightOf="@+id/text_day" android:layout_marginLeft="40dp"
android:textSize="16sp" android:textAlignment="textStart"
android:text="firstDish"
android:gravity="center_vertical"
android:layout_marginStart="40dp"/>
<TextView
android:layout_width="250dp"
android:layout_height="38dp"
android:id="@+id/text_second_dish"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="16dp"
app:layout_constraintLeft_toRightOf="@+id/text_day" android:layout_marginLeft="40dp"
android:textSize="16sp" android:text="secondDish"
android:gravity="center_vertical"
android:layout_marginStart="40dp"/>
<TextView
android:text="21.09."
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/text_date"
android:layout_marginLeft="16dp" app:layout_constraintLeft_toLeftOf="parent" android:textSize="18sp"
android:fontFamily="monospace" android:typeface="monospace" android:textColor="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="32dp"
android:layout_marginStart="16dp"/>
Activity
的布局:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.gymoth.goapp.SpeiseplanActivity" tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<ListView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/list_speiseplan"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
android:layout_marginStart="0dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="0dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp" app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.0" android:divider="@color/divider" android:dividerHeight="1dp"/>
我用于单行的ConstraintLayout
是否有问题?或Adapter
问题是什么? 感谢任何帮助。
答案 0 :(得分:0)
像这样使用
protected void onCreate(Bundle savedInstanceState) {
...
ListView listView = (ListView) findViewById(R.id.list_speiseplan);
SpeiseplanListAdapter adapter = new SpeiseplanListAdapter(entries, YourActivityName.this);
listView.setAdapter(adapter);
...
}
答案 1 :(得分:0)
首先在充气时加上条件......
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View entry = inflater.inflate(R.layout.layout_list_entry, parent, false);
}
然后在适配器中使用支架..