如何在listView适配器中的onclicklistener中获取textView文本

时间:2014-09-25 12:32:39

标签: java android eclipse listview

我有一个列表视图,其中包含imageView,TextView和两个按钮

我想要的是当我单击按钮例如列表视图中的第三个项目时,我希望获得文本视图中的文本,当我单击与文本视图位于相同位置的按钮时。如何在onclicklistener中的getView()里面的列表视图适配器中执行此操作?

这是我的SimpleArrayAdapter.java

   public class MySimpleArrayAdapter extends ArrayAdapter<String> {
     private final Context context;
     private final String[] values;

     public MySimpleArrayAdapter(Context context, String[] values) {
       super(context, R.layout.my_listview, values);
       this.context = context;
       this.values = values;
     }

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
       LayoutInflater inflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       View rowView = inflater.inflate(R.layout.my_listview, parent, false);
       Button d=(Button) rowView.findViewById(R.id.d);
       Button a=(Button) rowView.findViewById(R.id.a);
       TextView textView = (TextView) rowView.findViewById(R.id.label);
       ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
       textView.setText(values[position]);

       // Change the icon for Windows and iPhone
       String s = values[position];
       if (s.startsWith("Windows7") || s.startsWith("iPhone")
           || s.startsWith("Solaris")) {
         imageView.setImageResource(R.drawable.no);
       } else {
         imageView.setImageResource(R.drawable.ok);
       }

       d.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View view) {
        // here where i want to get the current textview text


        Log.d(null, "");
    /*Intent intent = new Intent(context, MainActivity2.class);
    context.startActivity(intent);*/

    }
       });

       a.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
    Log.d(null, "aaaaaaaaaaaaaaaaa");

    }
    });


       return rowView;
     }
   } 

4 个答案:

答案 0 :(得分:5)

按钮onClick只需

String txt = textView.getText().toString();

即。将代码重写为

 d.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View view) {
        // here where i want to get the current textview text
    String txt = textView.getText().toString();  // add here


        Log.d(null, "");
    /*Intent intent = new Intent(context, MainActivity2.class);
    context.startActivity(intent);*/

    }
       });

并将textview设为final,即更改

 TextView textView = (TextView) rowView.findViewById(R.id.label);

final TextView textView = (TextView) rowView.findViewById(R.id.label);

答案 1 :(得分:2)

final TextView textView = (TextView) rowView.findViewById(R.id.label);

d.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View view) {

           String myText = textView.getText().toString();

        Log.d(null, "");
    /*Intent intent = new Intent(context, MainActivity2.class);
    context.startActivity(intent);*/

    }
       });

答案 2 :(得分:2)

使用

String txtStr = textView.getText().toString();
<{1}} click

事件中的

答案 3 :(得分:0)

试试这个:

final TextView textView = (TextView) rowView.findViewById(R.id.label);

d.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {    
        textView.getText().toString();      
    }
});