views:

16

answers:

1

I override the function getView(), but when debug I found it never be invoked, really weird...

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

 }
A: 

I think you also have to override the 'getCount()' method on your adapter (I usually override getItem() and getItemId() too, tho they might be not neccesary.)

If you do not, then your ListView thinks that your data source doesn't contain any elements, so why bother calling getView on the related adapter.

Scythe