ArrayAdapter <散列映射<,,>&GT;与commonsguy EndlessAdapter类</hashmap <,,>

时间:2011-12-21 10:18:44

标签: java android commonsware-cwac

如何在EndlessAdapter类中使用ArrayAdapter-HashMap?

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

我的行R.layout.text1和R.layout.text2。

中有两列

我看到了PROJECTION_COLUMNSVIEW_MAPPINGS的一些示例,但不知道如何正确使用它。

这是完整的代码,它正在运行,但我在Listview(text1)中得到类似{test2 = dasdasd,test1 = asdasd}的内容:

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 个答案:

答案 0 :(得分:2)

您的问题与EndlessAdapter无关。

首先,让您的代码使用ArrayAdapter,暂时忽略EndlessAdapter。这将要求您了解如何创建自定义ArrayAdapter,因为您会很快发现ArrayAdapter对如何使用HashMap<String, String>知之甚少。您需要将ArrayAdapter扩展到您自己的WhateverThisIsAdapter,在其中覆盖getView()以处理从HashMap<String, String>向充气行中倾倒的数据,正确处理行回收。来自This free excerptone of my books将展示其中一些内容。

然后,只有这样,将工作ArrayAdapter包裹在EndlessAdapter

此外,永远不要硬线路径,因为/sdcard在大多数Android设备上都是错误的。