Java:获取键集值

时间:2016-11-25 03:35:47

标签: java android-studio android-activity textview sharedpreferences

我需要知道如何将某个键集的值传递到另一个活动表单中。我有一个列表,其中包含一个键和该键中的所有值。我想要它,以便当用户单击列表中的项目时,将加载另一个活动,并单击该特定项目的值。

例如,如果我有一个名为Running作为列表中的项目的键集,并且该集合的值是运行的StartTime和EndTime,我如何才能将属于该集合的值显示为下一个活动。这很难解释,但我希望我的代码能够清楚地说明我要求的内容。

public class ActivityLog extends Activity {
ListView listView ;



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

    // Get ListView object from xml
    listView = (ListView) findViewById(R.id.list);
    listView.setFriction(ViewConfiguration.getScrollFriction() * 0);
    // ListView Item Click Listener
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            int itemPosition     = position;
            String  itemValue    = (String) listView.getItemAtPosition(position);


            // ListView Clicked item index


            // ListView Clicked item value

            // Show Alert
           Toast.makeText(getApplicationContext(), "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG).show();

        }

    });
}

@Override
protected void onStart()
{
    SharedPreferences userInfo = getSharedPreferences("userData", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = userInfo.edit();
    String myString = userInfo.getAll().keySet().toString();
    String[] values = new String[] { myString };

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


    Map<String, ?> allEntries = userInfo.getAll();
    for (Map.Entry<String, ?> entry : allEntries.entrySet()) {

        Log.d("map values", entry.getKey() + ": " + entry.getValue().toString());
        adapter.add(entry.getKey()+ ": " + entry.getValue().toString());
        listView.setAdapter(adapter);

    }
    super.onStart();
}


public void showLog (View view){

    Intent intent = new Intent(this, ActivityNewLog.class);
    startActivity(intent);

}


public void deleteLog (View view){

    SharedPreferences settings = getSharedPreferences("userData", Context.MODE_PRIVATE);
    settings.edit().clear().commit();
    getSharedPreferences("userData", 0).edit().clear().commit();

}

public void dataShow (View view){

   Intent intent = new Intent(this, Log_Data.class);
    startActivity(intent);

}

}

这是我要加载值的活动:

public class Log_Data extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_log__data);
    TextView data = (TextView)findViewById(R.id.textView5);

    SharedPreferences userInfo = getSharedPreferences("userData", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = userInfo.edit();
    String myString = userInfo.getAll().keySet().toString();
    String[] values = new String[] { myString };

    //ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, android.R.id.text1 );
    Map<String, ?> allEntries = userInfo.getAll();
    for (Map.Entry<String, ?> entry : allEntries.entrySet()) {

        data.setText(entry.getKey()+ ": " + entry.getValue().toString());

    }


    }
}

我还应该提一下,我希望它们存储在textView中。

1 个答案:

答案 0 :(得分:1)

将值保存到String并使用onItemClick侦听器上的Bundle将值传递给另一个活动,从其他活动中检索值。例如 -

第一项活动

Intent i = new Intent(this, secondActivity.class);
    //Create the bundle
Bundle bundle = new Bundle();
//Add your data from getFactualResults method to bundle
bundle.putString("TAG", yourvalue);
//Add the bundle to the intent
i.putExtras(bundle);
startActivity(i);

在第二个活动中

Bundle bundle = getIntent().getExtras();

    //Extract the data…
    String something = bundle.getString("TAG");        

    //Create the text view
    TextView textView = new TextView(this);
    textView.setText(TAG);