1

I have a custom listView with and adapter:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    String player = playerList.get(position);
    View vi = convertView;
    String index = String.valueOf(position + 1);

    if (convertView == null) {
        holder = new ViewHolder();
        vi = inflater.inflate(R.layout.new_player_view, null);
        holder.text = (TextView) vi
                .findViewById(R.id.txtV_player_input_position_title);
        holder.edit = (Button) vi.findViewById(R.id.btn_Edit_Title);
        holder.edit.setOnClickListener(this);
        holder.text.setOnTouchListener(null);
        holder.text.setText((index) + ". " + player);
        vi.setOnTouchListener(null);
        vi.setTag(holder);
    } else {
        holder = (ViewHolder) vi.getTag();
        holder.text.setText((index) + ". " + player);
        holder.edit.setOnClickListener(this);
    }
    return vi;
}
@Override
public void onClick(View v) {
    TextView text = (TextView)v.getContext();
    String s = text.getText().toString();
    Intent intent = new Intent();
    intent.setClass(context, PlayerActivity.class);
    intent.putExtra("name", s);
    context.startActivity(intent);      
};

I am trying to get the text from the TextView that is in the custom ListView when I hit the edit button (also in textView). In my onClick - I want to take the text and pass it to an edit screen, so user can edit the text and return to the listview. My question is how do I get the text? I've tried static text and it works that way, but I can't figure out how to get the text from the TextView. Thanks!

user2533762
  • 69
  • 1
  • 2
  • 14

2 Answers2

2

You need to use setOnItemClickListener in your activity class

    lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> arg0, View v, int arg2,
            long arg3) 
    {

       TextView tv = (TextView) v.findViewById(R.id.txtV_player_input_position_title);
       String s = tv.getText().toString();
    }
    });

Try the below link

Android: ListView elements with multiple clickable buttons

In your getview

    holder.edit.setOnClickListener(mClickListener);   

Then

     private OnClickListener mClickListener = new OnClickListener() {
     @Override
     public void onClick(View v) {
     View view = (View) v.getParent();
     TextView tv = (TextView) view.findViewById(R.id.txtV_player_input_position_title);
     String s = tv.getText().toString();
     Intent intent = new Intent();
     intent.setClass(context, PlayerActivity.class);
     intent.putExtra("name", s);
     context.startActivity(intent);  
     }
 };  

Edit:

To avoid initializing TextView everytime on button click you can set a tag to the button and get the tag. This way you avoid initializing textview everytime.

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • I set this my activity class,but it didn't change anything. My onclick is working but I can't get the text from the textView that is in the same row. I'm going to do more research on OnItemClickListener. I haven't come accross this one yet. I'm new to fairly new to Java and this is my second project in Android. Thanks for the response. – user2533762 Jun 30 '13 at 09:05
  • the first method will work on clicking the row of the lsitview. – Raghunandan Jun 30 '13 at 09:09
  • @Raghunandan what`s the matter? – Blackbelt Jun 30 '13 at 09:21
  • @blackbelt initializing textview every time button is clicked. is this efficient. or is there a work around. every time button is clicked in op's case edit button. then you get the parent view initialize textview and then get the text. so i am worried about the efficiency. – Raghunandan Jun 30 '13 at 09:23
  • 1
    since the purpose is to get a String when a button is clicked, you could put it as tag for the button. this way you can avoid the findviewbyid. Is onItemClick not fired? – Blackbelt Jun 30 '13 at 09:26
  • @blackbelt `onItemClick` is fired. the problem was initializing the textview every time on button click. yup now i get the idea. thanks. – Raghunandan Jun 30 '13 at 09:28
  • I do not think is a huge problem, the init of a view at every clikc – Blackbelt Jun 30 '13 at 09:29
  • @blackbelt agreed performance is not a big issue however you suggestion is better. thanks for the suggestion. – Raghunandan Jun 30 '13 at 09:38
0

Try like this inside ur OnItemClick

 TextView text = (TextView) arg1.findViewById(R.id.ID_OF_YOUR_TEXTVIEW); //arg1 is the View
 String val= text.getText().toString();
Michael Shrestha
  • 2,547
  • 19
  • 30