从getItem()创建对象并访问字段

时间:2014-07-26 10:23:40

标签: java android

我的代码中有一个列表listview,包含2个按钮和2个TextView。我想通过单击按钮访问每个textView的文本。这是适配器的代码......

public class TracksAdapter extends SimpleAdapter {
    public TracksAdapter(Context context, List<? extends Map<String, ?>> data, int      resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final TracksAdapter proxy = this;
        final View trackView = super.getView(position, convertView, parent);

        Button dl = (Button) trackView.findViewById(R.id.dl);
        dl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Do something with data : proxy.getItem(position)
                TextView tv = (TextView) trackView.findViewById(R.id.track);
                String track = tv.getText().toString();
                Toast.makeText(getApplicationContext() , track , Toast.LENGTH_LONG);
            }
        });

        Button play = (Button) trackView.findViewById(R.id.play);
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Do something with data : proxy.getItem(position)
                Toast.makeText(getApplicationContext() , String.valueOf(proxy.getItem(position)).indexOf("file=") , Toast.LENGTH_LONG);
            }
        });

        return trackView;
    }
}

proxy.getItem(position)的结果类似于{id=4 , name=foo , track=fofoo}

我该怎么做才能访问'foo'????

1 个答案:

答案 0 :(得分:0)

尝试使用他的代码从onClick获取文本:

TextView tv = (TextView) parent.findViewById(R.id.your_textView_position_id_from_xml);
String s = tv.getText().toString();