Issue
I don't know whether what I want is possible (I'm sure it is to the experts) but my problem is this:
I have an activity that has a listview of electronic devices, etc, which has been set up in an array list. When I click on an item in the list, it opens another activity and displays an image using the code below:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_evidence_items);
toolbar = (Toolbar) findViewById(R.id.toolbar);
evidenceItem = (ImageView) findViewById(R.id.imageViewEvidenceItem);
evidenceInfo = (TextView) findViewById(R.id.textViewEvidenceInfo);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
toolbar.setTitle(bundle.getString("EvidenceType"));
if (toolbar.getTitle().toString().equalsIgnoreCase("Main computer unit")) {
evidenceItem.setImageDrawable(ContextCompat.getDrawable(EvidenceItemsActivity.this,
R.drawable.tower));
}else if (toolbar.getTitle().toString().equalsIgnoreCase("Monitor")) {
evidenceItem.setImageDrawable(ContextCompat.getDrawable(EvidenceItemsActivity.this,
R.drawable.monitor));
}else if (toolbar.getTitle().toString().equalsIgnoreCase("Keyboard and mouse")) {
evidenceItem.setImageDrawable(ContextCompat.getDrawable(EvidenceItemsActivity.this,
R.drawable.keyboardmouse));
The list is longer but I didn't want to bore you all.
You will see I have also set up a textview (textViewEvidenceInfo) which is where my question has arisen. I want to know if it's possible to set up a string resource (like the below)....
<string name="harddisk_txt">Protect from magnetic fields. Place in anti-static bags or in tough paper bags
or wrap in paper and place in aerated bags.</string>
....and put it with the bundle in order to add it to the activity, under the (drawable) image.
Could it be added to the IF statement or does it need to be separate? If indeed it is possible.
The adapter with the intent is below:
lvEvidence = (ListView) findViewById(R.id.lvEvidence);
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(EvidenceTypesActivity.this,
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.seized_item));
lvEvidence.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(EvidenceTypesActivity.this, EvidenceItemsActivity.class);
intent.putExtra("EvidenceType", lvEvidence.getItemAtPosition(i).toString());
startActivity(intent);
}
});
lvEvidence.setAdapter(mAdapter);
}
Hope this makes sense to someone.
Thanks in anticipation
Solution
EDITED:
Let's say that you have the following strig-array
<string-array name="seized_item">
<item>name1</item>
<item>name2</item>
<item>name3</item>
<item>name4</item>
<item>name5</item>
<item>name...</item>
</string-array>
<string-array name="seized_item_txt">
<item>name1 text: Protect from magnetic fields. </item>
<item>name2 text:</item>
<item>name3 text:</item>
<item>name4 text:</item>
<item>name5 text:</item>
<item>name... text:</item>
</string-array>
In your click listener put the following code:
Intent intent = new Intent(EvidenceTypesActivity.this, EvidenceItemsActivity.class);
intent.putExtra("EvidenceType", lvEvidence.getItemAtPosition(i).toString());
intent.putExtra("EvidenceText", getResources().getStringArray(R.array.seized_item_txt)[i]);
startActivity(intent);
In your other activity:
evidenceItem.setImageDrawable(...)
evidenceInfo.setText(bundle.getString("EvidenceText"))
----last answer-----
If I got you right, you can just add another parameter to the intent:
Intent intent = new Intent(getBaseContext(), evidenceActivity.class);
intent.putExtra("EvidenceType", evidenceType);
intent.putExtra("text", getString(R.string.harddisk_txt));
startActivity(intent);
And in the next activity:
toolbar.setTitle(getIntent().getStringExtra("EvidenceType"));
String text = getIntent().getStringExtra("EvidenceText");
Or if you would like to use the Bundle object:
Intent intent = new Intent(getBaseContext(), evidenceActivity.class);
Bundle b = new Bundle();
b.putString("EvidenceType", evidenceType);
b.putString("text", getString(R.string.harddisk_txt));
intent.putExtras(b);
startActivity(intent);
And in the next activity:
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
toolbar.setTitle(bundle.getString("EvidenceType"));
String text = bundle.getString("text")
Answered By - regev avraham
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.