简体   繁体   中英

How to add data from parse to an Android ListView

I have to parse an account. I got the data from parse but I cannot add it into an ArrayList nor a ListView.

I managed to get the data into a String but I am not able to add it to the List View.

Here is my code:

public class student_login extends Activity implements View.OnClickListener {

private TextView mTextView;
ArrayList arrayList;;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_student_login);

    arrayList = new ArrayList();
    setLisData();

    ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_list_view, arrayList);

    ListView listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(adapter);

    Button sub=(Button)findViewById(R.id.button2);
    sub.setOnClickListener(this);

}

private void setLisData() {

    ParseUser currentUser = ParseUser.getCurrentUser();
    String curser = currentUser.getUsername().toString();
    final String sc = currentUser.getString("school");
    Toast.makeText(getApplicationContext(), "Your id is " + sc, Toast.LENGTH_SHORT).show();

    ParseQuery<ParseObject> query = ParseQuery.getQuery("Staff");
    query.whereEqualTo("school", sc);
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            for (ParseObject parseObject : objects) {
                String name = parseObject.getString("Name");
                arrayList.add(name);
                Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();

            }
        }
    });
}

public void onClick(View v) {

    switch (v.getId()){
        case R.id.button2:

            // r =movie.getRatingStar();
            ParseUser.logOut();
            ParseUser currentUser = ParseUser.getCurrentUser(); // should be null but isn't...
            invalidateOptionsMenu();
            Toast.makeText(getApplicationContext(), "Disconnected...", Toast.LENGTH_LONG).show();

            Intent intent = new Intent(this,Login.class);
            startActivityForResult(intent,0);
            finish();

            break;
    }
}

}

At runtime the output names are displayed in toast but not adding list view.Thank you.

do below changes in your code its works fine

ArrayAdapter adapter

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_student_login);

    arrayList = new ArrayList();


    adapter = new ArrayAdapter<String>(this, R.layout.activity_list_view, arrayList);

    ListView listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(adapter);

    Button sub=(Button)findViewById(R.id.button2);
    sub.setOnClickListener(this);

    setLisData();
}

private void setLisData() {

    ParseUser currentUser = ParseUser.getCurrentUser();
    String curser = currentUser.getUsername().toString();
    final String sc = currentUser.getString("school");
    Toast.makeText(getApplicationContext(), "Your id is " + sc, Toast.LENGTH_SHORT).show();
    if(arrayList.size()>0){
     arrayList.clear();
    }
    ParseQuery<ParseObject> query = ParseQuery.getQuery("Staff");
    query.whereEqualTo("school", sc);
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            for (ParseObject parseObject : objects) {
                String name = parseObject.getString("Name");
                arrayList.add(name);
                Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();

            }

            if(arrayList.size()>0 && adapter != null){
               adapter.notifyDataSetChanged();
            }
        }
    });


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM