0

标题听起来有点混乱,但我正在从网站解析信息并将其存储在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 个左右。我认为问题出现在我用作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();
4

1 回答 1

1

问题onPostExecute()出在 AsyncTask 的方法中。

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);适用于 position zero。由于大小mTeachersone,因此您的列表视图中仅显示一项。

编辑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())。

于 2013-07-20T19:37:52.387 回答