-1

This is what i have tried, but my ListView doesn't get filled. I use a custom adapter because i wan't to change the background color of the items that't got a boolean value = true. I am using Android Studio. Hope someone can help. i'm new to android.

 public class ShoppingListActivity extends ActionBarActivity {
        private TextView header;
        private ListView listView;
        private CustomArrayAdapter arrayAdapter;
        private ShoppingList shoppingList;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_shopping_list);
            header = (TextView)findViewById(R.id.textView2);

            //get reference to ListView
            listView = (ListView)findViewById(R.id.shoppingListItemsView);
            shoppingList = (ShoppingList) getIntent().getSerializableExtra(Globals.CHOSENLISTKEY);
            arrayAdapter = new CustomArrayAdapter(this, R.layout.custom_list_view, shoppingList.getItemList());
            listView.setAdapter(arrayAdapter);


            header.setText(shoppingList.toString());

            Button btnShop = (Button) findViewById(R.id.btnShop);
            btnShop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ShoppingListActivity.this, SelectStoreActivity.class);
                    startActivity(intent);
                }
            });
        }

        public void setData() {
            for(int i = 0; i < shoppingList.getItemList().size(); i++) {
                arrayAdapter.add(shoppingList.getItemList().get(i));
            }
        }
    }


    public class CustomArrayAdapter extends ArrayAdapter<Entry> {

        private int resource;
        private ArrayList<Entry> list;

        public CustomArrayAdapter(Context context, int resource, ArrayList<Entry> list) {
            super(context, resource);
            this.resource = resource;
            this.list = list;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parentGroup) {
            View view = convertView;
            Entry entry = list.get((position));
            if(view == null) {
                LayoutInflater layoutInflater = ((Activity) getContext()).getLayoutInflater();
                view = layoutInflater.inflate(resource, parentGroup, false);
            }

            TextView textView = (TextView) view.findViewById(R.id.customName);

                if(entry.getItem().isBought()) {
                    textView.setBackgroundColor(Color.BLUE);
                }

            return view;
        }
    }

1 Answers1

1

To show data in ListView rows call setText method of TextView which is return from getView method:

TextView textView = (TextView) view.findViewById(R.id.customName);
// get Text from entry and pass it to setText method
String strTextViewData=entry.getItem()...;
textView.setText(strTextViewData); 
if(entry.getItem().isBought()) {
 textView.setBackgroundColor(Color.BLUE);
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • i get this error when adding your code java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference – user2993005 Feb 13 '15 at 16:12
  • @user2993005: Use `view = layoutInflater.inflate(R.layout.custom_list_view,parentGroup, false);` line instead of `view = layoutInflater.inflate(resource, parentGroup, false);` – ρяσѕρєя K Feb 13 '15 at 16:26
  • Okay that gives the same error, my code in getView looks like this now http://puu.sh/fRQur/d2822ca6b0.png – user2993005 Feb 13 '15 at 17:08
  • @user2993005: try to use `LayoutInflater layoutInflater = ShoppingListActivity.this.getLayoutInflater();` instead of `LayoutInflater layoutInflater = ((Activity) getContext()).getLayoutInflater();` – ρяσѕρєя K Feb 13 '15 at 17:16
  • Its in 2 separate class' CustomArrayAdapter is not an inner class. I get the error ShoppingListActivity is not an enclosing class – user2993005 Feb 13 '15 at 17:24
  • @user2993005: then use `context` instead of `ShoppingListActivity.this` – ρяσѕρєя K Feb 13 '15 at 17:28
  • @user2993005: Use `LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.custom_list_view,parentGroup, false);` – ρяσѕρєя K Feb 13 '15 at 17:43
  • @user2993005: and are you sure `customName` TextView is inside `custom_list_view` layout ? – ρяσѕρєя K Feb 13 '15 at 17:44
  • java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView i get this error now. on this line view = inflater.inflate(R.layout.custom_list_view,parentGroup, false); I don't think i had, i made it in two serparate xml files like this http://puu.sh/fRX75/bfe03b1b58.png should it be like this ? http://puu.sh/fRXuX/4cced257ca.png – user2993005 Feb 13 '15 at 18:01
  • @user2993005: in which layout you have `customName` in `custom_list_view` or in `custom_listview_item` ?? – ρяσѕρєя K Feb 13 '15 at 18:05
  • I had it in custom_listview_item before, now i moved it to custom_list_view its in both right now, i'm confused of where to put it. – user2993005 Feb 13 '15 at 18:07
  • @user2993005: remove `ListVIew` from `custom_list_view` and add only `TextView` in it then run your app – ρяσѕρєя K Feb 13 '15 at 18:08
  • Wow thank you so much for you help and patience :) REALLY appriciate it :D :D – user2993005 Feb 13 '15 at 18:14