¿Cómo funciona BaseAdapter cuando lo extendemos?

Cómo funcionan estos métodos cuando ampliamos Baseadapter .

 public int getCount() public Object getItem(int position) public long getItemId(int position) public View getView(int position, View convertView, ViewGroup parent) 

Porque si tenemos some_return_type_from_getCount() , lo que getView() obtendrá de él y cuando volvamos getView()_return_type quién más allí para get_the_return_value_of getView() .

Estoy totalmente confundido con estos métodos.

El siguiente post es de acuerdo a lo que entendí. Así que siéntase libre Si desea mejorar en lugar de criticar por un punto en particular.

private ArrayList<HashMap<String, String>> mProjectsList = new ArrayList<HashMap<String, String>>(); (Puede utilizar cualquier cursor o matriz que en realidad consiste en datos y desea enlazarlo mediante el adaptador)

Public int getCount () -> le da los elementos totales presentes en su adaptador (como el tamaño de la matriz)

 @Override public int getCount() { // TODO Auto-generated method stub return mProjectsList.size(); } 

Public Object getItem (int position) -> indica qué elemento se ha hecho clic en simplemente volver aquí de la manera en que lo hice especificando la posición. En realidad devuelve todo el biwe que has hecho clic con todas sus propiedades y eso es por qué volvemos aquí la vista de la posición que hicimos clic para decirle a la BASEADAPTER Class que esta vista es cliked

 @Override public Object getItem(int position) { // TODO Auto-generated method stub return mProjectsList.get(position); } 

Public getItemId largo (posición int) le dará la identificación principal que desea devolver cuando se taap en algún elemento de la lista. Cuando en realidad haga clic en algún elemento de la vista de lista que devuelve dos cosas primarykey de formato largo y la posición de formato int ..

Desde este método getItemId () en realidad que devuelve la clave primaria.

Usualmente especificamos la clave primaria como "_id" en nuestra base de datos por lo que cuando usamos adaptadores simples en lugar de extender la clase baseadapter automáticamente devuelve el campo _id como ID principal en formato largo. Pero tenemos que especificar manualmente aquí en BaseAdapter lo que queremos volver

 @Override public long getItemId(int position) { // TODO Auto-generated method stub return Long.parseLong(mProjectsList.get(position).get("ID")) ; // retuning my Primary id from the arraylist by } 

Public View getView (posición int, View convertView, ViewGroup parent) actaully crea la vista donde se enlaza su diseño personalizado

 @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub //**position** index of the item whose view we want. //**convertView** the old view to reuse, if possible. Note: You should //check that this view is non-null and of an appropriate type before using. If it is //not possible to convert this view to display the correct data, this method can create a //new view. // (Lets see if you have total 100 elements in listview , but //currently only 10 are visible in screen. So it will create only 10 items at a time as //only those are visible. //and when you will scroll that listView it will use these same 10 //Views(elemnts in rows to display new data, instead of creating 10 more new views, which //obviously will be efficeient) //While alternatively if we dont extend base adapter (but rather //use simple binding using simpleadapter) it will then generates whole list of 100 Views //in one short whic will be time consuimng/ memory consuming depending uopn the amount of //data to be bind //**parent** the parent that this view will eventually be attached to View rowView = convertView; if (rowView == null) { //always required to be checked as mentioned in google docs // this line checks for if initially row View is null then we have to create the Row View. Once it will be created then it will always Surpass this check and we will keep on reusing this rowView (thats what actually we are looking for) LayoutInflater inflater = mActivitycontext.getLayoutInflater(); // creating instance of layout inflater to inflate our custom layout rowView = inflater.inflate(R.layout.projectslist_row, null); //assigend our custom row layout to convertview whic is to be reused ViewHolder viewHolder = new ViewHolder(); // ViewHolder is a custom class in which we are storing are UI elaments of Row viewHolder.mprojectslistRowView = (TextView) rowView.findViewById(R.id.projectslist_row); //assigned the id of actual textView thats on our custom layout to the instance of TextView in our static class rowView.setTag(viewHolder); } ViewHolder holder = (ViewHolder) rowView.getTag(); String projectName = mProjectsList.get(position).get("ProjectName"); // here i am fetching data from my HashMap ArrayList holder.mprojectslistRowView.setText(projectName); // here i am just assigning what to show in my text view return rowView; } I created this as inner class static class ViewHolder { // create instances for all UI elemnts of your Row layout (here i am showing only text data as row of my list view so only instance of TextView has been created) // It is a statci class hence will keep this instance alive all the time and thats Why we will be able to reuse it again and again. TextView mprojectslistRowView; } 

Sólo tienes que vincular este adaptador a tu control, ya que estamos sobreescribiendo los métodos aquí todo será manejado automáticamente por su cuenta.

FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.