我创建了一个 StatusAdapter 类,用我通过查询Parse.com数据库获取的对象填充 MainActivity 。一切似乎都很好,我没有在Logcat中得到任何错误。当我运行 MainActivity 时,我得到的是一个没有操作栏和项目列表的白色屏幕。我不明白为什么,也许你可以帮助我。 我必须提到我是Android的初学者。
下面你可以看到我的适配器列表。
StatusAdapter.java
public class StatusAdapter extends ArrayAdapter<ParseObject> {
protected Context mContext;
protected List<ParseObject> mStatus;
public StatusAdapter(Context context, List<ParseObject> status) {
super(context, R.layout.homepage_custom_layout);
mContext = context;
mStatus = status;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.homepage_custom_layout, null);
holder = new ViewHolder();
holder.usernameHomepage = (TextView) convertView.findViewById(R.id.usernameHP);
holder = new ViewHolder();
holder.statusHomepage = (TextView) convertView.findViewById(R.id.statusHP);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ParseObject statusObject = mStatus.get(position);
// username
String username = statusObject.getString("username");
holder.usernameHomepage.setText(username);
// status
String status = statusObject.getString("newStatus");
holder.statusHomepage.setText(status);
return convertView;
}
public static class ViewHolder {
TextView usernameHomepage;
TextView statusHomepage;
}
在我的 MainActivity 中,我从Parse数据库获取数据,并使用 StatusAdapter 将信息输入 ListView 。另外我要提一下,我在 activity_main.xml 的布局中定义了 ListView ,它看起来像这样:
activity_main.xml中
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
MainActivity.java
public class MainActivity extends ListActivity {
protected List<ParseObject> mStatusObjects;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Enable Local Datastore.
Parse.initialize(this, "fLDZVeIkrA39AHPDK9gbujmzIb0bcZUIuaRNIBdk", "pwwZVAJWmzABPPShPB0qiD8764XiwlBJGZZObiqo");
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
// do stuff with the user
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Status");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> statusList, ParseException e) {
if(e == null) {
mStatusObjects = statusList;
StatusAdapter mAdapter = new StatusAdapter(getListView().getContext(),mStatusObjects);
setListAdapter(mAdapter);
}
}
});
} else {
// there was a problem
}
}
MainActivity.java 的ActionBar代码也是:
@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();
//noinspection SimplifiableIfStatement
switch (id) {
case R.id.updateStatus:
// take user to update activity
Intent toMainActivityIntent = new Intent(MainActivity.this, UpdateStatusActivity.class);
startActivity(toMainActivityIntent);
break;
case R.id.LogoutUser:
//Log out user
ParseUser.logOut();
// take user to login activity
Intent toLoginActivityIntent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(toLoginActivityIntent);
break;
}
return super.onOptionsItemSelected(item);
}