Android: Can i set custombase adapter to a textview - android

This is how i populate the data to
adapter = new CustomBaseAdapter(this, rowItems);
listView.setAdapter(adapter);
is there any option for me to do for setting textview to a custom base adapter
like below?
textview.setAdapter(adapter);

Related

ListView is not showing up on app

A quick problem, my Listview is not showing up in my app. I just deleted and redownloaded android studios, so I'm on the newest version.
public class ToDoList extends AppCompatActivity {
ArrayList<String> notes = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do_list);
ListView listView = (ListView) findViewById(R.id.listView);
notes.add("Here's an example");
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1);
listView.setAdapter(arrayAdapter);
}
}
You have to pass your arrayList to arrayAdapter, then set your arrayAdapter in your listview.
Use this
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,notes);
listView.setAdapter(arrayAdapter);
You didn't set adapter to listview
add this line
listView.setAdapter(arrayAdapter);
You should have to use this, pass notes to your adatpter
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, notes);
listview.setAdapter(arrayAdapter);
1.Define a new Adapter as follow
First parameter - Context
Second parameter - Layout for the row
Third parameter - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
2. Assign adapter to ListView
listView.setAdapter(adapter);
Use this instead
ArrayApater adapter=new ArrayAdapter(getApplicationContext,android.R.layout.simple_list_item_1,notes);
listView.setAdapter(adapter);
You must have to pass your arrayList to arrayAdapter, then after set your arrayAdapter in listview.
Use Like This
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, notes );
listView.setAdapter(arrayAdapter);

Using listView and spinner in 1 layout

I use listview with cursor adapter and spinner in 1 layout.
I don't know how to use setAdapter into these 2 different thing.
Spinner sp = (Spinner) findViewById(R.id.spinner);
SpinnerAdapter adapter = new SpinnerAdapter(this, R.layout.spinner_layou, R.id.txt, item);
//sp.setAdapter(adapter); <----- I should have to set my spinner adapter here, but it seems to make it crash
cursorAdapter = new NotesCursorAdapter(this, null, 0); //my own class which extend CursorAdapter
ListView list = (ListView) findViewById(android.R.id.list);
list.setAdapter(cursorAdapter);
I have google it and find some clue by using ArrayAdapter. But what i found is ArrayAdapter used for the same type adapter (in my case, I use 2 different adapter(Spinner and CursorAdapter) CMIIW. Is anyone can help me on this?

Adding subitem to ListView

ListView list = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> dataAdaptor = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, android.R.id.text1, cars);
list.setAdapter(dataAdaptor);
I write that codes for listview but I don't know how I get data for subitem. How can I do this?
A ListView item can have it's own custom layout. When you create your adapter for the ListView you can pass in the layout id to the Adapter constructor.
If you want to show some more details like image and text or two textview then You will have to extend an Adapter and implement getView() to property set the image+text.
please check this answer with examples.

Programatically replace String Array in a ListView Array Adapter

I have a ListView that i have populated with a string array: {"1","2","3"} using an Array Adapter.
(ListView) ListOptions = (ListView)findViewById(R.id.ListOptions);
String[] exampleString = new String[]{"1","2","3"};
ListAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,exampleString);
ListOptions.setAdapter(adapter);
Later on, i want to be able to change exampleString to exampleString2:
{"4","5","6"}
and have the ListView update with exampleString2 displayed as the list options.
is there something i can do along the lines of:
adapter.ChangeStringArray(exampleString2);
ListView.setAdapter(adapter);
I want to avoid having to create a new adapter each time I change the String array that is populating the list items.
ArrayAdapter has methods for managing the backing data array.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, new ArrayList<String>());
adapter.addAll(exampleString);
adapter.clear();
adapter.addAll(exampleString2);
adapter.notifyDataSetChanged();

Populating ListView with ArrayAdapter force closes

I am trying to use the following code to populate a ListView using a predefined array of strings:
String[] schedule_names = getResources().getStringArray(R.array.test_schedules);
// Populate the ListView using the array of schedule names
ArrayList<String> als = new ArrayList<String>(Arrays.asList(schedule_names));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.listView, als);
adapter.add("Test");
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
But it force closes unless I comment out listView.setAdapter(adapter); (which obviously means the ListView isn't populated at all). It seems the reason is a NullPointerException.
Why is this?
This line is wrong...
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.listView, als);
...the second parameter of the constructor should not be your ListView, it should be a layout with a TextView.
Try replacing R.id.listView with android.R.layout.simple_list_item_1

Categories

Resources