Brandon
Brandon

Reputation: 703

Android ArrayAdapter<String> not updating

So, my activity implements Runnable and within the run() method I do some web scraping and blah blah blah. But that doesn't have to do with my question. I want it to run some scraping then update the ListView with the results I get. Inside that run method I have this:

myAL.notifyDataSetChanged();

But it doesn't seem to be updating my listView... This is my onCreate:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    loadingSiteDialog = new ProgressDialog(this);
    loadingSiteDialog.setCancelable(false);
    loadingSiteDialog.setMessage("Retrieving Data, Please Wait...");

    loadingSiteDialog.show();

    list = (ListView)findViewById(R.id.list);
    myAL = new AdapterList(this,R.layout.mainlistlayout);
    list.setAdapter(myAL);
    list.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {

        }
    });
    loading = true;
    this.run();

}

Withing my arrayadapter class I use ArrayLists that I propagate within my run() method. My Custom ArrayAdapter class:

public class AdapterList extends ArrayAdapter<String>{

    public AdapterList(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }
    @Override
    public View getView(int pos, View convertView, ViewGroup parent){
        View v = convertView;
        if(v == null){
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.mainlistlayout, null);
        }
        TextView roomTV = (TextView)findViewById(R.id.roomTV);
        roomTV.setText(rooms.get(pos));
        TextView wTV = (TextView)findViewById(R.id.washersTV);
        wTV.setText(washersAvailable.get(pos) + "/" + washersTotal.get(pos));
        TextView dTV = (TextView)findViewById(R.id.dryersTV);
        dTV.setText(dryersAvailable.get(pos) + "/" + dryersTotal.get(pos));
        Log.i("CSUMB Laundy","Attempting to getView: " + convertView.toString());
        return v;
    }

}

Am I just forgetting something? Its been a while since I have had to work with custom listviews.

Upvotes: 0

Views: 966

Answers (1)

Serdar Samancıoğlu
Serdar Samancıoğlu

Reputation: 740

Instead of

TextView wTV = (TextView)findViewById(R.id.washersTV);

Try

TextView wTV = (TextView) v.findViewById(R.id.washersTV);

Edit:

I guess your listview is not updating because you never add data to your adapter. Change your adapter class construct method from,

public AdapterList(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

Like:

private ArrayList<ListItem> listItems;

        public AdapterList(Context context, int textViewResourceId,
                ArrayList<YourListItemClass> objects) {
            super(context, textViewResourceId, objects);
            listItems = objects;
            // TODO Auto-generated constructor stub
        }

and get your data with only

listItems.get(position);

and set its contents to your views.

Of course, you should define a class like ListItem and an ArrayList of this class to construct your adapter from this arraylist. When you stored your data in this arraylist, add it to your adapter.

myAL.add(yourArrayList);

and it will update your listview.

Upvotes: 2

Related Questions