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;
}
}