从简单的列表视图代码创建自定义适配器

时间:2014-06-11 13:06:05

标签: java android android-listview custom-adapter

我需要为列表视图创建自定义适配器,但我无法理解如何做到这一点。实际上它工作我的工作,但只使用列表视图与列表的默认Android布局内的所有对象。我需要“分离”创建custom adapter的对象。这是实际的代码:

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

我已经为行

创建了一个布局
<?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>

任何帮助我如何在这里创建和集成自定义适配器?感谢

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

//声明自定义适配器:

private EfficientAdapter adapter;

在onCreate()中初始化它:

adapter = new EfficientAdapter(this);

然后设置列表适配器

your_listview.setAdapter(adapter);

//您的适配器代码:

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

    }