0

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.

3
  • Call adapter.notifyDataSetChanged(); after the for loop in done(). You'll have to declare adapter as a class member, like you did arrayList. And you should move the setLisData(); call to the end of onCreate(). Commented Feb 6, 2016 at 3:23
  • Thank you very much for your response Commented Feb 6, 2016 at 3:43
  • Brother it's not working, Please give suggestions Commented Feb 6, 2016 at 4:50

1 Answer 1

1

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


}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.