ArrayAdapter>>与 commonsguy EndlessAdapter 类

发布于 2024-12-22 10:03:14 字数 4076 浏览 0 评论 0 原文

如何将 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());
                }
          }
        }
      }
    }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与往事干杯 2024-12-29 10:03:14

您的问题与EndlessAdapter无关。

首先,让您的代码使用 ArrayAdapter,暂时忽略 EndlessAdapter。这将要求您学习如何创建自定义 ArrayAdapter,因为您很快就会发现 ArrayAdapter 对如何使用 HashMap 知之甚少。 。您需要将 ArrayAdapter 扩展为您自己的 WhateverThisIsAdapter,在其中重写 getView() 以处理来自 HashMap 到膨胀的行中,正确处理行回收。 此免费摘录来自 我的一本书将展示其中的一些内容。

然后,也只有到那时,才能将正在工作的 ArrayAdapter 包装在 EndlessAdapter 中。

另外,切勿硬连线路径,因为 /sdcard 在大多数 Android 设备上都是错误的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文