-7

By this way i have showed ids from DB.

TextView setNote = (TextView) findViewById(R.id.idNotes);
setNote.setText("");  //empty current text

//DB connection
DatabaseHandler db = new DatabaseHandler(this);


List<Contact> contacts = db.getAll();

for (Contact cn : contacts) 
{
  Integer id = cn.getID();

  setNote.append(id.toString()+"\n");        
}

RESULT

12

35

42

Now i want to make these ids clickable such that when user clicks on each id, it will open a separate activity corresponding to this id. How can i do this?

wnafee
  • 2,126
  • 16
  • 23
  • you should set click listener on TextView – Mukesh Kumar Singh Jul 25 '13 at 09:50
  • possible duplicate of [how to make text view clickable in android?](http://stackoverflow.com/questions/6226699/how-to-make-text-view-clickable-in-android) – Bhavin Nattar Jul 25 '13 at 09:56
  • I think he's asking how to make each Id clickable separately rather then the whole TextView. Makes a big difference in the question. Maybe the question needs to be clarified to reflect this intention. – wnafee Jul 25 '13 at 10:11

7 Answers7

0

You need to use an onClickListener(), http://developer.android.com/reference/android/view/View.OnClickListener.html

If you would like each ID to trigger a different action they will need to be in their own views with their own associated action. You can't have a different action for different text in a view.

Scott Helme
  • 4,786
  • 2
  • 23
  • 35
  • there are 3 rows in DB. Apple,Pear,Cucumber. I want to show users them. When user clicked on Apple info about apple will show, if click on pear then info about pear will show, if click on cucumber then info about cucumber will show in info.class, i will get the id in info.class and i will know which one choosed by user. – Arif Cavadov Jul 25 '13 at 10:50
0
setNote.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub
            Intent i=new Intent(MainActivity.this,newactivity.class);
           startActivity(i);

       }
});
T_V
  • 17,440
  • 6
  • 36
  • 48
  • by this way will i be able to know on which id user made click(touch)? – Arif Cavadov Jul 25 '13 at 10:02
  • no, but you can set the tag by, setNote.setTag(id.toString()); and get it on onClick by - str myId=v.getTag();, which will return your ID. – T_V Jul 25 '13 at 10:09
  • there are 3 row in DB: Book,Copybook,pencil.i want to show them to user. when user click on book it will redirect to newClass.class and show user info about book, when click on copybook it will redirect do newClass.class and show info about copybook, when user click on pencil it will redirect to pencil and info about pencil will show. i want to to this. – Arif Cavadov Jul 25 '13 at 10:32
0

Use method TextView.setOnClickListener(this) and implement OnClickListener in your activity (class).

TN888
  • 7,659
  • 9
  • 48
  • 84
  • there are 3 rows in DB. Apple,Pear,Cucumber. I want to show users them. When user clicked on Apple info about apple will show, if click on pear then info about pear will show, if click on cucumber then info about cucumber will show in info.class, i will get the id in info.class and i will know which one choosed by user. – Arif Cavadov Jul 25 '13 at 10:50
0

you can do like this:

TextView setNote = (TextView) findViewById(R.id.idNotes);
setNote.setOnClickListener(this);


@Override
public void onClick(View v) {
         new Thread(new Runnable() {
            public void run() {
             Intent monIntent = new Intent(this,CibleClass.class);
             startActivity(monIntent);
            }
          }).start();  

       }
redIntent
  • 134
  • 5
  • by this way will i be able to know on which id user made click(touch)? – Arif Cavadov Jul 25 '13 at 10:02
  • that code detect click on textview , I you want to know which user id made click you have to create programmatically a new textview, this tutorial will help you :http://adilatwork.blogspot.fr/2011/07/android-how-to-make-only-part-of.html – redIntent Jul 25 '13 at 10:13
  • there are 3 row in DB: Book,Copybook,pencil. when user click on book it will redirect to newClass.class and show user info about book, when click on copybook it will redirect do newClass.class and show info about copybook i want to to this. – Arif Cavadov Jul 25 '13 at 10:27
0
setNote.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

Intent i=new Intent(this,new_activity_name_you_want_to_start.class);
startActivity(i);



       }
});

to call a class rather than activity, simply create an object of that class inside

public void click(View v)

and make your code your way

Anchit Mittal
  • 3,412
  • 4
  • 28
  • 48
  • there are 3 rows in DB. Apple,Pear,Cucumber. I want to show users them. When user clicked on Apple info about apple will show, if click on pear then info about pear will show, if click on cucumber then info about cucumber will show in info.class, i will get the id in info.class and i will know which one choosed by user. – Arif Cavadov Jul 25 '13 at 10:51
  • setNote.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i=new Intent(this,info.class); startActivity(i); } }) public class Info extends Activity{ onCreate(){ if(apple){ setContentView(Apple.xml"); }else if(pear){ setContentView(Pear.xml"); } – Anchit Mittal Jul 25 '13 at 11:39
0

If you want to make the whole TextView clickable then you can just add an onClickListener as others have mentioned.

However, if you want to make each of the ID's you're printing clickable separately (ie. each one goes somewhere else), you'll have to create add your own Span class.

Here's an example implementation. First define your own span by extending from ClickableSpan:

static class MyClickableSpan extends ClickableSpan {  
    OnClickListener mListener;  

    public MyClickableSpan(OnClickListener listener) {  
        mListener = listener;  
    }  

    @Override  
    public void onClick(View widget) {  
        mListener.onClick(widget);  
    }  
}  

Next, you want to create a SpannableString for each of the id's you print:

for (Contact cn : contacts) {
   String id = cn.getID().toString();
   SpannableString mySpan = new SpannableString(id+"\n")  
   mySpan.setSpan(new MyClickableSpan(new OnClickListener() {  
       public void onClick(View v) {  
          //Do whatever you want when clicked here! <----  
       }  
     }), 0, id.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
   setNote.append(mySpan);        
}

Finally, to enable clicking, you need to add the LinkMovementMethod to your TextView as follows:

// Put this at the end after finishing your for-loop
setNote.setMovementMethod(LinkMovementMethod.getInstance());

This will allow you to make each of the ID's clickable where each one goes to a separate Activity if that's what you want

wnafee
  • 2,126
  • 16
  • 23
  • Thank you very much, you understoof what i want. public void onClick(View widget) { mListener.onClick(widget); } in here errror occured. The method onClick(DialogInterface, int) in the type DialogInterface.OnClickListener is not applicable for the arguments (View) – Arif Cavadov Jul 25 '13 at 10:43
  • You need to import `View.onClickListener`. Instead, you imported `DialogInterface.OnClickListener` which is wrong. Make sure you import the correct interface – wnafee Jul 25 '13 at 10:48
  • you are perfect its ok now.when user click on an ID new class will open, but how can know on which ID clicked? – Arif Cavadov Jul 25 '13 at 11:05
  • just change the `id` declaration to `final String id = cn.getID().toString();` When you make it `final` you will be able to use it in the `onClick()` method – wnafee Jul 25 '13 at 11:20
  • friend,Sory i disturb you. public void onClick(View v) { Intent redir = new Intent(this, notInfo.class); startActivity(redir); } i couldnt redirect by this way(this message shown: The constructor Intent(new View.OnClickListener(){}, Class) is undefined – Arif Cavadov Jul 26 '13 at 06:09
  • inside the `onClick()` you're inside another class. So `this` refers to the `OnClickListener`. You should do `.this` (just replace the name of your activity here). I advise you to read up on Java to understand how all this works since this is not an Android question – wnafee Jul 27 '13 at 15:51
0

SetOnclickListner. Use to click any View

CRUSADER
  • 5,486
  • 3
  • 28
  • 64
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28