【问题标题】:getView from overloaded ArrayAdapter is not beeing called from AsyncTask / onPostExecute未从 AsyncTask / onPostExecute 调用来自重载 ArrayAdapter 的 getView
【发布时间】:2011-09-02 16:13:23
【问题描述】:

嗨,这是我在这里的第一篇文章,我的第一个更高级的应用程序适用于我的 Android 手机......我知道这里提到了这个主题,谷歌知道很多例子,但它们并不完全符合我的外观;(

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

问题是我的列表视图元素没有被填充。应用程序编译并运行没有错误,但它没有午餐 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);
    }
};

}

【问题讨论】:

    标签: java android android-asynctask android-arrayadapter


    【解决方案1】:

    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;
    }
    

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

    【讨论】:

    • 我按照您的建议更改了代码。我现在返回行,我删除了所有 notifyDatasetChanged(),但仍然没有。 getView 方法不吃午饭。 Log.d("dupa", "!!!!getView");不向日志输出任何内容:/
    • 你应该覆盖适配器中的getCount
    • 编辑是我的问题(覆盖 getCount)。我从另一个项目中复制了一个适配器,但忘记从底部抓取此方法。
    【解决方案2】:

    由于您将适配器设置为 onPostExecute,因此无需在您调用它的任何地方设置 notifyDataChanged。

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

    【讨论】:

    • 我怀疑,添加 notifyDataChanged 并没有帮助......这是我绝望的行为:/
    • 从 onPostExecute 和适配器的构造函数中删除。如果它仍然没有工作,你会得到任何错误吗?列表的大小是否 > 0 ?
    • 删除所有 'notifyDataChanged()' Log.d("dupa", tabObj[2].toString());在 DDMS 中显示正确的数据,所以是的,列表 'output' > 0 并且数组 'tabObj' 是 > 0。
    【解决方案3】:
    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(context, resource, textViewResourceId,output); 会解决你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      • 1970-01-01
      相关资源
      最近更新 更多