标题听起来有点混乱,但我正在从网站解析信息并将其存储在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
. 好吧,我不确定如何表达所有这些,因为我是自己学习的,我不确定我的术语是否正确,但我将在下面发布每个必要文件的摘录。谢谢!另外,很抱歉代码的垃圾,请随时编辑它。
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));
}
}
教师班
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();