2011-09-02 75 views
3

您好我的第一篇文章我的第一個Android應用程序的更高級的應用程序...我知道這個主題在這裏提到,谷歌知道很多例子,但他們不是什麼即時通訊形式;(從重載的ArrayAdapter getView不being從AsyncTask/onPostExecute調用

我承認我是一個n00000b ...在Android編碼方面...我開始...前幾天,請原諒我:)

問題是我的listview元素沒有被填充。應用程序編譯和運行沒有錯誤,但它沒有午餐getView。從我閱讀網絡上...我需要這與它的內容顯示我心愛的ListView ...

請幫助:)

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

}

回答

5

返回rowgetView方法。也從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; 
} 

編輯: 嘗試重寫適配器getCount,你返回tabObj

+0

我按照你的建議更改了代碼。 Iam現在返回行,我刪除了所有notifyDatasetChanged(),但仍然沒有。 getView方法不吃午餐。 Log.d(「dupa」,「!!!! getView」);不會輸出任何內容到Log:/ – mentis

+3

您應該在適配器中重寫'getCount'。 – Ronnie

+0

是的:D thx非常非常! – mentis

1

既然你設置適配器onPostExecute,沒有必要在你調用它的任何地方設置notifyDataChanged。

使用notifyDataChanged的原因是通知已設置爲列表視圖的適配器數據已更改,您應更新它。希望這可以幫助!

+0

我懷疑,添加notifyDataChanged沒有幫助...這是我的絕望行爲:/ – mentis

+0

從onPostExecute和適配器的構造函數中刪除這兩個。如果它仍然沒有工作,你會得到任何錯誤?列表的大小是否大於0? –

+0

刪除所有'notifyDataChanged()'Log.d(「dupa」,tabObj [2] .toString())後仍然不起作用。在DDMS中顯示正確的數據,所以是的,列表'輸出'> 0和數組'tabObj'> 0. – mentis

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

size() 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); 將解決你的問題。

相關問題