根据选中的复选框,在textView中显示字符串数组值

时间:2014-08-21 17:58:25

标签: android

这可能有点太多了,但我需要它作为我的应用程序中的核心功能。我在strings.xml中得到了几个用string-array组成的类别:

<string-array name="sport_array">
    <item>Juggle a soccer ball for at least 25 times!</item>
</string-array>

<string-array name="ff_array">
    <item>Make your friend or family member some scrambled egg for breakfast!</item>
    <item>Play hide and seek with your friends!</item>
    <item>Go outside and pick some flowers for your best friend!</item>
</string-array>

<string-array name="money_array">
    <item>Head over to the movie house!</item>
</string-array>

<string-array name="outside_array">
    <item>Go outside and pick some flowers for your best friend!</item>
</string-array>

<string-array name="school_array">
    <item>Help your neighbour with his homework!</item>
    <item>During your lunch break, go talk to one of your teachers! They might have something interesting to share with you</item>
</string-array>

<string-array name="fitness_array">
    <item>Do 10 push-ups without stopping</item>
    <item>Do 15 dips without stopping</item>
</string-array>

<string-array name="photo_array">
    <item>Take a selfie with a stranger!</item>
    <item>Give it your best, take the best picture in history!</item>
</string-array>

<string-array name="beach_array">
    <item>Gather with your best friends and have a BBQ at the beach</item>
</string-array>

<string-array name="food_array">
    <item>Make your family some cupcakes!</item>
    <item>Make your friend or family member some scrambled egg for breakfast!</item>
</string-array>

它们位于Listview中,每行包含一个复选框和textview。 MyActivity3:

public class MyActivity3 extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my3);
    Button m = (Button) findViewById(R.id.button3);
    tv = (TextView) findViewById(R.id.textViewcat);
    Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
    tv.setTypeface(typeface);



    String[] listArray = new String[] { "All", "Friends", "Family", "Sports", "Outside","Money", "At School", "Fitness", "Photography", "Food", "Beach",  };
    SharedPreferences sharedPreferences = getSharedPreferences("status", MODE_PRIVATE);

    Boolean[] checkedStatus = new Boolean[listArray.length];
    for ( int index = 0; index < checkedStatus.length; index++)
        checkedStatus[index] = sharedPreferences.getBoolean(Integer.toString(index), false);

    ListView listView = (ListView) findViewById(R.id.listView);
    MyAdapter adapter = new MyAdapter(this, R.layout.row_layout, listArray, checkedStatus);
    listView.setAdapter(adapter);
    listView.setOverScrollMode(View.OVER_SCROLL_NEVER);

}

在另一个活动中,MyActivity2有一个textview,其中的值从allthings_array加载:

<string-array name="allthings_array">
    <item>Make your friend or family member some scrambled egg for breakfast!</item>
    <item>Juggle a soccer ball for at least 100 times</item>
    <item>Head over to the movie house</item>
    <item>Take a selfie with a stranger!</item>
    <item>Play hide and seek with your friends!</item>
    <item>Do 10 push-ups without stopping</item>
    <item>Go outside and pick some flowers for your best friend!</item>
    <item>Help your neighbour with his homework!</item>
    <item>Make your family some cupcakes!</item>
    <item>Give it your best, take the best picture in history!</item>
    <item>Do 15 dips without stopping</item>
    <item>During your lunch break, go talk to one of your teachers! They might have something interesting to share with you</item>
    <item>Gather with your best friends and have a BBQ at the beach</item>
</string-array>

MyActivity2:

public class MyActivity2 extends Activity {
private String[] colors;
private String[] values;
private TextView tv;
private RelativeLayout rl;
Button n;
int index = 0;
int position2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_activity2);
    n = (Button) findViewById(R.id.button);
    tv = (TextView) findViewById(R.id.textView);
    rl = (RelativeLayout) findViewById(R.id.layout_view);
    Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
    n.setTypeface(typeface);
    Typeface face = Typeface.createFromAsset(getAssets(), "OSP-DIN.ttf");
    tv.setTypeface(face);
    values = getResources().getStringArray(R.array.allthings_array);
    colors = getResources().getStringArray(R.array.colorcode_array);

            n.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            if (index == 13) {
                                Toast.makeText(getApplicationContext(), "More coming each Sunday!", Toast.LENGTH_SHORT).show();
                            } else {
                                position2 = (index++);
                            }
                            String textValue = values[position2];
                            tv.setText(textValue);
                            Random RAND = new Random();
                            int position = RAND.nextInt(colors.length);
                            String nextValue = colors[position];
                            rl.setBackgroundColor(Color.parseColor(nextValue));
                            n.setBackgroundColor(Color.argb(00, 00, 00, 00));
                            return true;
                        case MotionEvent.ACTION_UP:
                            n.setBackgroundColor(Color.argb(00, 00, 00, 00));
                            return true;
                        default:
                            return false;
                    }
                }
            });
        }

现在我需要的是基于用户检查的复选框,textView必须从string_array中加载适当的值。因此,例如,如果我在MyActivity3中检查“Beach”和“Money”,则MyActivity2中的textview必须仅加载来自beach_array和money_array的值。

我该怎么做?如果示例基于我的代码

,我会对它进行说明

编辑:

MyActivity:

public class MyActivity extends Activity {
private String[] colors;
private RelativeLayout rl2;
private MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    Button n = (Button) findViewById(R.id.button);
    Button m = (Button) findViewById(R.id.button3);
    Button v = (Button) findViewById(R.id.buttonabout);
    rl2 = (RelativeLayout) findViewById(R.id.layout_view2);
    Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
    n.setTypeface(typeface);
    v.setTypeface(typeface);
    m.setTypeface(typeface);
    colors = getResources().getStringArray(R.array.colorcodebored_array);
    Random RAND = new Random();
    int position = RAND.nextInt(colors.length);
    String nextValue = colors[position];
    rl2.setBackgroundColor(Color.parseColor(nextValue));

    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    player = MediaPlayer.create(this, R.raw.pleasant);
    player.start();
}

public void openNewActivity(View view) {
// Do something in response to button
    Intent intent = new Intent(this, MyActivity2.class);
    startActivity(intent);
    overridePendingTransition(R.anim.animation, R.anim.animation2);
}

public void openNewActivity2(View view) {
// Do something in response to button
    Intent intent = new Intent(this, MyActivity3.class);
    startActivity(intent);
    startActivityForResult(intent, 1);//Best if you use some static int
    overridePendingTransition(R.anim.animation5, R.anim.animation6);
}

public void openNewActivity3(View view) {
// Do something in response to button
    Intent intent = new Intent(this, MyActivity4.class);
    startActivity(intent);
    overridePendingTransition(R.anim.animation, R.anim.animation2);
    }
}

1 个答案:

答案 0 :(得分:0)

好的,既然这两个活动都是从你的“主屏幕”调用的,你只需要使用方法“onActivityResult”,并在调用你的活动时在你的意图中传递信息。

在调用包含您的复选框列表的活动时,您将开始期待结果的活动(已检查的值):

Intent myIntent = new Intent(this, MyCheckboxActiity.class);
startActivityForResult(myIntent, 1);//Best if you use some static int

完成复选框活动后,您会将所有值放在意图中,并将其作为主屏幕活动的结果发布:

 Intent myIntent = getIntent();
 myIntent.putExtra(MY_VALUE, myValue);
 setResult(MY_ACTIVITY_RESULT, myIntent);
 finish();

这将调用您的方法onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //Store your data or do what you want
}

由于您知道您的数据存储在您的活动中,您可以通过意图调用您的其他活动传递信息:)