How do you set adapter of ListView?

How to set adapter of ListView?

import java.util.List;

import android.widget.ListAdapter;
@SimpleFunction(description = "Add image, title, subtitle in List with image and text")

public void Add(List title, List subtitle, List image){

ListAdapter listAdapter = new ListAdapter(title, subtitle, image);

listView.setAdapter(listAdapter);

}

Compiling Java files
│ warning: The following options were not recognized by any processor: '[output, org, root, version, extName]'

│ ERR src\com\faraz\ListWithImageAndText\ListWithImageAndText.java:39: error: ListAdapter is abstract; cannot be instantiated
│ ListAdapter listAdapter = new ListAdapter(title, subtitle, image);
│ ^

│ Total error(s): 1
└ Failed

• Build failed [2s 441ms]

1 Like

I also use ArrayAdapter

public void Add(List list) {
        ListView lv =  new ListView(this.container.$context());

        List myRowItems = new ArrayList(list);

        ArrayAdapter myAdapter = new ArrayAdapter(this.container.$context(), myRowItems);

        lv.setAdapter(myAdapter);
}

┌ Compiling Java files
│ warning: The following options were not recognized by any processor: '[output, org, root, version, extName]'

│ ERR src\com\faraz\CustomAdapter\CustomAdapter.java:27: error: incompatible types: List cannot be converted to int
│ ArrayAdapter myAdapter = new ArrayAdapter(this.container.$context(), myRowItems);
│ ^
│ Note: C:\Users\Afroz Ahmad\Desktop\Coding\MyExtensions\custom-adapter\src\com\faraz\CustomAdapter\CustomAdapter.java uses unchecked or unsafe operations.
│ Note: Recompile with -Xlint:unchecked for details.
│ Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output

│ Total error(s): 1
└ Failed

• Build failed [2s 198ms]

1 Like

@Faraz_Firoz I will give you a good example.


First, this is the part you have to do or change on your code

........
...
.....

public class ListWithImageAndText extends AndroidNonvisibleComponent {

    private Context context;
    private Activity activity;
    public ListView listView;

    public ListWithImageAndText(ComponentContainer container){
        super(container.$form());
        this.activity = container.$context();
        this.context = container.$context();
    }

Second, here's an example that will serve as an illustration for you

    @SimpleFunction(description = "")
    public void Create(HVArrangement arrangement){
        
        listView = new ListView(context);
        FrameLayout frameLayout = (FrameLayout) arrangement.getView();
        frameLayout.addView(listView, new FrameLayout.LayoutParams(-1, -1));

    }

    @SimpleFunction(description = "")
    public void Add(){

        String[] myData = {"Visual Basic .NET", "Java", "Ruby"};
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, 17367043, myData);
        listView.setAdapter(arrayAdapter);

    }

And i just want you to understand, here is the Parameters I use for ArrayAdapter

ArrayAdapter (Context context,  int resource, T[] objects)

Context = container.$context();
Resource = android.R.layout.simple_list_item_1 = 17367043 (Constant Value)
T[] objects = your simple list, ArrayList etc.

4 Likes

I use this tomorrow may this works

2 Likes

@Faraz_Firoz Please check again the post above carefully, because I've changed something

2 Likes

Do you compile this Is this works

2 Likes

100% I guarantee it works :star_struck:

2 Likes

That's too good👍

2 Likes

Is we use any another int on this

2 Likes

no, don't change that constant value.
I fine that constant velue here
https://developer.android.com/reference/android/R.layout#simple_list_item_1

2 Likes

Ok thanks🥳

2 Likes

@Faraz_Firoz if you have tested it and it works, please close this topic. Thank you :kissing_heart:

2 Likes

From where I close this only moderator can close this

2 Likes

Select Solution checkbox on one of @Salman_Dev 's posts

3 Likes

I check this code tommorow and if works then I mark his post as solution

3 Likes

Thank you @Salman_Dev as I told you earlier you are the best developer.

3 Likes

Thank you very much @Faraz_Firoz :kissing_heart:
You are also the best developer :wink: :+1:

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.