Android ListActivity适配器类未被访问

时间:2014-04-30 00:34:20

标签: android listview adapter listactivity

我正在尝试ListView并且我知道我的AsyncTask正在通过日志文件运行但是实际的Adapter类没有被调用,因为我也有一个登录它没有显示出来。

@Override
      protected void onPostExecute(Void result) {
          super.onPostExecute(result);

          TaskAdapter adapter = new TaskAdapter(ToDoList.this, R.layout.item_task, tasks);
          setListAdapter(adapter);

          Log.d("demo", "onPostExecute ACCESSED");

      }

TaskAdapter.java

public class TaskAdapter extends ArrayAdapter<Task>{

private Context context;
private List<Task> objects;

public TaskAdapter(Context context, int resource, List<Task> objects) {
    super(context, resource, objects);
    this.context = context;
    this.objects = objects;
    }

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    Task task = objects.get(position);

    LayoutInflater inflater = 
            (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.item_task, null);

    // Get Text Views
    TextView tv_title = (TextView) view.findViewById(R.id.task_title);
    TextView tv_item = (TextView) view.findViewById(R.id.task_item);

    tv_title.setText(task.getTitle());
    tv_item.setText(task.getItem());

    Log.d("demo", "ADAPTER ACCESSED");

    return view;
    }


}

activity_to_do_list.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.geotasker.ToDoList"
tools:ignore="MergeRootFrame" >

    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="To Do List Here"
    android:textAppearance="?android:attr/textAppearanceLarge" />

    <ListView
    android:id="@+id/android:list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</FrameLayout>

ToDoList(我的活动)

public class ToDoList extends ListActivity {

private ActionBarActivity abarAct;

// Needed stuff for getting Tasks list and creating ListView
DataManager dm;
ArrayList<Task> tasks;
Task task;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_to_do_list);
    abarAct = new ActionBarActivity();

//      if (savedInstanceState == null) {
//          abarAct.getSupportFragmentManager().beginTransaction()
//                  .add(R.id.container, new PlaceholderFragment()).commit();
//      }

    tasks = new ArrayList<Task>();
    task = new Task();
    dm = new DataManager(this);

//      TextView tv = (TextView) findViewById(R.id.textView1);
//      tv.setText(tasks.get(0).getTitle());

    new GetGeoTasks().execute();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.to_do_list, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_to_do_list,
                container, false);
        return rootView;
    }
}


// If any errors occur, then make a method to first check the db for tasks, then execute AsyncTask

// AsyncTask to get GeoTasks
  private class GetGeoTasks extends AsyncTask<Void, Void, Void> {

      @Override
      protected Void doInBackground(Void... params) {

          tasks = dm.getAllTasks();
          Log.d("demo", "doInBackground ACCESSED");

          return null;
      }

      @Override
      protected void onPostExecute(Void result) {
          super.onPostExecute(result);

          TaskAdapter adapter = new TaskAdapter(ToDoList.this, R.layout.item_task, tasks);
          setListAdapter(adapter);

          Log.d("demo", "onPostExecute ACCESSED");

      }

      @Override
      protected void onPreExecute() {
          super.onPreExecute();
      }


  } // END GetGeoTasks


}

任何人都可以帮我指出正确的方向吗?谢谢。

1 个答案:

答案 0 :(得分:2)

尝试添加

adapter.notifyDataSetChanged();

在setListAdapter(Adapter)下面;

希望这有助于你=)