Get Text from TextView -w/Button in custom ListVie

2019-08-05 07:28发布

问题:

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!

回答1:

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.



回答2:

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();