2016-11-21 10 views
-1

Ich möchte zu einer anderen Aktivität gelangen, indem ich auf ListView Element (zum Beispiel Farbliste) klicke auf schwarz 'Ich möchte zu einer anderen Aktivität mit schwarzem Hintergrund gelangen. Thats, was ich für jetzt habe: public class SecondActivity erweitert AppCompatActivity { ArrayList allColors;Listview Farbliste

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

    final ListView listView=(ListView)findViewById(R.id.colorList); 
    allColors= new ArrayList<>(); 
    allColors.add("Blue"); 
    allColors.add("Yellow"); 
    allColors.add("Green"); 
    allColors.add("Red"); 
    allColors.add("Grey"); 
    allColors.add("Black"); 
    allColors.add("Orange"); 
    allColors.add("Purple"); 

    ArrayAdapter<String>myAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,allColors); 
    listView.setAdapter(myAdapter); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
+0

hinzuzufügen Bitte klären Sie Ihre Frage. –

+0

Ich möchte zu einer neuen Aktivität gelangen, indem ich auf einen Eintrag in der Listenansicht drücke. Ich habe eine Liste von Farben, indem ich auf eines der Elemente klicke, um zu einer neuen Aktivität mit der gleichen Farbe auf dem Hintergrund zu gelangen. –

Antwort

0

können Sie verwenden enum mit HashMap als Farbnamen und die Hex-Codes Speicher und dann Hex-Code der Farbe als String für die zweite Activity über Intent passieren:

MainActivity.java

public class MainActivity extends AppCompatActivity { 

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

     List<String> allColors= new ArrayList<>(8); 
     allColors.add(Color.Blue.toString()); 
     allColors.add(Color.Yellow.toString()); 
     allColors.add(Color.Green.toString()); 
     allColors.add(Color.Red.toString()); 
     allColors.add(Color.Grey.toString()); 
     allColors.add(Color.Black.toString()); 
     allColors.add(Color.Orange.toString()); 
     allColors.add(Color.Purple.toString()); 

     final ListView listView = (ListView)findViewById(R.id.colorList); 

     ArrayAdapter<String> myAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, android.R.id.text1, allColors); 
     listView.setAdapter(myAdapter); 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Intent intent = new Intent(MainActivity.this, SecondActivity.class); 
       intent.putExtra("BackgroundColor", Color.fromString(((TextView)view).getText().toString()).getHexCode()); 
       startActivity(intent); 
      } 
     }); 

    } 

    public enum Color { 
     Blue("#0000FF"), 
     Yellow("#FFFF00"), 
     Green("#00FF00"), 
     Red("#FF0000"), 
     Grey("#808080"), 
     Black("#000000"), 
     Orange("#FFA500"), 
     Purple("#800080"); 

     Color(String color) { 
      this.colourCode = color; 
     } 

     final String colourCode; 
     private final static Map<Color, String> colorNames = new HashMap<>(8); 


     static { 
      colorNames.put(Blue, "Blue"); 
      colorNames.put(Yellow, "Yellow"); 
      colorNames.put(Green, "Green"); 
      colorNames.put(Red, "Red"); 
      colorNames.put(Grey, "Grey"); 
      colorNames.put(Black, "Black"); 
      colorNames.put(Orange, "Orange"); 
      colorNames.put(Purple, "Purple"); 
     } 

     public String getHexCode() { 
      return colourCode; 
     } 

     @Override 
     public String toString() { 
      return colorNames.get(this); 
     } 

     public static Color fromString(String name) { 
      for (Color key : colorNames.keySet()) { 
       if (colorNames.get(key).equals(name)) { 
        return key; 
       } 
      } 
      return null; 
     } 
    } 
} 

SecondActivity.java

public class SecondActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_second); 
     String backgroundColor = "#FFFFFF"; 
     Intent intent = getIntent(); 
     if (intent != null) { 
      backgroundColor = intent.getStringExtra("BackgroundColor"); 
     } 
     getWindow().getDecorView().setBackgroundColor(Color.parseColor(backgroundColor)); 
    } 
} 

Und vergessen Sie nicht, SecondActivity zu AndroidManifest.xml

+0

Vielen Dank! /// –

+0

Also, wenn es hilft, kannst du antworten :) –