如何使用ArrayAdapter-HashMap-與EndlessAdapter類?ArrayAdapter <HashMap<,,>>與commonsguy 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
,但不知道如何正確使用它。
下面是完整的代碼,它的工作,但我得到的列表視圖(文本)類似{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());
}
}
}
}
}
檢查這個博客http://www.vogella.de/articles/AndroidListView/article.html – Pratik 2011-12-21 10:23:26
試圖與simpleadapter更換arrayadapter但得到崩潰:'(超(新SimpleAdapter( \t \t \t \t ReadChat.this, \t \t \t \t物品, \t \t \t \t R.layout.row, 新的String [] { 「測試1」, 「TEST2」}, 新INT [] {R.id.text1,R.id.text2 }));' – Dikobraz 2011-12-21 11:28:49