具有多个 TextView 的自定义 ListView 适配器每行都会重复
我使用自定义 ListView 在每行 3 个 TextView 上显示 3 个不同的字符串。 它应该是这样的:
Customer 1
Type 1
Date 1
-------
Customer 2
Type 2
Date 2
-------
Customer 3
...
问题是它显示每一行包含最后一个客户的信息。我已经检查了传递给 CustomAdapter 的信息并且是正确的,而且我尝试了不同的方法来实现这一点,但没有任何效果,并且由于我不熟悉 Android Studio,我无法真正找到到底出了什么问题。
MainActivity.class
public class MainActivity extends AppCompatActivity {
ArrayList<InfoModel> listDisplay = new ArrayList<InfoModel>();
InfoModel data = new InfoModel();
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.customerslist);
// This here just takes the data from a local database and puts it into the ArrayList. I've checked it and the data passed to the adapter is correct
DataBaseHelper dataBaseHelper = new DataBaseHelper(MainActivity.this);
List<HolidaysModel> list = dataBaseHelper.getAllTable("HOLIDAYS_TABLE");
List<CustomerModel> list2 = dataBaseHelper.getAllTable("CUSTOMER_TABLE");
for (HolidaysModel holidays: list) {
data.setType(holidays.getType());
data.setDates(new StringBuilder().append(holidays.getDateStart()).append(" - ").append(holidays.getDateFinish()).toString());
for (CustomerModel customer: listCustomer) {
if(customer.get_id() == (Integer) holidays.getId()) {
data.setName(new StringBuilder().append(customer.getName()).append(" ").append(customer.getSurname()).toString());
listDisplay.add(data);
break;
}
}
}
CustomAdapter arrayAdapter = new CustomAdapter(MainActivity.this, listDisplay);
listView.setAdapter(arrayAdapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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=".activities.MainActivity">
<ListView
android:id="@+id/customerslist"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
CustomAdapter.class
public class CustomAdapter extends BaseAdapter {
private final Context context;
private ArrayList<InfoModel> items = new ArrayList<InfoModel>();
public CustomAdapter(Context context, ArrayList<InfoModel> items) {
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public InfoVacacionesModel getItem(int i) {
return items.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
private class ViewHolder{
TextView tv_name;
TextView tv_type;
TextView tv_dates;
}
@Override
public View getView(final int position, View oneLine, ViewGroup parent) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
ViewHolder holder;
if(oneLine == null) {
oneLine = inflater.inflate(R.layout.custom_list, parent, false);
holder = new ViewHolder();
holder.tv_name = (TextView) oneLine.findViewById(R.id.tv_name);
holder.tv_type = (TextView) oneLine.findViewById(R.id.tv_type);
holder.tv_dates = (TextView) oneLine.findViewById(R.id.tv_dates);
oneLine.setTag(holder);
}
else
holder = (ViewHolder) oneLine.getTag();
holder.tv_name.setText(items.get(position).getName());
holder.tv_type.setText(items.get(position).getType());
holder.tv_dates.setText(items.get(position).getDates());
return oneLine;
}
}
custom_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="5dp"
android:layout_height="match_parent">
<TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:id="@+id/tv_name"
android:textStyle="bold"
android:textSize="20sp">
</TextView>
<TextView
android:layout_weight="1"
android:layout_width="70dp"
android:layout_height="20dp"
android:id="@+id/tv_type"
android:textSize="15sp">
</TextView>
<TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:id="@+id/tv_dates"
android:textSize="15sp">
</TextView>
</LinearLayout>
I am using a Custom ListView to show 3 different strings on 3 TextViews every row.
It should go like:
Customer 1
Type 1
Date 1
-------
Customer 2
Type 2
Date 2
-------
Customer 3
...
The problem is it shows every single row with the information from the last customer. I have checked the information passed to the CustomAdapter and is correct plus I tried different methods to implement this but none worked and as I'm not familiar with Android Studio I can't really find what's wrong exactly.
MainActivity.class
public class MainActivity extends AppCompatActivity {
ArrayList<InfoModel> listDisplay = new ArrayList<InfoModel>();
InfoModel data = new InfoModel();
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.customerslist);
// This here just takes the data from a local database and puts it into the ArrayList. I've checked it and the data passed to the adapter is correct
DataBaseHelper dataBaseHelper = new DataBaseHelper(MainActivity.this);
List<HolidaysModel> list = dataBaseHelper.getAllTable("HOLIDAYS_TABLE");
List<CustomerModel> list2 = dataBaseHelper.getAllTable("CUSTOMER_TABLE");
for (HolidaysModel holidays: list) {
data.setType(holidays.getType());
data.setDates(new StringBuilder().append(holidays.getDateStart()).append(" - ").append(holidays.getDateFinish()).toString());
for (CustomerModel customer: listCustomer) {
if(customer.get_id() == (Integer) holidays.getId()) {
data.setName(new StringBuilder().append(customer.getName()).append(" ").append(customer.getSurname()).toString());
listDisplay.add(data);
break;
}
}
}
CustomAdapter arrayAdapter = new CustomAdapter(MainActivity.this, listDisplay);
listView.setAdapter(arrayAdapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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=".activities.MainActivity">
<ListView
android:id="@+id/customerslist"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
CustomAdapter.class
public class CustomAdapter extends BaseAdapter {
private final Context context;
private ArrayList<InfoModel> items = new ArrayList<InfoModel>();
public CustomAdapter(Context context, ArrayList<InfoModel> items) {
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public InfoVacacionesModel getItem(int i) {
return items.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
private class ViewHolder{
TextView tv_name;
TextView tv_type;
TextView tv_dates;
}
@Override
public View getView(final int position, View oneLine, ViewGroup parent) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
ViewHolder holder;
if(oneLine == null) {
oneLine = inflater.inflate(R.layout.custom_list, parent, false);
holder = new ViewHolder();
holder.tv_name = (TextView) oneLine.findViewById(R.id.tv_name);
holder.tv_type = (TextView) oneLine.findViewById(R.id.tv_type);
holder.tv_dates = (TextView) oneLine.findViewById(R.id.tv_dates);
oneLine.setTag(holder);
}
else
holder = (ViewHolder) oneLine.getTag();
holder.tv_name.setText(items.get(position).getName());
holder.tv_type.setText(items.get(position).getType());
holder.tv_dates.setText(items.get(position).getDates());
return oneLine;
}
}
custom_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="5dp"
android:layout_height="match_parent">
<TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:id="@+id/tv_name"
android:textStyle="bold"
android:textSize="20sp">
</TextView>
<TextView
android:layout_weight="1"
android:layout_width="70dp"
android:layout_height="20dp"
android:id="@+id/tv_type"
android:textSize="15sp">
</TextView>
<TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:id="@+id/tv_dates"
android:textSize="15sp">
</TextView>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 MainActivity 中声明 InfoModel 对象:
In MainActivity declare InfoModel objects within the for: