标题听起来有点令人困惑,但我正在从网站解析信息并将其存储在arraylist<string>
中。在我ArrayList<String> mPictures = new GetTeachers().execute("div.post_thumb_clean img", "src").get();
存储我正在使用的asynctask
的结果后,我会使用ArrayList<Teacher> = mTeachers
和mTeachers.add(new Teacher(Splash.mNames, Splash.mEmails, Splash.mPictures));
在我的TeacherAdapter
中实施。问题是每当我运行所有这些时,只有一个listitem
显示而不是11个左右。我认为问题发生在我用作listitem
的Teacher.class中。好吧,我不知道怎么说这一切,因为我自己学习,我不确定我的术语是否合适,但是我将在下面的每个文件中发布摘录。谢谢!
另外,对于代码的大量抱歉,请随时编辑它。
AsyncTask(GetTeachers.class)
public class GetTeachers extends AsyncTask<String, Void, ArrayList<String>> {
public static ArrayList<Teacher> mTeachers;
protected final ArrayList<String> mInfo = new ArrayList<String>();
@Override
protected ArrayList<String> doInBackground(String... param) {
try {
Document doc = Jsoup.connect("http://www.androidpolice.com/")
.timeout(10000).userAgent("Mozilla").get();
Elements elements = doc.select(param[0]);
for (Element element : elements) {
mInfo.add(element.absUrl(param[1]));
}
} catch (Exception ex) {
ex.printStackTrace();
}
return mInfo;
}
@Override
protected void onPostExecute(ArrayList<String> result) {
super.onPostExecute(result);
mTeachers = new ArrayList<Teacher>();
mTeachers.add(new Teacher(Splash.mNames, Splash.mEmails,
Splash.mPictures));
}
}
Teacher.class
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;
}
}
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;
}
}
除了Splash.class
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();
答案 0 :(得分:1)
问题在于AsyncTask的onPostExecute()
方法。
mTeachers = new ArrayList<Teacher>();
mTeachers.add(new Teacher(Splash.mNames, Splash.mEmails,
Splash.mPictures));
在这里,您初始化mTeachers
列表,但只添加一个Teacher
对象。我不确定Splash.mNames,Splash.mEmails和Splash.mPictures是什么。
现在,在适配器的getView()
方法中,Teacher teacher = mTeacher.get(position);
适用于位置zero
。由于mTeachers
的尺寸为one
,因此列表视图中只会显示一个项目。
修改1:
让我们对您的适配器进行一些更改。添加以上Typeface thin;
:
private ArrayList<String> mNames;
private ArrayList<String> mEmails;
private ArrayList<String> mPictures;
接下来,从以下位置更改构造函数:
public TeacherAdapter(Context ctx, int viewResourceId,
ArrayList<Teacher> teacher)
为:
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;
您的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));
目的是为TeacherAdapter提供3个ArrayList来代替ArrayList。设置适配器并更新它(adapter.notifyDataSetChanged())。