簡體   English   中英

如何使用嵌套開關案例顯示數據?

[英]how display data using nested switch case?

[我的項目圖]

動作= 1

    String[] values = new String[] { "Android", "iPhone", "WindowsMobile"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
     android.R.layout.simple_list_item_1, android.R.id.text1, values);
      lisdis.setAdapter(adapter); 
      lisdis.setOnItemClickListener(this);

}
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
{

    Intent mIntent = new Intent(display_publishermagazine.this, display_publicationmagazine.class);
        mIntent.putExtra("position", position+1);
        startActivity(mIntent);

行動= 2

    Intent mIntent = getIntent();
    int intValue = mIntent.getIntExtra("position",0);

   switch (intValue)
    {
        case 1:values = new String[] { "Android For Beginer","Android Devloper" };break;
        case 2:values = new String[] { "I-phone for beginer","I-phone for devloper" };break;
        case 3:values = new String[] { "windows for beginer","windows for devloper" };break;
    }


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
         android.R.layout.simple_list_item_1, android.R.id.text1, values);

lisdis.setAdapter(adapter); 
    lisdis.setOnItemClickListener(this);        
}

@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
{
    Intent myIntent = new Intent(display_publishermagazine.this, display_publicationmagazine.class);


    myIntent.putExtra("position", position+1);
    startActivity(myIntent);
   }

行為3

      Intent myIntent = getIntent(); 
      int intValue = myIntent.getIntExtra("position", 0);

    switch (intValue)
    {
        case 1:values = new String[] { "Android for lerner1","android for learner2" };break;
        case 2:values = new String[] { "Android  for devloper1","android for devloper2" };break;
    }

/*values = new String[] { "iphone for lerner1","iphone for learner2" }      
values = new String[] { "iphone for devloper1","iphone for devloper2} */
}

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,

             android.R.layout.simple_list_item_1, android.R.id.text1, values);

        g1.setAdapter(adapter); 
}

我需要

  • 當用戶單擊android時,使用切換案例在另一個活動中顯示數據
  • 然后顯示數據android Lerner或android開發人員。然后單擊android學習器
  • 然后顯示android lerner1或android Learner2
  • 然后選擇android開發人員,然后顯示android lerner1或android Learner2。

在這里,它僅適用於android,不適用於iphone或window。

這個怎么樣

用法: TutorialUtils.getMenu(); TutorialUtils.getMenu(new int[] { 0 })TutorialUtils.getMenu(new int[] { 2, 1 })或在您的情況下進行一些修改后的TutorialUtils.getMenu(getIntent().getExtras().getIntArray("positionsHistory"))

TutorialUtils類,自己處理異常。

import java.util.ArrayList;
import java.util.List;

public class TutorialUtils {
private static CompositeTutorialNode nodes = new CompositeTutorialNode(
        "root");
static {
    nodes = new CompositeTutorialNode("root")
            .add(new CompositeTutorialNode("Android").add(
                    new CompositeTutorialNode("Android For Developers").add(
                            new TextTutorialNode("Android for Developer1"))
                            .add(new TextTutorialNode(
                                    "Android for Developer2"))).add(
                    new CompositeTutorialNode("Android For Learners").add(
                            new TextTutorialNode("Android for Learner1"))
                            .add(new TextTutorialNode(
                                    "Android for Learner2"))))
            .add(new CompositeTutorialNode("iPhone Sucks !!")
                    .add(new CompositeTutorialNode("iPhone Sucks For Developers")
                            .add(new TextTutorialNode(
                                    "iPhone Sucks for Developer1")).add(
                                    new TextTutorialNode(
                                            "iPhone Sucks for Developer2")))
                    .add(new CompositeTutorialNode("iPhone For Learners")
                            .add(new TextTutorialNode("iPhone Sucks for Learner1"))
                            .add(new TextTutorialNode("iPhone Sucks for Learner2"))))
            .add(new CompositeTutorialNode("Windows").add(
                    new CompositeTutorialNode("Windows For Developers").add(
                            new TextTutorialNode("Windows for Developer1"))
                            .add(new TextTutorialNode(
                                    "Windows for Developer2"))).add(
                    new CompositeTutorialNode("Windows For Learners").add(
                            new TextTutorialNode("Windows for Learner1"))
                            .add(new TextTutorialNode(
                                    "Windows for Learner2"))));

}

public static String[] getMenu() {
    TutorialNode tmpNode = nodes;
    return prepareNames(tmpNode);
}

private static String[] prepareNames(TutorialNode node) {
    List<String> names = new ArrayList<String>();
    if (node instanceof CompositeTutorialNode) {
        List<TutorialNode> childs = ((CompositeTutorialNode) node).get();
        for (TutorialNode n : childs) {
            names.add(n.getName());
        }
    }
    return names.toArray(new String[] {});
}

public static String[] getMenu(int[] position) {
    TutorialNode tmpNode = nodes;
    for (int i : position) {
        if (tmpNode instanceof CompositeTutorialNode) {
            tmpNode = ((CompositeTutorialNode) tmpNode).get(i);
        }
    }
    return prepareNames(tmpNode);
}

interface TutorialNode {

    public String getName();
}

public static class CompositeTutorialNode implements TutorialNode {
    private List<TutorialNode> childs = new ArrayList<TutorialNode>();
    String name;

    public CompositeTutorialNode(String name) {
        this.name = name;
    }

    public CompositeTutorialNode add(TutorialNode node) {
        childs.add(node);
        return this;
    }

    public TutorialNode get(int index) {
        return childs.get(index);
    }

    public List<TutorialNode> get() {
        return childs;
    }

    @Override
    public String getName() {
        return name;
    }
}

public static class TextTutorialNode implements TutorialNode {
    String name;

    public TextTutorialNode(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return name;
    }
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM