简体   繁体   中英

Create a custom adapter from simple listview code

i need create a custom adapter for a listview but i can't understand how could do it. Actually it works my work but using simply a listview with all objects inside the default android layout for the lists. I need "separate" the objects creating the custom adapter . This is the actual code:

private static class SoluzioniLoader extends AsyncTaskLoader<List<Soluzione>> {

    private FermataComune partenza;
    private FermataComune arrivo;
    private String data;

    public SoluzioniLoader(Context context, FermataComune partenza, FermataComune arrivo, String data) {
      super(context);
      this.partenza = partenza;
      this.arrivo = arrivo;
      this.data = data;
    }

    @Override
    public List<Soluzione> loadInBackground() {

    try {

      List<Soluzione> soluzioni = Client.cercaCorseAndata(partenza, arrivo, data);
      return soluzioni;
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }

    }
}

private LoaderCallbacks<List<Soluzione>> mLoaderCallbacks = new LoaderCallbacks<List<Soluzione>>() {
    private ProgressDialog pd;
    @Override
    public Loader<List<Soluzione>> onCreateLoader(int id, Bundle args) {
    pd = new ProgressDialog(SoluzioniActivity.this);
    pd.setTitle("Caricamento Soluzioni Trovate");
    pd.setMessage("Attendi...");
    pd.setIndeterminate(false);
    pd.show();

    return new SoluzioniLoader(SoluzioniActivity.this, partenza, arrivo, data);
}

@Override
public void onLoadFinished(Loader<List<Soluzione>> loader, List<Soluzione> data) {
    try {
    pd.dismiss();
     } catch(Exception e){  
    }

    if (data == null) {
    // ERRORE
    } else {
    mListView.setAdapter(new ArrayAdapter<Soluzione>(SoluzioniActivity.this, android.r.layout.simple_list_item_1, data));
}

I've already created a layout for the row

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
 >


    <TextView 
    android:id="@+id/fromhour"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    />

    <TextView 
    android:id="@+id/from"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    />

    <TextView 
    android:id="@+id/tohour"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    />

    <TextView 
    android:id="@+id/to"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    />

</LinearLayout>

Any help how could i create and integrate a custom adapter here? Thanks

Try below code:

//Declare your custom adapter:

private EfficientAdapter adapter;

In onCreate() initialize it:

adapter = new EfficientAdapter(this);

Then set your list adapter,

your_listview.setAdapter(adapter);

//Your adapter code:

private class EfficientAdapter extends BaseAdapter {

        private LayoutInflater mInflater;
        private Context context;
        private Bitmap imageBitmap = null;

        public EfficientAdapter(Context context) {
            mInflater = LayoutInflater.from(context);
            this.context = context;
        }

        @Override
        public int getCount() {
            return YOUR_ARRAY.size();
        }

        @Override
        public Object getItem(int arg0) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            final ViewHolder holder;

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.featured_item, null);
                holder = new ViewHolder();
                holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
                holder.txtAuthorName = (TextView) convertView.findViewById(R.id.txtAuthorName);
                holder.txtDescription = (TextView) convertView.findViewById(R.id.txtDescription);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.txtTitle.setText(data.get(position).get("article_title"));
            holder.txtAuthorName.setText(data.get(position).get("author_name"));
            holder.txtDescription.setText(data.get(position).get("article_text"));

            return convertView;
        }

        class ViewHolder {
//YOUR ATTRUBUTES TO DISPLAY IN LIST DECLARE HERE
            TextView txtTitle;
            TextView txtAuthorName;
            TextView txtDescription;
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM