【问题标题】:Java: Get key set valuesJava:获取键集值
【发布时间】:2016-11-25 03:35:46
【问题描述】:

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

例如,如果我有一个名为 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 中。

【问题讨论】:

    标签: java android android-activity textview sharedpreferences


    【解决方案1】:

    将值保存到字符串并使用 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);
    

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2010-10-26
      • 2015-12-08
      • 1970-01-01
      • 2010-11-25
      • 2016-03-08
      • 2015-10-20
      相关资源
      最近更新 更多