2016-10-11 3 views
0

Ich versuche, ein oder mehrere Elemente in einer erweiterbaren Listenansicht hervorzuheben. Ich habe viele Lösungen gefunden, aber nichts konnte mir helfen. Ich hoffe, jemand kann mir helfen. Ich habe eine Datenbank mit Cocktails. In der App kannst du einen neuen Cocktail kreieren. Die Zutaten eines Cocktails sollten vom Benutzer in einer erweiterbaren Listenansicht ausgewählt werden. Ich kann nur einen Gegenstand gleichzeitig markieren. Für eine bessere Benutzererfahrung ist es jedoch wichtig, dass der Benutzer mehr als ein Element auswählen kann. Wenn er alle Zutaten ausgewählt hat, kann er seine Auswahl speichern und die Aktivität wird geschlossen. Wenn er eine Zutat vergessen hat, kann er die Aktivität erneut starten und er wird alle markierten Elemente sehen, die er gerade ausgewählt und die vergessenen Gegenstände ausgewählt hat.Markieren Sie alle ausgewählten Elemente in einer erweiterbaren Listenansicht

Ich hoffe, mein Englisch ist nicht so schlecht und Sie können verstehen, was ich meine und mir helfen.

ist die Aktivität, was für einen Cocktail Zutaten zu wählen beginnen:

public class SelectIngredientByCategory extends AppCompatActivity { 
private ExpandableListView ingredients; 
private ExpandableListAdapter listAdapter; 
private List<String> listDataHeader; 
private HashMap<String, List<String>> listDataChild; 
private DBConnection db; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_select_ingredient_by_category); 

    db = new DBConnection(getApplication()); 

    /* ... */ 

    ingredients = (ExpandableListView) findViewById(R.id.elvIngredientsByCategory); 
    prepareListData(); 
    listAdapter = new ExpandableListAdapter(getApplication(), listDataHeader, listDataChild); 

    ingredients.setAdapter(listAdapter); 
    ingredients.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 
      String ingredientName = (String) listAdapter.getChild(groupPosition, childPosition); 

      /* ... */ 

      /* 
      * if item is selected 
      * mark item blue 
      * if not 
      * mark item red 
      */ 

      return false; 
     } 
    }); 
} 

public void prepareListData() { 
    listDataHeader = new ArrayList<String>(); //category 
    listDataChild = new HashMap<String, List<String>>(); //ingredient 

    listDataHeader = db.getAllCategoryIngredientsName(); 

    List<String> tmp; 

    for (int i = 0; i < listDataHeader.size(); i++) { 
     tmp = db.getAllIngredientByCategory(listDataHeader.get(i)); 
     listDataChild.put(listDataHeader.get(i), tmp); 
    } 
} 

/* ... */ 

}

Das ist mein Adapter:

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

private Context _context; 
private List<String> category; 
private HashMap<String, List<String>> ingredient; 

public ExpandableListAdapter(Context context, List<String> category, 
          HashMap<String, List<String>> ingredient) { 
    this._context = context; 
    this.category = category; 
    this.ingredient = ingredient; 
} 

@Override 
public Object getChild(int groupPosition, int childPosititon) { 
    return this.ingredient.get(this.category.get(groupPosition)) 
      .get(childPosititon); 
} 

@Override 
public long getChildId(int groupPosition, int childPosition) { 
    return childPosition; 
} 

@Override 
public View getChildView(final int groupPosition, final int childPosition, 
         boolean isLastChild, View convertView, ViewGroup parent) { 

    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.exp_list_item, null); 
    } 

    final String childText = (String) getChild(groupPosition, childPosition); 

    TextView child = (TextView) convertView 
      .findViewById(R.id.lblListChild); 
    child.setText(childText); 

    return convertView; 
} 

@Override 
public int getChildrenCount(int groupPosition) { 
    return this.ingredient.get(this.category.get(groupPosition)) 
      .size(); 
} 

@Override 
public Object getGroup(int groupPosition) { 
    return this.category.get(groupPosition); 
} 

@Override 
public int getGroupCount() { 
    return this.category.size(); 
} 

@Override 
public long getGroupId(int groupPosition) { 
    return groupPosition; 
} 

@Override 
public View getGroupView(final int groupPosition, boolean isExpanded, 
         View convertView, ViewGroup parent) { 
    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.exp_list_group, null); 
    } 

    String headerTitle = (String) getGroup(groupPosition); 

    final TextView lblListHeader = (TextView) convertView 
      .findViewById(R.id.lblListHeader); 
    lblListHeader.setTypeface(null, Typeface.BOLD); 
    lblListHeader.setText(headerTitle); 

    return convertView; 
} 

@Override 
public boolean hasStableIds() { 
    return false; 
} 

@Override 
public boolean isChildSelectable(int groupPosition, int childPosition) { 
    return true; 
} 

}

Und hier die xml-Dateien:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".Ingredient.SelectIngredientByCategory" 
android:orientation="vertical"> 

<ExpandableListView 
    android:id="@+id/elvIngredientsByCategory" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:divider="#b5b5b5" 
    android:dividerHeight="1dp" 
    android:padding="1dp" 
    android:choiceMode="multipleChoice"/> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TextView 
    android:id="@+id/lblListHeader" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="17dp" 
    android:textColor="#000000"/> 

</LinearLayout> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TextView 
    android:id="@+id/lblListChild" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="17dp"/> 

</LinearLayout> 

EDIT: Ich fand eine Lösung für mein Problem. Danke für die Hilfe. :)

Antwort

0

Sie Hintergrundfarbe auf das ausgewählte Kind Ansicht

v.setBackgroundColor(Color.parseColor("#4482e5")); 
+0

Vielen Dank für Ihre Antwort festlegen. Aber was ist, wenn der Benutzer die falsche Zutat auswählt? Ich habe diesen Weg versucht, aber ich konnte die Farbe nicht wieder auf die Standardfarbe ändern. Ich habe das mit setSelected versucht, aber es funktioniert nicht. Ich könnte nur ein oder mehrere Elemente hervorheben. Aber abwählen funktioniert nicht ... Irgendeine Idee, wie kann ich das machen? – maddy

Verwandte Themen