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

时间:2011-09-02 16:13:23

标签: java android android-asynctask android-arrayadapter

嗨,这是我的第一篇文章,这是我的Android手机的第一个更高级的应用程序...我知道这个主题在这里被提到,google知道很多例子,但是它们并不是我看起来形式的确切;(

我承认我是一个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);
    }
};

}

3 个答案:

答案 0 :(得分:5)

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

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

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

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[]{});

当我们创建自定义arrayadapter时,我们必须使用

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); 将解决您的问题。