我重写了函数getView(),但是当调试我发现它永远不会被调用时,真的很奇怪......
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;
}
}
答案 0 :(得分:1)
我认为你还必须在你的适配器上覆盖'getCount()'方法(我通常也会覆盖getItem()和getItemId(),因为它们可能不是必需的。)
如果不这样做,那么ListView认为您的数据源不包含任何元素,那么为什么还要在相关的适配器上调用getView。