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;
}
}
我正在按照这个教程学习。