Reputation:
So I have this Activity B with textview. The string to be displayed into textview comes from my previous Activity A (a result query which contains more than one record/Passed thru Bundle )
My question is how do you make each record clickable? Note that in my Activity B, I only have one textview for the string from Activity A. I want each word to be clickable and then it will go to its corresponding activity.
FYI, This is the selected
UPDATED:
Result.class
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.symptomsresult);
result = (TextView) findViewById (R.id.tvSymptomResult);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("results");
result.setText(value);
}
else{
}
}
Symptoms.class
String[] symptoms = alSymptoms.toArray(new String[alSymptoms.size()]);
String c = dbHelper.getData(symptoms);
final int result = 1;
Bundle extras = new Bundle();
Intent i = new Intent(this, SymptomsResult.class);
extras.putString("results", c);
i.putExtras(extras);
startActivityForResult(i, result);
DBHelper.class
public String getData(String[] symptoms) {
String where = KEY_SYMPTOMS + "= ?";
String orStr = " OR "; /* Maybe " AND "? */
StringBuilder builder = new StringBuilder(where);
for(int i = 1; i < symptoms.length; i++)
builder.append(orStr).append(where);
String search = "";
Cursor c = myDataBase.query(true, DB_TABLE, new String[]
{KEY_CONDITIONS}, builder.toString(), symptoms, null, null, null, null);
c.moveToFirst();
while (c.isAfterLast() == false) {
search += c.getString(0) + ", ";
c.moveToNext();
}
return search;
}
Any help is appreciated. Thanks!
Upvotes: 0
Views: 651
Reputation: 18440
You can use HTML href
as the value of your text and then add this line to make it appear as web link
txtView.setMovementMethod(LinkMovementMethod.getInstance());
You can open your activity on click by making the link with a custom scheme
Example code:
txtView.setText(Html.fromHtml(String.format("<a href='%s://%s'>%s</a>",
schema,action,text)));
And for the activity that you want to open when this link is clicked you have to add the following in the manifest
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.VIEW" />
<data
android:host = "YOUR_ACTION"
anroid:scheme="YOUR_SCHEME" />
</intent-filter>
The trick here that you are creating your custom URL and instead of the host
you have an action that can be set to launch different activities. Note that action/host
and scheme
can be any arbitrary string as long as they match with the values you use when creating the link
So when you click that text with such link it will invoke the activity that matches scheme/host
I am using this solution in very similar case to yours and it is working perfectly.
Upvotes: 1
Reputation: 86948
You can do this with the Linkify class and Spannables. But I believe a ListView is a better choice, the items are easier to click because they are bigger and this approach has much more documentation.
You can still use the code I wrote in getData()
but rather than turn the Cursor into a String, pass your Cursor to a SimpleCursorAdapter.
So how do I put them in a listview?
Modify getData()
like so:
public Cursor getData(String[] symptoms) {
String where = KEY_SYMPTOMS + "= ?";
String orStr = " OR "; /* Maybe " AND "? */
StringBuilder builder = new StringBuilder(where);
for(int i = 1; i < symptoms.length; i++)
builder.append(orStr).append(where);
return myDataBase.query(true, DB_TABLE, new String[]
{"rowid as _id", KEY_CONDITIONS}, builder.toString(), symptoms, null, null, null, null);
}
Then pass this Cursor to a CursorAdapter and bind it to a ListView:
String[] fromColumns = {DBAdapter.KEY_CONDITIONS};
int[] toViews = {android.R.id.text1}; // The TextView in simple_list_item_1
setListAdapter(new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, getData(symptoms),
fromColumns, toViews, 0));
(I made a few assumptions here, but this is how to bind a Cursor to a ListActivity.)
Upvotes: 1