如何从列表视图中点击按钮获取文本视图的价值?

时间:2014-09-03 12:52:33

标签: java android listview android-listview

我有一个自定义适配器类的列表视图。每个列表项都包含2个文本视图和一个按钮。我希望在点击列表中的按钮时获取这些文字视图的数据。

下面的

是listview的演示模型

]

请帮帮我们

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    IconView icon;
    TextView title;
    TextView description;
    Button favour;

    if (convertView == null) {
        convertView = ((Activity) getContext()).getLayoutInflater().inflate(textViewResourceId, parent, false);
    }

    icon = (IconView) convertView.findViewById(R.id.station_icon);
    title = (TextView) convertView.findViewById(R.id.station_title);
    description = (TextView) convertView.findViewById(R.id.station_description);
    favour=(Button)convertView.findViewById(R.id.favbutton);

    Station station = getItem(position);

    convertView.setTag(station);

    title.setText(station.title);
    icon.setIcon(station.thumb);
    description.setText(station.description);

    return convertView;
}

}

4 个答案:

答案 0 :(得分:0)

  listView.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
        {
            Value That u Pass In Adapter
            Toast.makeText(getApplicationContext(), ""+position, Toast.LENGTH_LONG).show();
        }
    });


  inside the getview methed also see Toast 

    title.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {

        }
    });
    icon.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {

        }
    });
    favour.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {

        }
    });

答案 1 :(得分:0)

您可以在自定义适配器中的getView方法中使用列表小部件,如下所示 -

public View getView(int pos, View convertView, ViewGroup parent) {
        View gridView = convertView;
        if (gridView == null) {
            gridView = mInflater.inflate(layoutId, null);
        }


        TextView tv1 = (TextView) gridView.findViewById(R.id.tv1);
        TextView tv2 = (TextView) gridView.findViewById(R.id.tv2);
        Button button = (Button)gridView.findViewById(R.id.btn);
}

现在点击按钮,您可以使用文本视图。

答案 2 :(得分:0)

只需在onclick listener方法中设置getView;

    icon = (IconView) convertView.findViewById(R.id.station_icon);
    title = (TextView) convertView.findViewById(R.id.station_title);
    description = (TextView) convertView.findViewById(R.id.station_description);
    favour=(Button)convertView.findViewById(R.id.favbutton);

然后在onclick listener内获取TextView's文字。

favour.setOnClickListener(new OnClickListener() {
    public void onClick(View v)
    {
       //title.getText() 
       // description.getText();
    } 

答案 3 :(得分:0)

//这里我有一个textview并编辑文本并在列表视图中提交按钮

@覆盖

public View getView(int position,View convertView,final ViewGroup parent)                 {

                final View v = super.getView(position, convertView, parent);

                final TextView txt= (TextView) v.findViewById(R.id.lblMenuname);
                final EditText QtyView = (EditText) v.findViewById(R.id.editText);
                Button b=(Button)v.findViewById(R.id.btnInterview);
                b.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        String txt2 = txt.getText().toString();
                        Toast.makeText(ItemList.this,txt2.toString() + "=" + QtyView.getText().toString(),Toast.LENGTH_SHORT).show();
                    }
                });
                return v;
            }