1

I watched thenewboston tutorials and there is something I didnt understood.

I didnt understand how the MainActicty knows what CustomListView to use-we arent pass any info about it. The only info is the context and the string array that we set-up at the MainActivty. (https://www.youtube.com/watch?v=nOdSARCVYic&index=47&list=PL6gx4Cwl9DGBsvRxJJOzG4r4k_zLKrnxl 9:01) it's looks like he took the name of the array from the custom_row but I checked it and this is the MainActivty.

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String [] names = {"a"," b","c", "d", "e"};


        ListAdapter elichaiAdapter = new CustomAdapter(this , names);
        ListView elichaiListView = (ListView) findViewById(R.id.elichaiListView);

        elichaiListView.setAdapter(elichaiAdapter);


        elichaiListView.setOnItemClickListener(
                new AdapterView.OnItemClickListener(){
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        //get the the value as string
                        String food = String.valueOf(parent.getItemAtPosition(position));
                        Toast.makeText(MainActivity.this , food , Toast.LENGTH_SHORT).show();
                    }
                }
        );
    }
}

custom_row class

    class CustomAdapter extends ArrayAdapter<String> {

    CustomAdapter(Context context, String [] names) {

         super(context, R.layout.coustem_raw,  names );
     }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater elichaiInflater = LayoutInflater.from(getContext());

        View customView = elichaiInflater.inflate(R.layout.coustem_raw, parent, false);
        String nameItem = getItem(position);
        TextView elichaiText = (TextView) customView.findViewById(R.id.elichaiTextView);
        ImageView elichaiImage = (ImageView) customView.findViewById(R.id.elichaiImageView);

        elichaiText.setText(nameItem);
        elichaiImage.setImageResource(R.drawable.noname);
        return customView;
    }
}

1 Answers1

0

What is the confusion?

The important line is here:

ListAdapter elichaiAdapter = new CustomAdapter(this , names);

So you are telling MainActivity to use your CustomAdapter. You are supplying it with a context and an array which will populate the List View.

Since it is a new instance of CustomAdapter, any changes made to it will be will be specific to that instance alone.

You are then telling your listView to use this custom Adapter here:

elichaiListView.setAdapter(elichaiAdapter);

If you wanted to change your custom Adapter. You would have to edit or create a new instance, or create a new Adapter altogether, and run the above line of code on that Adapter. Then you would notify the list view that changes have been made.

So we have:

(CustomAdapter)    (new CustomAdapter)      (findViewById)      .inflate(R.layout)

Adapter Class   ->   Adapter instance    ->    List View     ->  Layout (design) 

Connecting them all together gives you a functioning List View

  • Hi, thanks for the answer. I knew what the important line but I didnt understand something-the context I set is the MainActivty context (*this*) and the array is array whcih I set-up before at the MainActivty, where is the info from the Custom_Row? If I had another custom ListView how shuld he know whic one to use? –  Aug 10 '15 at 13:32
  • `View customView = elichaiInflater.inflate(R.layout.coustem_raw, parent, false);` ... in the `getView()` method you are connecting the individual rows to a data object in your array (passed through), and you are also connecting the row to your layout file so we know what it looks like –  Aug 10 '15 at 13:34
  • So your list view is created in the `activity_main` xml file. You reference this from your `MainActivity.java` file, using `findViewById`. You create an instance of CustomAdapter which you will connect to this ListView, passing through an Array of data objects. Inside the Adapter Class `getView()` method, you use the data to manipulate the rows, and you use the layout xml to view this data. Make sense? –  Aug 10 '15 at 13:37
  • how mainActivty get the Custom_Raw class data? from the array or from the context ? –  Aug 10 '15 at 13:42
  • Hmm.. I think I see where you're confused. ALL the data comes from the String array, `names`. Your Adapter references your `custom_row` layout xml file, so we know what the rows should look like. TheNewBoston has all his data in `foods`. But his Adapter class is a bit different. He is using a default list layout that is readily available in android called `simple_list_item_1`. This layout is there for people who only need to show simple text for each row, and in your case you have a custom layout. So you do not use the same approach as him. –  Aug 10 '15 at 13:50
  • Clearly his constructor method for his Adapter is also different to yours, there is no strict way of doing it. Try and follow all the method arguments and where data is being passed in order to fully understand. –  Aug 10 '15 at 13:52
  • guys, sorry I spent your time. I understood what was my problem-CustomAdapter is my name of class, I was sure that this is a method. Thank you very much for help. –  Aug 10 '15 at 14:03
  • Happens to the best of us! –  Aug 10 '15 at 14:05