0
我有一個視圖與TextViews,ImageViews,但我想包括使用ExpandableList一些數據。我不知道如何將這個ExpandableList與其他數據一起包含在我的View中。如何在視圖中包含ExpandableListActivity
我objetive:
我已經除了ExpandableList在一個視圖中的所有組件,我有其他活動:
OfferDetailsActivity(這是活動昭示着查看並從這裏我想打電話到ExpandableList)
public class OfferDetailsActivity extends Activity{
final public static String OFERTA = "oferta";
private Oferta oferta = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.offer_details);
oferta = (Oferta)getIntent().getExtras().getParcelable(OFERTA);
View view = new View(this.getBaseContext());
LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = linflater.inflate(R.layout.offer_details, null);
// poblamos la lista de elementos
TextView precio = (TextView)view.findViewById(R.id.precio_offer_details);
precio.setText(oferta.getPrecio());
ImageView imagen = (ImageView) view.findViewById(R.id.imagen_offer_details);
imagen.setImageBitmap(new GetImages().downloadFile(oferta.getImagen()));
TextView cabecera = (TextView)view.findViewById(R.id.cabecera_offer_details);
cabecera.setText(oferta.getCabecera());
TextView descripcion = (TextView)view.findViewById(R.id.descripcion_offer_details);
descripcion.setText(oferta.getDescripcion());
setContentView(view);
}
}
ExpandableList
public class ExpandableList extends ExpandableListActivity {
@SuppressWarnings("unchecked")
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.offer_details);
SimpleExpandableListAdapter expListAdapter =
new SimpleExpandableListAdapter(
this,
createGroupList(), // Creating group List.
R.layout.group_row, // Group item layout XML.
new String[] { "Group Item" }, // the key of group item.
new int[] { R.id.row_name }, // ID of each group item.-Data under the key goes into this TextView.
createChildList(), // childData describes second-level entries.
R.layout.child_row, // Layout for sub-level entries(second level).
new String[] {"Sub Item"}, // Keys in childData maps to display.
new int[] { R.id.grp_child} // Data under the keys above go into these TextViews.
);
setListAdapter(expListAdapter); // setting the adapter in the list.
}catch(Exception e){
System.out.println("Errrr +++ " + e.getMessage());
}
}
/* Creating the Hashmap for the row */
@SuppressWarnings("unchecked")
private List createGroupList() {
ArrayList result = new ArrayList();
for(int i = 0 ; i < 15 ; ++i) { // 15 groups........
HashMap m = new HashMap();
m.put("Group Item","Group Item " + i); // the key and it's value.
result.add(m);
}
return (List)result;
}
/* creatin the HashMap for the children */
@SuppressWarnings("unchecked")
private List createChildList() {
ArrayList result = new ArrayList();
for(int i = 0 ; i < 15 ; ++i) { // this -15 is the number of groups(Here it's fifteen)
/* each group need each HashMap-Here for each group we have 3 subgroups */
ArrayList secList = new ArrayList();
for(int n = 0 ; n < 3 ; n++) {
HashMap child = new HashMap();
child.put("Sub Item", "Sub Item " + n);
secList.add(child);
}
result.add(secList);
}
return result;
}
public void onContentChanged () {
System.out.println("onContentChanged");
super.onContentChanged();
}
/* This function is called on each child click */
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,int childPosition,long id) {
System.out.println("Inside onChildClick at groupPosition = " + groupPosition +" Child clicked at position " + childPosition);
return true;
}
/* This function is called on expansion of the group */
public void onGroupExpand (int groupPosition) {
try{
System.out.println("Group exapanding Listener => groupPosition = " + groupPosition);
}catch(Exception e){
System.out.println(" groupPosition Errrr +++ " + e.getMessage());
}
}
}
offer_details.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView android:id="@+id/precio_offer_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="12px" />
<TextView android:id="@+id/cabecera_offer_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="12px" />
</TableRow>
<TableRow>
<ImageView android:id="@+id/imagen_offer_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="@+id/descripcion_offer_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="normal"
android:textSize="10px" />
</TableRow>
<TextView android:id="@+id/texto3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="normal"
android:textSize="10px"
android:text="texto3"/>
<TextView android:id="@+id/texto4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="normal"
android:textSize="10px"
android:text="texto4"/>
也許繼承問題...活動的一個活動擴展和ExpandableListActivity的其他擴展。我會盡力去做你說的。 – JoseAntonio1984
您可能需要做的唯一更改是查找可展開列表視圖(findViewById),因爲Activity不會假定該標識。你調用的設置適配器的方法是不同的(如果我正確記得,setAdapter vs setListAdapter)。 – Barak
如果將代碼從ExpandableList.class移動到OfferDetailsActivity.class出現編譯錯誤,例如,如果您從ExpandableListActivity擴展,則只有「createGroupList()」和「createChildList()」方法可用。爲了解決這個問題,我決定保留2個類並在OfferDetailsActivity.class中寫這行以在setContentView(view)之前調用其他類。不能正常工作:ExpandableListView oExpandableListView = new ExpandableListView(this.getBaseContext()); \t \t \t oExpandableListView.setAdapter(new ExpandableList()。getExpandableListAdapter()); – JoseAntonio1984