-3

I'm reading this forum all day and can't find solution for my problem. I have MyWorkActivity.java with public class MyWorkActivity. Inside is public void popuniListView (). There I want to populate string array with items from database.

It look like this:

public class MyWorkActivity extends Activity implements OnClickListener {
    .
    .
    .

 public void popuniListView ()
 {
        final String[] listaTipovaRada = new String[90];

I also have ListViewCustomAdapter.java that looks like this:

public class ListViewCustomAdapter extends BaseAdapter 
{
  .
  .
  .
public View getView(int position, View convertView, ViewGroup parent) 
 {
  //Read values from array listaTipovaRada

There I need to read values from string array listaTipovaRada.

I have found some examples of sending array from one ACTIVITY to another ACTIVITY but none of them is about sending array from ACTIVITY to BASEADAPTER.

How can I pass string array from activity in one .java to baseadapter in second .java?

Thanx in advance.

  • Have you read any of the Android documentation about how to use views and adapters? Just pass it to the adapter when you create the adapter (in the adapter's constructor). Or just use the already-there-for-you-by-Android `ArrayAdapter` – David Wasser Apr 04 '13 at 16:40
  • Thank you for your answer, but I'm not professional java programmer. I'm creating this application for free and couldn't find time to read entire android documentation. I'm learning it thru this aplication. So, I would be grateful if you can provide some code example. – Amicussamir Apr 04 '13 at 16:46

1 Answers1

0

In simplicity you could also define your string outside the popuniListView() method making it static access. You can even leave it as a final variable if you initialize it in your constructor or onCreate() method. In addition to passing elements through to your adapter that are already mentioned. It depends on the architecture of your project.

public class MyWorkActivity extends Activity implements OnClickListener 
{
    final static String[] listaTipovaRada;
    .
    .
 protected onCreate()
 {
    ...
    listaTipovaRada = new String{ ... , ... , ...
 }

 public void popuniListView ()
 {
        ...

Then you can statically access the String in normal Java access through the class. If this indeed suites your needs.

public class ListViewCustomAdapter extends BaseAdapter 
{
      .
      .
      .
    public View getView(int position, View convertView, ViewGroup parent) 
     {
       MyWorkActivity.listaTipovaRada[index] ...
Jay Snayder
  • 4,298
  • 4
  • 27
  • 53
  • This is a horrible suggestion :-( You have a memory leak (because the `static String[]` stays around forever, even after the activity has finished. This violates all the usual MVC stuff that separating `View` from `Adapter` is there for. And it breaks encapsulation because you have the adapter accessing variables in the activity. All bad. The Activity should just pass the array in to the adapter when the adapter is constructed. This is how all the other `View`/`Adapter` behaviour works. Check out the Android tutorials for `ListView` as an example. – David Wasser Apr 04 '13 at 17:29
  • Thank you for your remarks about memory usage optimization, but I have no use of your suggestions without some example how i SHOULD do it. If you could, please, provide some code example as Jay Snayder is. That way I can learn the best. Thank you. – Amicussamir Apr 04 '13 at 17:49