如何在自定义列表视图中将特定文本视图文本显示为Toast消息?

时间:2016-05-05 11:39:25

标签: android listview

我有一个自定义列表视图。在每行中,有3个文本视图  这是我的自定义列表视图适配器。 如何在自定义列表视图中将特定文本视图文本显示为Toast消息,以及在另一个活动中发送该文本?

细节是我的getter和setter类

public class CustomListViewAdapter extends ArrayAdapter<Details> {

Context context;


public CustomListViewAdapter(Context context, int resourceId,
                             List<Details> items) {
    super(context, resourceId, items);
    this.context = context;
}



/*private view holder class*/
private class ViewHolder {
    //ImageView textView;
    TextView txtTitle;
    TextView txtTitle2;
    TextView txtDesc;
}

@SuppressLint("InflateParams")
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    //Details rowItem = (Details) getItem(position);
    Details rowItem = getItem(position);
    context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null || convertView.getTag() == null) {

        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.list_item, null); //TODO: parent instead of null?

        holder = new ViewHolder();

        holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
        holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
        holder.txtTitle2 = (TextView) convertView.findViewById(R.id.link);
        convertView.setTag(holder);


    }

    else if (rowItem.getResType().equals(vid)){

        holder = (ViewHolder) convertView.getTag();
    }
    if (holder != null) {
        holder.txtDesc.setText(rowItem.getResType());
        holder.txtTitle.setText(rowItem.getName());

        holder.txtTitle2.setText(rowItem.getUrl());

           }     

       return convertView;
    }

  }

2 个答案:

答案 0 :(得分:1)

您可以在自定义适配器中使用dr("COLUMNAME").ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ") Override TextView方法,并根据该方法在OnClick中显示文字,如下所示

Toast

答案 1 :(得分:0)

参考此代码:

public class profileAdapter extends ArrayAdapter<DTO_Detail> {

    public profileAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        // TODO Auto-generated method stub
        // return super.getView(position, convertView, parent);
        View v = convertView;
        ViewWraperDV mwp;

        if (v == null) {
            LayoutInflater l = getLayoutInflater();
            v = l.inflate(R.layout.layoutrowtitle, null);
            mwp = new ViewWraperDV(v);
            v.setTag(mwp);
        } else {

            mwp = (ViewWraperDV) convertView.getTag();
        }

        TextView txtID = mwp.getLable();
        TextView txtTitle = mwp.getTitle();
        final DTO_Detail dt = this.getItem(position);
        txtTitle .setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // you can get value of dt at here and process your code.
                Intent myinten = new Intent(MainActivity.this,
                        Detail.class);
                Bundle b = new Bundle();
                b.putString("Id", dt.ID);
                myinten.putExtras(b);
                startActivityForResult(myinten, 0);
            }
        }); 

        if (dt != null) {
            txtID.setText(dt.ID + ": ");
            txtTitle.setText(dt.Title);
        }
        return v;
    }
}

class ViewWraperDV {

    View base;
    TextView lable1 = null;
    TextView lable2 = null;

    ViewWraperDV(View base) {

        this.base = base;
    }

    TextView getLable() {
        if (lable1 == null) {
            lable1 = (TextView) base.findViewById(R.id.txtID);
        }
        return lable1;
    }

    TextView getTitle() {
        if (lable2 == null) {
            lable2 = (TextView) base.findViewById(R.id.txtTitle);
        }
        return lable2;
    }

}