arraylist - 将自定义列表实现到自定义 array 列表中所有从jsoup到自定义 array 适配器的问题

  显示原文与译文双语对照的内容
149 5

这个标题有点迷惑,但是我从一个网站解析信息并将它的存储在 arraylist<string> 中。一旦我得到ArrayList<String> mPictures = new GetTeachers().execute("div.post_thumb_clean img","src").get();要存储我正在使用的asynctask的结果,我使用ArrayList<Teacher> = mTeachersmTeachers.add(new Teacher(Splash.mNames, Splash.mEmails, Splash.mPictures));在我的TeacherAdapter 中实现。问题是,每当我运行所有这一切,只有一个 listitem 出现,而不是 11,这应该是。我认为问题发生在我的Teacher.class 中,我使用它作为 listitem我不确定自己学习了什么,我不确定是否有术语,但是我要从每个文件中摘录。感谢同样,对代码的crapload感谢,请随意编辑它。

AsyncTask ( getteachers 。类)

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();
时间:原作者:1个回答

111 1

问题是 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);works zero 。由于 mTeachers的大小是 one,所以你的listview中只显示一项。

编辑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的的目的。设置适配器并将它的更新为( 。适配器。notifydatasetchanged ( ) ) 。

原作者:
...