2

如何将 ArrayAdapter-HashMap- 与 EndlessAdapter 类一起使用?

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

1 回答 1

2

你的问题与EndlessAdapter.

首先,让您的代码使用ArrayAdapter, 暂时忽略EndlessAdapter。这将要求您学习如何创建自定义ArrayAdapter,因为您很快就会发现ArrayAdapter对如何使用HashMap<String, String>. 您将需要扩展ArrayAdapter到您自己的WhateverThisIsAdapter,您可以在其中覆盖getView()以处理将数据从您的HashMap<String, String>行中倒入膨胀的行,正确处理您的行回收。我的一本书的这个免费摘录将展示其中的一些。

然后,并且只有在那时,将工作包装ArrayAdapter在一个EndlessAdapter.

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

于 2011-12-21T12:12:46.683 回答