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();
希望这有帮助!