获取自定义的ListView从TextView的-w /按钮文本 [英] Get Text from TextView -w/Button in custom ListView

查看:197
本文介绍了获取自定义的ListView从TextView的-w /按钮文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的ListView有和适配器:

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

我想从TextView的是在自定义的ListView时,我打的编辑按钮(也的TextView)的文本。在我的onClick - 我想借此文并把它传递到编辑屏幕,使用户可以编辑文本,并返回到列表视图。我的问题是如何获得的文字?我试过静态文本和它的工作原理是这样的,但我无法弄清楚如何从TextView的文本。谢谢!

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!

推荐答案

您需要使用 setOnItemClickListener 在活动课

    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

<一个href="http://stackoverflow.com/questions/1709166/android-listview-elements-with-multiple-clickable-buttons">Android: ListView的元素有多个可单击按钮

在你的 getview

    holder.edit.setOnClickListener(mClickListener);   

然后

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

编辑:

要避免初始化的TextView 每次按钮单击可以设置标签的按钮,并获得标签。这样就避免了初始化的TextView每次。

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.

这篇关于获取自定义的ListView从TextView的-w /按钮文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆