3

I have made a custom ListView. I'm trying to populate the ListView by Arraylist. I can successfully send data as a string to populate the ListView but not as ArrayList. Only single row with all the values of arraylist is being displayed.

MainActivity

public class MainActivity extends Activity {
ArrayList<Product> products = new ArrayList<Product>();
Adapter listviewAdapter;
List arrlist = new ArrayList();  
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    arrlist.add("1");
    arrlist.add("2");

    fillData();   
    listviewAdapter = new Adapter(this, products);
    ListView lvMain = (ListView) findViewById(R.id.lvMain);
    lvMain.setAdapter(listviewAdapter);
  }
  void fillData() {
      products.add(new Product(arrlist.toString(),false)); //problem is here i suppose

  }
}

Adapter.java

public class Adapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<Product> objects;
Adapter(Context context, ArrayList<Product> products) {
    ctx = context;
    objects = products;
    lInflater = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    return objects.size();
}
@Override
public Object getItem(int position) {
    return objects.get(position);
}
@Override
public long getItemId(int position) {
    return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        view = lInflater.inflate(R.layout.item, parent, false);
    }
    Product p = getProduct(position);

    ((TextView) view.findViewById(R.id.tvDescr)).setText(p.name);
    CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox);
    return view;
}
Product getProduct(int position) {
    return ((Product) getItem(position));
}
}

Product.java

public class Product {
String name;
boolean selected;

  Product( String items, boolean _box) {
      name = items;
      selected = _box;
  }
}
5
  • Any exception you are getting? Commented May 4, 2015 at 11:00
  • 1
    So what is the problem? Commented May 4, 2015 at 11:00
  • Only single row with combined arraylist is being displayed Commented May 4, 2015 at 11:00
  • You passed all arrlist.toString() wooooooo! check your Argument of Product class then..... Commented May 4, 2015 at 11:01
  • removed .toString(); from arrlist.toString() and made changes to Product class i.e List name; boolean selected; Product( List items, boolean _box) { name = items; selected = _box; } Still doesnt solves it Commented May 4, 2015 at 11:07

2 Answers 2

2

Try add each ArrayList item to Product object through iterating ArrayList :

for(String row :arrlist) {
    products.add(new Product(row, false));
}

Define arrlist as String ArrayList instead of generic type List:

ArrayList<String> arrlist = new ArrayList<String>(); 
Sign up to request clarification or add additional context in comments.

2 Comments

Won't be a type mismatch? From object to string?
@Ted,Glad to help you.
1

Your function should be

void fillData() {
  products.add(new Product("1",false));
  products.add(new Product("2",false));
  products.add(new Product("3",false));
}

1 Comment

I know sending string works. This works. But my question is sending arraylist containing previous data

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.