ArrayAdapter<HashMap<,,>> with commonsguy EndlessAdapter class

北慕城南 提交于 2020-01-23 13:25:11

问题


How to use ArrayAdapter-HashMap- with EndlessAdapter class?

  class DemoAdapter extends EndlessAdapter {
        private RotateAnimation rotate=null;

        @SuppressWarnings("unchecked")
        ArrayAdapter<HashMap<String,String>> a=(ArrayAdapter<HashMap<String,String>>)getWrappedAdapter();

        DemoAdapter(ArrayList<HashMap<String, String>> items) {
        super(new ArrayAdapter<HashMap<String, String>>(Base.this,
                                          R.layout.row,
                                          android.R.id.text1, <------ ????
                                          items)); <------ ????

I have two columns in my row R.layout.text1 and R.layout.text2.

I saw few examples with PROJECTION_COLUMNS and VIEW_MAPPINGS but don't know how to use it correctly.

Here is full code, it's working but I get something like {test2=dasdasd, test1=asdasd} in Listview (text1):

import com.commonsware.cwac.endless.EndlessAdapter;

import java.util.ArrayList;
import java.util.HashMap;

public class Read extends ListActivity {

      EndlessAdapter x;
      SQLiteDatabase Db;    
      public Cursor c;
      Integer cx;

      String Hash;

      @Override
      public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.readmain);

        Hash = getIntent().getStringExtra("Hash");
        cx = getIntent().getIntExtra("Count", 0);

        Db = openOrCreateDatabase("/sdcard/test.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);

        ArrayList<HashMap<String,String>> items=new ArrayList<HashMap<String,String>>();

        x = new DemoAdapter(items);
        setListAdapter(x);
      }


      class DemoAdapter extends EndlessAdapter {
        private RotateAnimation rotate=null;

        @SuppressWarnings("unchecked")
        ArrayAdapter<HashMap<String,String>> a=(ArrayAdapter<HashMap<String,String>>)getWrappedAdapter();


          DemoAdapter(ArrayList<HashMap<String, String>> items) {


          super(new ArrayAdapter<HashMap<String, String>>(ReadChat.this,
                                          R.layout.row,
                                          R.id.textas1,
                                          items));

          rotate=new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
                                      0.5f, Animation.RELATIVE_TO_SELF,
                                      0.5f);
          rotate.setDuration(600);
          rotate.setRepeatMode(Animation.RESTART);
          rotate.setRepeatCount(Animation.INFINITE);
        }

        @Override
        protected View getPendingView(ViewGroup parent) {
          View row=getLayoutInflater().inflate(R.layout.row, null);

          View child=row.findViewById(R.id.textas1);

          child.setVisibility(View.GONE);

          child=row.findViewById(R.id.throbber);
          child.setVisibility(View.VISIBLE);
          child.startAnimation(rotate);

          return(row);
        }

        @Override
        protected boolean cacheInBackground() {
            c = Db.rawQuery("SELECT a, b FROM TableLIMIT "+a.getCount()+", 15",null);

            return(getWrappedAdapter().getCount()< cx);
        }

        @Override
        protected void appendCachedData() {

          String a;
              String b;

          if (c != null ) {
                if  (c.moveToFirst()) {
                      do {
                           a = c.getString(c.getColumnIndex("a"));
                           b = c.getString(c.getColumnIndex("b"));

                           HashMap<String, String> temp = new HashMap<String, String>();                        
                               temp.put("test1", a);
                               temp.put("test2", b);                        
                               a.add(temp); 

                  } while (c.moveToNext());
                }
          }
        }
      }
    }

回答1:


Your problems have nothing to do with EndlessAdapter.

First, get your code working with an ArrayAdapter, ignoring EndlessAdapter for now. This will require you to learn how to create a custom ArrayAdapter, as you will quickly discover that ArrayAdapter knows little about how to use a HashMap<String, String>. You will need to extend ArrayAdapter to your own WhateverThisIsAdapter, where you override getView() to handle pouring data from your HashMap<String, String> into inflated rows, properly handling your row recycling. This free excerpt from one of my books will demonstrate some of this.

Then, and only then, wrap the working ArrayAdapter in an EndlessAdapter.

Also, never hard-wire paths, as /sdcard is wrong on most Android devices.



来源:https://stackoverflow.com/questions/8588246/arrayadapterhashmap-with-commonsguy-endlessadapter-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!