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

例如,如果我有一个名为“正在作为一个项目作为列表中的项运行”的键集,并且该键集的值是运行的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中。

最佳答案

将值保存到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);

08-06 02:30