1

We are making a dynamic custom array adapter for our android project. Now we need to pass dynamic android layout object and other data source as argument when initialize this custom adapter class. After that everything will be dynamic.

public class CustomAdapterView extends ArrayAdapter<String> {
private Activity context;
private String[] googleProducts;
private String[] productDescription;
private Integer[] productImage;
private Layout listItemLayout;

public CustomAdapterView(Activity context, Layout listItemLayout, String[] googleProducts, String[] productDescription, Integer[] productImage) {

    super(context, R.layout.content_custom_listview, googleProducts);
    // Tring to do this
    //super(context, listItemLayout, googleProducts);
    //Error: Cannot resolve method 'super(android.app.Activity, android.text.Layout, java.lang.String[])'

    // Set Local Property
    this.context = context;
    this.googleProducts = googleProducts;
    this.productDescription = productDescription;
    this.productImage = productImage;
}

Now in the super() method we want to pass dynamic Layout for displaying custom list view.

super(context, listItemLayout, googleProducts);

//Error: Cannot resolve method 'super(android.app.Activity, android.text.Layout, java.lang.String[])'

Than call that from any Activity

// Initialize
    ListView lv = (ListView) findViewById(R.id.custom_listview);
    Layout dynamicLayout = null;

    // Our Custom Adapter Object
    CustomAdapterView cav = new CustomAdapterView(this, dynamicLayout, googleProducts, productDescription, productImage);
    //Set Adapter
    lv.setAdapter(cav);

2 Answers 2

3

R.layout.content_custom_listview is an int so, there won't be a super method for that signature. You can pass the int type directly to your adapter instead of Layout type.

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

Comments

2

You seem to be confusing a "layout" resource (i.e. an xml file with a bunch of views in it, accessed via an R.layout.foo constant), and an android.text.Layout object (which manages the internals of displaying text, e.g. in a TextView).

Layout resources are generally passed around as an int, or as a View inflated from that layout resource. Chances are very high that your custom adapter's constructor:

public CustomAdapterView(Activity context, 
    Layout listItemLayout,
    String[] googleProducts,
    String[] productDescription,
    Integer[] productImage) {

    ...
}

should be declared like this instead:

public CustomAdapterView(Activity context, 
    int listItemLayout,
    String[] googleProducts,
    String[] productDescription,
    Integer[] productImage) {

    ...
}

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.