未从 AsyncTask / onPostExecute 调用来自重载 ArrayAdapter 的 getView

发布于 2024-12-02 11:01:04 字数 3596 浏览 0 评论 0原文

您好,这是我的第一篇文章,也是我的第一个适用于 Android 手机的更高级的应用程序...我知道这里提到了这个主题,谷歌知道很多例子,但是它们并不完全是我所看到的形式;(

我承认我我是一个 n00000b...就 Android 编码而言...我开始...就在几天前,请原谅我:)

问题是我的 listview 元素没有被填充。应用程序编译并运行没有错误,但它不支持 getView。根据我在网上读到的内容...我需要这个来显示我心爱的列表视图及其内容...

请帮助:)

    package com.test.stackParser;

    public class StackParserActivity extends Activity {
/** Called when the activity is first created. */
private ProgressDialog pd;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //final TextView tv = (TextView) findViewById(R.id.textView1);
    //tv.setMovementMethod(new ScrollingMovementMethod());

    Button button = (Button)findViewById(R.id.button1);
    button.setOnClickListener(myListener);
}

private OnClickListener myListener = new OnClickListener() {
    public void onClick(View v) {
        pd = ProgressDialog.show(StackParserActivity.this, "Working...", "request to server", true, false);
        new ParseSite().execute("http://multikino.pl/pl/repertuar/warszawa-ursynow/2011-09-02/");
    }
};

private class ParseSite extends AsyncTask<String, Void, List<String>> {

    protected List<String> doInBackground(String... arg) {
        List<String> output = new ArrayList<String>();

        try
        {
            HtmlHelper hh = new HtmlHelper(new URL(arg[0]));
            List<TagNode> links = hh.getLinksByClass("title");

            for (Iterator<TagNode> iterator = links.iterator(); iterator.hasNext();)
            {
                TagNode divElement = (TagNode) iterator.next();
                output.add(divElement.getText().toString());
            }
            Log.d("dupa", "siteParsed");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return output;
    }

    protected void onPostExecute(List<String> output) {

        pd.dismiss();
        Log.d("dupa", "postExecute");
        ListView listview = (ListView) findViewById(R.id.listViewData);
        //listview.setAdapter(new ArrayAdapter<String>(StackParserActivity.this, android.R.layout.simple_list_item_1 , output));
        MyAdapter dupa = new MyAdapter(StackParserActivity.this, android.R.layout.simple_list_item_1, R.id.movieTitle, output); 
        dupa.notifyDataSetChanged();
        listview.setAdapter(dupa);
        dupa.notifyDataSetChanged();

    }
}

private class MyAdapter extends ArrayAdapter<string> {

    String[] tabObj;

    public MyAdapter(Context context, int resource, int textViewResourceId, List<String> output) {
        super(context, resource, textViewResourceId);
        tabObj = output.toArray(new String[]{});

        Log.d("dupa", tabObj[2].toString());
        notifyDataSetChanged();
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Log.d("dupa", "!!!!getView");

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.row_layout,parent,false);
        TextView tv = (TextView) row.findViewById(R.id.movieTitle);         

        tv.setText(tabObj[position]);

        //Button buton1 = (Button) row.findViewById(R.id.buttonInfo);
        //Button button2 = (Button) row.findViewById(R.id.buttonReserve);

        return super.getView(position, convertView, parent);
    }
};

}

Hi its my first post here nad my first a little bit more advanced app for my Android phone... I know that this subject was mentioned here, google knows many examples, however they are not exacty what im looking form ;(

I admit I'm a n00000b... in terms of Android coding... I started... ust few days ago, please forgive me :)

The problem is that my listview element is not beeing populated. App compiles and runs without errors, BUT it doesnt lunch getView. From what i have read on the web... I need this to display my beloved listview with its content...

Please help :)

    package com.test.stackParser;

    public class StackParserActivity extends Activity {
/** Called when the activity is first created. */
private ProgressDialog pd;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //final TextView tv = (TextView) findViewById(R.id.textView1);
    //tv.setMovementMethod(new ScrollingMovementMethod());

    Button button = (Button)findViewById(R.id.button1);
    button.setOnClickListener(myListener);
}

private OnClickListener myListener = new OnClickListener() {
    public void onClick(View v) {
        pd = ProgressDialog.show(StackParserActivity.this, "Working...", "request to server", true, false);
        new ParseSite().execute("http://multikino.pl/pl/repertuar/warszawa-ursynow/2011-09-02/");
    }
};

private class ParseSite extends AsyncTask<String, Void, List<String>> {

    protected List<String> doInBackground(String... arg) {
        List<String> output = new ArrayList<String>();

        try
        {
            HtmlHelper hh = new HtmlHelper(new URL(arg[0]));
            List<TagNode> links = hh.getLinksByClass("title");

            for (Iterator<TagNode> iterator = links.iterator(); iterator.hasNext();)
            {
                TagNode divElement = (TagNode) iterator.next();
                output.add(divElement.getText().toString());
            }
            Log.d("dupa", "siteParsed");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return output;
    }

    protected void onPostExecute(List<String> output) {

        pd.dismiss();
        Log.d("dupa", "postExecute");
        ListView listview = (ListView) findViewById(R.id.listViewData);
        //listview.setAdapter(new ArrayAdapter<String>(StackParserActivity.this, android.R.layout.simple_list_item_1 , output));
        MyAdapter dupa = new MyAdapter(StackParserActivity.this, android.R.layout.simple_list_item_1, R.id.movieTitle, output); 
        dupa.notifyDataSetChanged();
        listview.setAdapter(dupa);
        dupa.notifyDataSetChanged();

    }
}

private class MyAdapter extends ArrayAdapter<string> {

    String[] tabObj;

    public MyAdapter(Context context, int resource, int textViewResourceId, List<String> output) {
        super(context, resource, textViewResourceId);
        tabObj = output.toArray(new String[]{});

        Log.d("dupa", tabObj[2].toString());
        notifyDataSetChanged();
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Log.d("dupa", "!!!!getView");

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.row_layout,parent,false);
        TextView tv = (TextView) row.findViewById(R.id.movieTitle);         

        tv.setText(tabObj[position]);

        //Button buton1 = (Button) row.findViewById(R.id.buttonInfo);
        //Button button2 = (Button) row.findViewById(R.id.buttonReserve);

        return super.getView(position, convertView, parent);
    }
};

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尝蛊 2024-12-09 11:01:04

getView 方法返回 row。还要从 MyAdapter 构造函数中删除 notifyDatasetChanged()

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Log.d("dupa", "!!!!getView");

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.row_layout,parent,false);
    TextView tv = (TextView) row.findViewById(R.id.movieTitle);         

    tv.setText(tabObj[position]);

    //Button buton1 = (Button) row.findViewById(R.id.buttonInfo);
    //Button button2 = (Button) row.findViewById(R.id.buttonReserve);

    return row;
}

编辑:
尝试在返回 tabObjsize() 的适配器中覆盖 getCount

Return row from getView method. Also remove the notifyDatasetChanged() from the MyAdapter constructor.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Log.d("dupa", "!!!!getView");

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.row_layout,parent,false);
    TextView tv = (TextView) row.findViewById(R.id.movieTitle);         

    tv.setText(tabObj[position]);

    //Button buton1 = (Button) row.findViewById(R.id.buttonInfo);
    //Button button2 = (Button) row.findViewById(R.id.buttonReserve);

    return row;
}

EDIT :
Try overriding getCount in adapter where you return the size() of tabObj

夜声 2024-12-09 11:01:04

由于您设置了适配器 onPostExecute,因此无需在调用它的任何位置设置 notificationDataChanged。

使用notifyDataChanged的原因是通知已设置为listview的适配器数据已更改,您应该更新它。希望这有帮助!

Since you set the adapter onPostExecute, there is no need to set notifyDataChanged in any of the places that you call it.

The reason that notifyDataChanged is used is to notify the adapter that has been set to a listview that the data have changed and you should update it. Hope this helps!

暖风昔人 2024-12-09 11:01:04
private class MyAdapter extends ArrayAdapter<string> {

    String[] tabObj;

    public MyAdapter(Context context, int resource, int textViewResourceId, List<String> output) {
        super(context, resource, textViewResourceId);
        tabObj = output.toArray(new String[]{});

当我们创建自定义数组适配器时,

public ArrayAdapter (Context context, int textViewResourceId, T[] objects)

public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)

public ArrayAdapter (Context context, int textViewResourceId, List<T> objects)

public ArrayAdapter (Context context, int resource, int textViewResourceId, List<T> objects)

如果我们不使用上述适配器,则必须使用,我们需要重写 getCount() 方法。

就你而言,
super(上下文、资源、textViewResourceId、输出);
会解决你的问题。

private class MyAdapter extends ArrayAdapter<string> {

    String[] tabObj;

    public MyAdapter(Context context, int resource, int textViewResourceId, List<String> output) {
        super(context, resource, textViewResourceId);
        tabObj = output.toArray(new String[]{});

When we are creating a custom arrayadapter we must be using either

public ArrayAdapter (Context context, int textViewResourceId, T[] objects)

public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)

public ArrayAdapter (Context context, int textViewResourceId, List<T> objects)

public ArrayAdapter (Context context, int resource, int textViewResourceId, List<T> objects)

if we are not using above mention adapter, we need to override getCount() method.

In your case,
super(context, resource, textViewResourceId,output);
will solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文