Android 从jsoup将自定义列表实现为自定义数组列表并实现为自定义数组适配器时出现问题
标题听起来有点混乱,但我正在解析来自网站的信息并将其存储在Android 从jsoup将自定义列表实现为自定义数组列表并实现为自定义数组适配器时出现问题,android,arraylist,android-listview,jsoup,listadapter,Android,Arraylist,Android Listview,Jsoup,Listadapter,标题听起来有点混乱,但我正在解析来自网站的信息并将其存储在arraylist中。一旦我得到ArrayList mPictures=new GetTeachers().execute(“div.post\u thumb\u clean img”,“src”).get()为了存储我正在使用的异步任务的结果,我使用ArrayList=mTeachers和mTeachers.add(new Teacher(Splash.mNames,Splash.mEmails,Splash.mPictures))在m
arraylist
中。一旦我得到ArrayList mPictures=new GetTeachers().execute(“div.post\u thumb\u clean img”,“src”).get()
为了存储我正在使用的异步任务的结果,我使用ArrayList=mTeachers
和mTeachers.add(new Teacher(Splash.mNames,Splash.mEmails,Splash.mPictures))代码>在myTeacherAdapter
中实现。问题是,每当我运行所有这些时,只会显示一个listitem
,而不是应该显示的11个左右。我想问题发生在我的Teacher.class上,我用它作为列表项。嗯,我不知道如何表达所有这些,因为我自己在学习,我不确定我是否有正确的术语,但我将在下面发布每个必要文件的摘录。谢谢
另外,很抱歉代码太多,请随意编辑
AsyncTask(GetTeachers.class)
public类GetTeachers扩展异步任务{
公共静态数组列表mTeachers;
受保护的最终ArrayList mInfo=新ArrayList();
@凌驾
受保护的ArrayList doInBackground(字符串…参数){
试一试{
Document doc=Jsoup.connect(“http://www.androidpolice.com/")
.timeout(10000).userAgent(“Mozilla”).get();
元素=单据选择(参数[0]);
for(元素:元素){
mInfo.add(element.absUrl(param[1]);
}
}捕获(例外情况除外){
例如printStackTrace();
}
返回mInfo;
}
@凌驾
受保护的void onPostExecute(ArrayList结果){
super.onPostExecute(结果);
mTeachers=newarraylist();
mTeachers.add(新教师)(Splash.mNames、Splash.mEmails、,
溅起的水花;
}
}
班主任
public class Teacher {
String mName, mEmail, mPicture;
public Teacher() {
}
public Teacher(ArrayList<String> n, ArrayList<String> e, ArrayList<String> p) {
StringBuilder sbn = new StringBuilder();
for (String mN : n) {
sbn.append(mN);
mName = mN;
}
StringBuilder sbe = new StringBuilder();
for (String mE : e) {
sbe.append(mE);
mEmail = mE;
}
StringBuilder sbp = new StringBuilder();
for (String mP : p) {
sbp.append(mP);
mPicture = mP;
}
}
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
}
public String getEmail() {
return mEmail;
}
public void setEmail(String email) {
mEmail = email;
}
public String getPicture() {
return mPicture;
}
public void setPicture(String picture) {
mPicture = picture;
}
}
公共课堂教师{
字符串mName、mEmail、mPicture;
公共教师(){
}
公立教师(ArrayList n、ArrayList e、ArrayList p){
StringBuilder sbn=新的StringBuilder();
for(字符串mN:n){
附加(mN);
mName=mN;
}
StringBuilder sbe=新的StringBuilder();
for(字符串mE:e){
附加(我);
梅梅尔=我;
}
StringBuilder sbp=新的StringBuilder();
for(字符串mP:p){
sbp追加(mP);
mPicture=mP;
}
}
公共字符串getName(){
返回mName;
}
公共void集合名(字符串名){
mName=名称;
}
公共字符串getEmail(){
返回mEmail;
}
公用电子邮件(字符串电子邮件){
mEmail=电子邮件;
}
公共字符串getPicture(){
返回结构;
}
公共无效设置图片(字符串图片){
M结构=图片;
}
}
TeacherAdapter.class:
public class TeacherAdapter extends ArrayAdapter<Teacher> {
Typeface thin;
private LayoutInflater mInflater;
private ArrayList<Teacher> mTeacher;
private int mViewResourceId;
public TeacherAdapter(Context ctx, int viewResourceId,
ArrayList<Teacher> teacher) {
super(ctx, viewResourceId, teacher);
mInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
thin = Typeface.createFromAsset(ctx.getAssets(), "RobotoThin.ttf");
mViewResourceId = viewResourceId;
mTeacher = teacher;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
Teacher teacher = mTeacher.get(position);
TextView teachername = (TextView) convertView
.findViewById(R.id.teacher_name);
TextView teacheremail = (TextView) convertView
.findViewById(R.id.teacher_email);
ImageView iv = (ImageView) convertView
.findViewById(R.id.teacher_picture);
teachername.setTypeface(thin);
teacheremail.setTypeface(thin);
AQuery aq = new AQuery(getContext());
AQUtility.setDebug(false);
teachername.setText(teacher.getName());
teacheremail.setText(teacher.getEmail());
aq.id(iv).image(teacher.getPicture(), false, true, 64, R.drawable.ic_contact_picture);
return convertView;
}
}
公共类TeacherAdapter扩展了ArrayAdapter{
字体薄;
私人停车场;
私人ArrayList mTeacher;
私有int-mViewResourceId;
公共TeacherAdapter(上下文ctx,int-viewResourceId,
ArrayList(教师){
超级(ctx、viewResourceId、教师);
mInflater=(LayoutInflater)ctx
.getSystemService(上下文布局\充气机\服务);
thin=Typeface.createFromAsset(ctx.getAssets(),“robototothin.ttf”);
mViewResourceId=viewsourceid;
老师=老师;
}
@凌驾
公共长getItemId(int位置){
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
convertView=mInflater.inflate(mViewResourceId,null);
教师=mTeacher.get(位置);
TextView教师姓名=(TextView)convertView
.findviewbyd(R.id.teacher\u name);
TextView教师电子邮件=(TextView)convertView
.findviewbyd(R.id.teacher_电子邮件);
ImageView iv=(ImageView)convertView
.findviewbyd(R.id.teacher\u图片);
教师姓名。设置字体(细);
teacheremail.setTypeface(瘦);
AQuery aq=新的AQuery(getContext());
AQUtility.setDebug(false);
teachername.setText(teacher.getName());
teacheremail.setText(teacher.getEmail());
aq.id(iv).image(teacher.getPicture(),false,true,64,R.drawable.ic\u contact\u picture);
返回视图;
}
}
除了飞溅类
public static ArrayList<String> mNames, mEmails, mPictures = new ArrayList<String>();
...
mPictures = new ArrayList<String>();
mNames = new GetTeachers().execute("h3 a", "href").get();
mEmails = new GetTeachers().execute("h3 a", "href").get();
mPictures = new GetTeachers().execute("div.post_thumb_clean img",
"src").get();
publicstaticarraylistmnames,mEmails,mPictures=newarraylist();
...
mpactures=newarraylist();
mNames=new GetTeachers().execute(“h3 a”,“href”).get();
mEmails=new GetTeachers().execute(“h3 a”,“href”).get();
mpactures=new GetTeachers().execute(“div.post\u thumb\u clean img”,
“src”).get();
问题出在AsyncTask的onPostExecute()
方法中
mTeachers = new ArrayList<Teacher>();
mTeachers.add(new Teacher(Splash.mNames, Splash.mEmails,
Splash.mPictures));
接下来,将构造函数更改为:
public TeacherAdapter(Context ctx, int viewResourceId,
ArrayList<Teacher> teacher)
您的getView()
将具有以下内容:
convertView = mInflater.inflate(mViewResourceId, null);
TextView teachername = (TextView) convertView
.findViewById(R.id.teacher_name);
TextView teacheremail = (TextView) convertView
.findViewById(R.id.teacher_email);
ImageView iv = (ImageView) convertView
.findViewById(R.id.teacher_picture);
teachername.setTypeface(thin);
teacheremail.setTypeface(thin);
AQuery aq = new AQuery(getContext());
AQUtility.setDebug(false);
teachername.setText(mNames.get(position));
teacheremail.setText(mEmails.get(position));
aq.id(iv).image(mPictures.get(position), false, true, 64, R.drawable.ic_contact_picture);
return convertView;
从onPostExecute()
方法中删除以下行:
mTeachers = new ArrayList<Teacher>();
mTeachers.add(new Teacher(Splash.mNames, Splash.mEmails,
Splash.mPictures));
mTeachers=newarraylist();
mTeachers.add(新教师)(Splash.mNames、Splash.mEmails、,
溅起的水花;
用于为教师适配器提供3 ArrayList代替ArrayList。设置并更新适配器(adapter.notifyDataSetChanged())。Splash.mNames
、Splash.mEmails
和Splash.mPictures
是我在启动屏幕中使用AsyncTask创建的ArrayList
。我认为添加我创建的ArrayList会自动增加mTeachers
的大小,因为ArrayList
@wizkid95中有多个项,我可以看到您正在填充m
public TeacherAdapter(Context ctx, int viewResourceId,
ArrayList<String> names, ArrayList<String> emails, ArrayList<String> pictures)
super();
mInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
thin = Typeface.createFromAsset(ctx.getAssets(), "RobotoThin.ttf");
mViewResourceId = viewResourceId;
mNames = names;
mEmails = emails;
mPictures = pictures;
convertView = mInflater.inflate(mViewResourceId, null);
TextView teachername = (TextView) convertView
.findViewById(R.id.teacher_name);
TextView teacheremail = (TextView) convertView
.findViewById(R.id.teacher_email);
ImageView iv = (ImageView) convertView
.findViewById(R.id.teacher_picture);
teachername.setTypeface(thin);
teacheremail.setTypeface(thin);
AQuery aq = new AQuery(getContext());
AQUtility.setDebug(false);
teachername.setText(mNames.get(position));
teacheremail.setText(mEmails.get(position));
aq.id(iv).image(mPictures.get(position), false, true, 64, R.drawable.ic_contact_picture);
return convertView;
mTeachers = new ArrayList<Teacher>();
mTeachers.add(new Teacher(Splash.mNames, Splash.mEmails,
Splash.mPictures));