gpt4 book ai didi

android - 自定义适配器和 ListView 崩溃

转载 作者:行者123 更新时间:2023-12-03 16:59:23 24 4
gpt4 key购买 nike

我创建了一个 java 类,然后想创建一个自定义适配器,然后最终填充一个列表。我按照指示完成了适配器,但是当我运行应用程序时,它一直在崩溃。由于我只是一个初学者,我不知道从哪里开始调试过程,所以有人可以帮我找出错误吗?

我的 Java 类:

package com.example.android.obgynconference2020;

public class MySessions {

private int imageResourceId;
private String sessionName;
private String doctorName;
private String hall;
private String time;


public MySessions(int imageResourceId, String sessionName, String doctorName,String hall, String time) {
this.imageResourceId = imageResourceId;
this.sessionName = sessionName;
this.doctorName = doctorName;
this.time = time;
}

public String getHall() {
return hall;
}

public void setHall(String hall) {
this.hall = hall;
}

public int getImageResourceId() {
return imageResourceId;
}

public void setImageResourceId(int imageResourceId) {
this.imageResourceId = imageResourceId;
}

public String getSessionName() {
return sessionName;
}

public void setSessionName(String sessionName) {
this.sessionName = sessionName;
}

public String getDoctorName() {
return doctorName;
}

public void setDoctorName(String doctorName) {
this.doctorName = doctorName;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}
}

我的适配器:
package com.example.android.obgynconference2020;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class SessionAdapter extends ArrayAdapter<MySessions> {

private Context mContext;
int mResouce;

public SessionAdapter(Context context, int resource, ArrayList<MySessions> objects) {
super(context, resource, objects);
}


@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {

View listItemView = convertView;

if (listItemView == null) {

listItemView =LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);

}

MySessions currentSession = getItem(position);

ImageView imageView = (ImageView)listItemView.findViewById(R.id.doctorImage);
imageView.setImageResource(currentSession.getImageResourceId());

TextView sessionTextView = (TextView)convertView.findViewById(R.id.sessionTextView);
sessionTextView.setText(currentSession.getSessionName());

TextView doctorTextView = (TextView)convertView.findViewById(R.id.doctorTextView);
doctorTextView.setText(currentSession.getDoctorName());

TextView hallTextView = (TextView)convertView.findViewById(R.id.hallTextView);
hallTextView.setText(currentSession.getHall());

TextView timeTextView = (TextView)convertView.findViewById(R.id.timeTextView);
timeTextView.setText(currentSession.getTime());

return listItemView;
}
}

我的 Activity :
package com.example.android.obgynconference2020;

import android.se.omapi.Session;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Adapter;
import android.widget.ListView;

import java.util.ArrayList;

public class DayOneActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);

ArrayList<MySessions> sessionsList = new ArrayList<>();

sessionsList.add(new MySessions(R.drawable.ic_launcher_background,"Session One", "Doctor One", "Hall A", "9AM - 10AM"));
sessionsList.add(new MySessions(R.drawable.ic_launcher_background,"Session Two", "Doctor Two", "Hall B", "9AM - 10AM"));
sessionsList.add(new MySessions(R.drawable.ic_launcher_background,"Session Three", "Doctor Three", "Hall C", "9AM - 10AM"));
sessionsList.add(new MySessions(R.drawable.ic_launcher_background,"Session Four", "Doctor Four", "Hall A", "10AM - 11AM"));
sessionsList.add(new MySessions(R.drawable.ic_launcher_background,"Session Five", "Doctor Five", "Hall B", "10AM - 11AM"));
sessionsList.add(new MySessions(R.drawable.ic_launcher_background,"Session Six", "Doctor Six", "Hall C", "10AM - 11AM"));
sessionsList.add(new MySessions(R.drawable.ic_launcher_background,"Session Seven", "Doctor Seven", "Hall A", "11AM - 12AM"));
sessionsList.add(new MySessions(R.drawable.ic_launcher_background,"Session Eight", "Doctor Eight", "Hall B", "11AM - 12AM"));
sessionsList.add(new MySessions(R.drawable.ic_launcher_background,"Session Nine", "Doctor Nine", "Hall C", "11AM - 12AM"));
sessionsList.add(new MySessions(R.drawable.ic_launcher_background,"Session Ten", "Doctor Ten", "Hall A", "12AM - 1PM"));
sessionsList.add(new MySessions(R.drawable.ic_launcher_background,"Session Eleven", "Doctor Eleven", "Hall B", "12AM - 1PM"));
sessionsList.add(new MySessions(R.drawable.ic_launcher_background,"Session Twelve", "Doctor Twelve", "Hall C", "12AM - 1PM"));


SessionAdapter sessionAdapter = new SessionAdapter(this, R.layout.list_view, sessionsList);

ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(sessionAdapter);
}
}

我的 list XML:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/list"/>

我的 list_item XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="4"
android:paddingTop="8dp"
android:paddingLeft="8dp">

<ImageView
android:layout_width="88dp"
android:layout_height="88dp"
android:src="@mipmap/ic_launcher"
android:id="@+id/doctorImage"/>

<LinearLayout
android:layout_width="0dp"
android:layout_height="88dp"
android:orientation="vertical"
android:layout_weight="3"
android:gravity="center">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Session Name"
android:id="@+id/sessionTextView"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Doctor Name"
android:id="@+id/doctorTextView"/>
</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="88dp"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="center_horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="88dp"
android:text="hall"
android:id="@+id/hallTextView" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time"
android:id="@+id/timeTextView" />

</LinearLayout>

</LinearLayout>

最佳答案

在 SessionAdapter 类的 getView 方法中替换 转换查看 列表项查看

public View getView(int position, View convertView, ViewGroup parent) {

View listItemView = convertView;

if (listItemView == null) {

listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);

}

MySessions currentSession = getItem(position);


ImageView imageView = (ImageView)listItemView.findViewById(R.id.doctorImage);
imageView.setImageResource(currentSession.getImageResourceId());


TextView sessionTextView = (TextView)listItemView.findViewById(R.id.sessionTextView);
sessionTextView.setText(currentSession.getSessionName());


TextView doctorTextView = (TextView)listItemView.findViewById(R.id.doctorTextView);
doctorTextView.setText(currentSession.getDoctorName());


TextView hallTextView = (TextView)listItemView.findViewById(R.id.hallTextView);
hallTextView.setText(currentSession.getHall());


TextView timeTextView = (TextView)listItemView.findViewById(R.id.timeTextView);
timeTextView.setText(currentSession.getTime());

return listItemView;

}

关于android - 自定义适配器和 ListView 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58224484/

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