public class MainActivity extends AppCompatActivity {

   protected List<ParseObject> mStatus;

   @Override
   protected void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
   setSupportActionBar(toolbar);

   ParseUser currentUser = ParseUser.getCurrentUser();

   if (currentUser != null){
       //show user the homepage
       ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Status");
       query.findInBackground(new FindCallback<ParseObject>() {
       @Override
       public void done(List<ParseObject> status, ParseException e) {
       if (e==null){
           //success
           mStatus = status;

            StatusAdapter adapter = new StatusAdapter(getListView().getContext(), mStatus);
            setListAdapter(adapter);
        } else {
        //ther was a problem.
        }
      }
    });

    } else {
        //show login screen
        Intent takeUserToLogin = new Intent(this, LoginActivity.class);
        startActivity(takeUserToLogin);
    }
}

在上面的代码中,我在getListView()setListAdapter上出错。
我在StatusAdapter类中有以下代码:
public class StatusAdapter extends ArrayAdapter<ParseObject> {

    protected Context mContext;
    protected List<ParseObject> mStatus;

    public StatusAdapter(Context context, List<ParseObject> status){
        super(context, R.layout.homepagecustomlayout, status);
        mContext = context;
        mStatus = status;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent){
        ViewHolder holder;

        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.homepagecustomlayout,null);
            holder = new ViewHolder();
            holder.usernameHomepage = (TextView)convertView.findViewById(R.id.usernameHP);
            holder.statusHomepage = (TextView)convertView.findViewById(R.id.statusHP);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ParseObject statusObject = mStatus.get(position);

        //user
        String username = statusObject.getString("user");
        holder.usernameHomepage.setText(username);

        //status
        String status = statusObject.getString("newStatus");
        holder.statusHomepage.setText(status);

        return convertView;
    }

    public static class ViewHolder{
        TextView usernameHomepage;
        TextView statusHomepage;
    }
}

我正在跟踪this tutorial

最佳答案

getListView()setListAdapter()ListActivity/ListFragment的方法。
但是,看起来您正在扩展AppCompatActivity。因此,您可以将alistView添加到您的activity_main中,然后:

ListView listview = findViewById(R.id.yourlistID);

通过这种方式设置适配器-listView.setAdapter();
希望这有帮助!

10-08 18:31