未从 AsyncTask / onPostExecute 调用来自重载 ArrayAdapter 的 getView
您好,这是我的第一篇文章,也是我的第一个适用于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从
getView
方法返回row
。还要从MyAdapter
构造函数中删除notifyDatasetChanged()
。编辑:
尝试在返回
tabObj
的size()
的适配器中覆盖getCount
Return
row
fromgetView
method. Also remove thenotifyDatasetChanged()
from theMyAdapter
constructor.EDIT :
Try overriding
getCount
in adapter where you return thesize()
oftabObj
由于您设置了适配器 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!
当我们创建自定义数组适配器时,
如果我们不使用上述适配器,则必须使用,我们需要重写 getCount() 方法。
就你而言,
super(上下文、资源、textViewResourceId、输出);
会解决你的问题。
When we are creating a custom arrayadapter we must be using either
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.