这是我用来在可扩展列表视图中显示项目的活动。目前,我已经对可扩展列表视图中的值进行了硬编码。
String subTotal = getIntent().getStringExtra("subTotal"); // I have these 2 values
String price = getIntent().getStringExtra("price");
这是我想要动态加载的2个值。
在下面我有prepareListData()
,其中我的ExpandableListView有硬编码值。
listDataHeader.add("Top 250"); // subTotal
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank"); //price
我想知道如何用动态值替换这些硬编码值。任何帮助都将受到高度赞赏。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cart_activity);
String subTotal = getIntent().getStringExtra("subTotal"); // i ahve these 2 values
String price = getIntent().getStringExtra("price");//
ActionBar actionBar = getActionBar();
getActionBar().setIcon(
new ColorDrawable(getResources().getColor(android.R.color.transparent)));
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
// Enabling Back navigation on Action Bar icon
actionBar.setDisplayHomeAsUpEnabled(true);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Toast.makeText(getApplicationContext(),
"Group Clicked " + listDataHeader.get(groupPosition),
Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Top 250"); // subTotal
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank"); //price
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
mExpandableList = (ExpandableListView)findViewById(R.id.lvExp);
mExpandableList.setIndicatorBounds(width - GetPixelFromDips(50), width - GetPixelFromDips(10)); }
public int GetPixelFromDips(float pixels) {
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
答案 0 :(得分:0)
我能够通过这样做来克服这一点。
String price;
String subTotal ;
subTotal = getIntent().getStringExtra("subTotal");
price = getIntent().getStringExtra("price");
listDataHeadersubTotal= new ArrayList<String>();
listDataHeaderPrice= new ArrayList<String>();
listDataHeadersubTotal.add(subTotal);
listDataHeaderPrice.add(price);
List<String> price= new ArrayList<String>();
price.add("The Shawshank");
List<String> subTotal= new ArrayList<String>();
subTotal.add("The Shawshank");
listDataChild.put(listDataHeadersubTotal.get(0), subTotal);
listDataChild.put(listDataHeaderPrice.get(0), price);