我正在使用下面的代码来填充我的列表视图,但我想使用另一个显示动画的代码。我真的很想知道如何进行操作并合并下面的代码。对我来说主要问题是我ArrayList<HashMap<String, String>>
用于存储数据,而其他代码使用ArrayList<String>
. 您的帮助将不胜感激,并将引导我进一步了解带有 Hashmap 的 BaseAdapters。
我正在使用下面的代码:
ArrayList<HashMap<String, String>> productsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
//bMain = (Button) findViewById(R.id.main);
// Loading products in Background Thread
new LoadAllProducts().execute();
// Get listview
ListView lv = getListView();
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
//Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
protected void onPostExecute(String file_url) {
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, productsList,
R.layout.list_item, new String[] { TAG_PID,
TAG_NAME},
new int[] { R.id.pid, R.id.name });
//updating listview
setListAdapter(adapter);
}
}
我现在正在努力如何使用下面的代码来启用动画:
AlphaInActivity
public class AlphaInActivity extends MyListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BaseAdapter mAdapter = createListAdapter();
AlphaInAnimationAdapter aplhaInAnimationAdapter = new AlphaInAnimationAdapter(mAdapter);
aplhaInAnimationAdapter.setAbsListView(getListView());
getListView().setAdapter(aplhaInAnimationAdapter);
}
}
我的列表活动
public class MyListActivity extends ListActivity {
private static ArrayList<HashMap<String, String>> productsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getListView().setDivider(null);
}
protected ArrayAdapter<String> createListAdapter() {
return new MyListAdapter(this, getItems());
}
public static ArrayList<String> getItems() {
ArrayList<String> items = new ArrayList<String>();
for (int i = 0; i < 1000; i++) {
items.add(String.valueOf(i));
}
return items;
}
private static class MyListAdapter extends ArrayAdapter<String> {
private Context mContext;
public MyListAdapter(Context context, ArrayList<String> items) {
super(items);
mContext = context;
}
@Override
public long getItemId(int position) {
return getItem(position).hashCode();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = (TextView) convertView;
if (tv == null) {
tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.list_row, parent, false);
}
tv.setText("This is row number " + getItem(position));
return tv;
}
}
}