ArrayAdapter>>与 commonsguy EndlessAdapter 类
如何将 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_COLUMNS
和 VIEW_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());
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题与
EndlessAdapter
无关。首先,让您的代码使用 知之甚少。 。您需要将
ArrayAdapter
,暂时忽略EndlessAdapter
。这将要求您学习如何创建自定义 ArrayAdapter,因为您很快就会发现 ArrayAdapter 对如何使用 HashMapArrayAdapter
扩展为您自己的WhateverThisIsAdapter
,在其中重写getView()
以处理来自HashMap 到膨胀的行中,正确处理行回收。 此免费摘录来自 我的一本书将展示其中的一些内容。
然后,也只有到那时,才能将正在工作的 ArrayAdapter 包装在 EndlessAdapter 中。
另外,切勿硬连线路径,因为
/sdcard
在大多数 Android 设备上都是错误的。Your problems have nothing to do with
EndlessAdapter
.First, get your code working with an
ArrayAdapter
, ignoringEndlessAdapter
for now. This will require you to learn how to create a customArrayAdapter
, as you will quickly discover thatArrayAdapter
knows little about how to use aHashMap<String, String>
. You will need to extendArrayAdapter
to your ownWhateverThisIsAdapter
, where you overridegetView()
to handle pouring data from yourHashMap<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 anEndlessAdapter
.Also, never hard-wire paths, as
/sdcard
is wrong on most Android devices.