How do I implement listview in appcompatactivity?

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);
    }
}

In the above code, I encountered errors on getlistview () and setlistadapter

I have the following code in the statusadapter class:

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;
    }
}

I'm tracking this tutorial

resolvent:

Getlistview () and setlistadapter () are methods of listactivity / listfragment

However, it seems that you are extending appcompatactivity. Therefore, you can add listview to your activity_ main; Medium. Next:

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

Set the adapter in this way - listview. Setadapter();

I hope this can help!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>