android listview问题,重写getView(),但它永远不会被调用
我重写了函数getView(),但调试时发现它从未被调用,真的很奇怪android listview问题,重写getView(),但它永远不会被调用,android,listview,Android,Listview,我重写了函数getView(),但调试时发现它从未被调用,真的很奇怪 public class EntriesActivity extends ListActivity { private static final String TAG = "EntriesActivity"; private EntriesAdapter mArrayAdapter; private List<Entry> mEntries = new ArrayList<Entry>();
public class EntriesActivity extends ListActivity {
private static final String TAG = "EntriesActivity";
private EntriesAdapter mArrayAdapter;
private List<Entry> mEntries = new ArrayList<Entry>();
String mGoalId;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.entries_activity);
Bundle extras = getIntent().getExtras();
mGoalId = extras.getString("goal_id");
mArrayAdapter = new EntriesAdapter(this);
setListAdapter(mArrayAdapter);
new EntriesRefreshTask().execute();
}
class EntriesAdapter extends ArrayAdapter<Entry> {
Activity context;
EntriesAdapter(Activity context) {
super(context, R.layout.entries_row, mEntries);
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.v("EntriesActivity", position + "");
LayoutInflater inflater = context.getLayoutInflater();
View row = inflater.inflate(R.layout.entries_row, null);
TextView title = (TextView) row.findViewById(R.id.entry_title);
TextView name = (TextView) row.findViewById(R.id.entry_name);
TextView content = (TextView) row.findViewById(R.id.entry_content);
Entry e = EntriesActivity.this.mEntries.get(position);
title.setText(e.getTitle());
name.setText(e.getName());
content.setText(e.getContent());
return (row);
}
}
private class EntriesRefreshTask extends AsyncTask<Void, Void, Boolean> {
private static final String TAG = "EntriesRefreshTask";
private static final boolean DEBUG = Things43Settings.DEBUG;
private Things43HttpApi mHApi = ((Things43App) getApplication())
.getHttpApi();
@Override
protected void onPreExecute() {
if (DEBUG)
Log.d(TAG, "onPreExecute()");
// showProgressDialog("load");
Toast.makeText(EntriesActivity.this, "Loading entries...",
Toast.LENGTH_LONG).show();
}
@Override
protected Boolean doInBackground(Void... params) {
if (DEBUG)
Log.d(TAG, "doInBackground()");
try {
// GoalActivity.this.
getEntries(mGoalId);
return true;
} catch (Exception e) {
if (DEBUG)
Log.d(TAG, "Caught Exception logging in.", e);
// Preferences.logoutUser(foursquare, editor);
return false;
}
}
@Override
protected void onPostExecute(Boolean success) {
try {
if (success) {
Log.d(TAG, "success");
mArrayAdapter.notifyDataSetChanged();
} else {
Toast.makeText(EntriesActivity.this,
R.string.request_failed_toast, Toast.LENGTH_LONG)
.show();
mArrayAdapter.notifyDataSetChanged();
}
} catch (Exception e) {
if (DEBUG)
Log.d(TAG, "GoalActivity:onPostExecute():", e);
}
// dismissProgressDialog();
}
@Override
protected void onCancelled() {
// dismissProgressDialog();
}
public boolean getEntries(String goal_id) {
ArrayList<Entry> entries = mHApi.GetGoalEntries(mGoalId);
Log.v(TAG, entries.size() + "");
if (entries.size() > 0) {
mEntries = entries;
}
return true;
}
}
公共类EntriesActivity扩展了ListActivity{
私有静态最终字符串TAG=“EntriesActivity”;
私人住宅区住宅区;
private List mEntries=new ArrayList();
字符串mGoalId;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.entries\u活动);
Bundle extras=getIntent().getExtras();
mGoalId=extras.getString(“goal_id”);
MarrayaAdapter=新的EntriesAdapter(本);
setListAdapter(MarrayaAdapter);
新建EntriesRefreshTask().execute();
}
类EntriesAdapter扩展了ArrayAdapter{
活动语境;
EntriesAdapter(活动上下文){
super(上下文、R.layout.entries\u行、mEntries);
this.context=上下文;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
Log.v(“EntriesActivity”,位置+”);
LayoutInflater充气器=上下文。getLayoutInflater();
视图行=充气机。充气(R.layout.entries\u行,空);
TextView title=(TextView)row.findViewById(R.id.entry\u title);
TextView name=(TextView)row.findViewById(R.id.entry\u name);
TextView content=(TextView)row.findViewById(R.id.entry\u content);
条目e=EntriesActivity.this.mEntries.get(位置);
title.setText(e.getTitle());
name.setText(如getName());
setText(例如getContent());
返回(行);
}
}
私有类EntriesRefreshTask扩展了AsyncTask{
私有静态最终字符串标记=“EntriesRefreshTask”;
私有静态最终布尔调试=Things43Settings.DEBUG;
私有Things43HttpApi mHApi=((Things43App)getApplication())
.getHttpApi();
@凌驾
受保护的void onPreExecute(){
如果(调试)
Log.d(标记“onPreExecute()”);
//showProgressDialog(“加载”);
Toast.makeText(EntriesActivity.this,“加载条目…”,
Toast.LENGTH_LONG).show();
}
@凌驾
受保护的布尔doInBackground(Void…params){
如果(调试)
Log.d(标记“doInBackground()”);
试一试{
//这个。
getEntries(mGoalId);
返回true;
}捕获(例外e){
如果(调试)
d(标记“捕获异常登录”,e);
//logoutUser(foursquare,编辑器);
返回false;
}
}
@凌驾
受保护的void onPostExecute(布尔值成功){
试一试{
如果(成功){
日志d(标记为“成功”);
mArrayAdapter.notifyDataSetChanged();
}否则{
Toast.makeText(EntriesActivity.this、,
R.string.request_失败_toast,toast.LENGTH_LONG)
.show();
mArrayAdapter.notifyDataSetChanged();
}
}捕获(例外e){
如果(调试)
d(标记“GoalActivity:onPostExecute():”,e);
}
//dismissProgressDialog();
}
@凌驾
受保护的void onCancelled(){
//dismissProgressDialog();
}
公共布尔getEntries(字符串目标\u id){
ArrayList entries=mHApi.GetGoalEntries(mGoalId);
Log.v(TAG,entries.size()+);
if(entries.size()>0){
mEntries=条目;
}
返回true;
}
}
我认为您还必须重写适配器上的“getCount()”方法(我通常也重写getItem()和getItemId(),尽管它们可能不是必需的。)
如果没有,那么ListView认为数据源不包含任何元素,那么为什么还要在相关适配器上调用getView呢。我认为您还必须重写适配器上的“getCount()”方法(我通常也重写getItem()和getItemId(),尽管它们可能不是必需的。) 如果不包含,那么ListView认为数据源不包含任何元素,那么为什么还要在相关适配器上调用getView呢