从Listview中的Textview获取文本膨胀布局

时间:2014-08-21 20:50:10

标签: android listview

我有一个列表视图,其中我使用多个textview和按钮来扩展布局。我理解从点击的视图中获取文本((Textview)view...。但是,我试图从位于用户单击的布局中的特定textview获取文本。我尝试过使用OnItemClick但是当我使用它时,必须在任何按钮功能工作之前聚焦项目。我使用并优先在我的自定义适配器的getView方法中使用onClickListeners。简单地说,如果因为每个膨胀的布局被视为一个列表项,我如何单击一个Button并获取TextView中位于相应的膨胀布局列表视图项中的文本?

更新

这是照片,以澄清我在寻找什么。两种布局都是列表视图的成员。 enter image description here

enter image description here

我想单击带有日期的按钮,并从布局中间的textview中获取文本。但是,当我单击带有日期的按钮时,我只能从最后一个孩子的布局中间的textview中获取文本。如果"我的党"是列表视图中的第一个孩子," 3303离开服务..."是第二个孩子,当我点击日期按钮时,我的自定义适配器中的代码返回视图中最后加载的文本中的文本,这将是" 3303离开服务"。我想要做的是当我点击"我的派对"上的日期按钮,得到文字"我的派对"。和第二个孩子一样聪明。

这是我的自定义适配器中的getView()。

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

        viewHolder = new ViewHolder();

        positionHolder = position;
        Log.i("Position", "" + position);

        if(convertView == null) {

            try {

                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.post_layout, parent, false);
                postLayout = convertView;

                viewHolder.unameTV = (TextView) postLayout.findViewById(R.id.postUnameTv);
                viewHolder.unameTV.setText(viewContent.get(index));
                viewHolder.unameTV.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        // Starting new intent
                        Intent in = new Intent(getActivity(),
                                Profile.class);
                        // sending pid to next activity
                        String username =((TextView)view).getText().toString();
                        in.putExtra("username", username);

                        // starting new activity and expecting some response back
                        startActivityForResult(in, 100);
                    }
                });

                viewHolder.fillSpace = (TextView)postLayout.findViewById(R.id.posthelpSpace);
                viewHolder.fillSpace.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        viewHolder.unameTV.performClick();
                    }
                });




                viewHolder.image = (ImageView) postLayout.findViewById(R.id.postProfPic);
                DisplayImageOptions options = initiateDisplayImageOptions();
                viewHolder.image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        viewHolder.unameTV.performClick();
                    }
                });

                ImageLoader imageloader = ImageLoader.getInstance();

                initImageLoader(getActivity());
                imageloader.displayImage(viewContent.get(index + 1), viewHolder.image, options);




                viewHolder.addToCalendarButton = (TextView) postLayout.findViewById(R.id.addToCalendarButton);
                viewHolder.addToCalendarButton.setText(viewContent.get(index + 2));
                viewHolder.addToCalendarButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Calendar cal = new GregorianCalendar();
                        cal.setTime(new Date());
                        cal.add(Calendar.MONTH, 2);
                        long time = cal.getTime().getTime();

                        Uri.Builder builder =
                                CalendarContract.CONTENT_URI.buildUpon();
                        builder.appendPath("time");
                        builder.appendPath(Long.toString(time));
                        Intent intent =
                                new Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI);

                        title = testText.getText().toString();
                        Log.i("Title", "" + title);
                        intent.putExtra("title", title); // **NOT WORKING**
                        startActivity(intent);
                    }
                });



                viewHolder.eventTitle = (TextView) postLayout.findViewById(R.id.postTitleTV);
                viewHolder.eventTitle.setText(viewContent.get(index + 3));
                viewHolder.eventTitle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        title = ((TextView)view).getText().toString();
                        Log.i("TITLE", "" + title);

                    }
                });

                testText = viewHolder.eventTitle;
                viewHolder.eventImage = (ImageView) postLayout.findViewById(R.id.eventImage);
                imageloader.displayImage(viewContent.get(index + 4), viewHolder.eventImage, options);


                viewHolder.likesTV = (TextView) postLayout.findViewById(R.id.likesTV);
                viewHolder.likesTV.setText("" + viewContent.get(index + 5));

                viewHolder.planToAttendTV = (TextView) postLayout.findViewById(R.id.planToAttendTV);
                viewHolder.planToAttendTV.setText(viewContent.get(index + 6));

                viewHolder.addressTV = (TextView) postLayout.findViewById(R.id.postLocationTV);
                viewHolder.addressTV.setText("" + viewContent.get(index + 7));


                index = index + 8;


            }
            catch (IndexOutOfBoundsException ie)
            {
                ie.printStackTrace();
            }
        }
        else
        {
            viewHolder = (ViewHolder) postLayout.getTag();
        }




        return postLayout;
    }

3 个答案:

答案 0 :(得分:0)

创建自定义baseadapter并在适配器的文本视图上单击侦听器。然后将为与位置对应的特定文本视图设置此项。

答案 1 :(得分:0)

我认为您已经在要将点击注册到的textview的自定义BaseAdapter setOnclicklistener中创建了自定义BaseAdapter。

以下示例代码

public class M_Adapter extends BaseAdapter {
    private LayoutInflater inflater = null;
    private TextView contact_name;
    private Context context;
    private List<String> list;
    private List<String> temp;
    private Button btn;

    public M_Adapter(Context context) {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        try {
            list = new ArrayList<String>();
            //add some values in list here
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, final View convertView, ViewGroup parent) {
        final View rowview = inflater.inflate(R.layout.contact_listrow, parent, false);
        contact_name = (TextView) rowview.findViewById(R.id.excl_ppl_contact_name);
        btn=(Button)rowview.findViewById(R.id.excl_ppl_btn);
        contact_name.setText(list.get(temp));

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Log.v("myapp", "text clicked " + contact_name.getText());
            }
        });
        return rowview;
    }

}

答案 2 :(得分:0)

我用两个if / else语句系列解决了这个问题。首先,在我的onBindViewHolder(你的getView)中我有一些全局的String变量采用我感兴趣的TextView的值。其次,在onClick方法中,我让ViewHolder告诉我使用getPosition点击项目的位置方法。最后,我将点击的位置值与第一部分中创建的字符串变量进行匹配,然后从那里继续。

public void onBindViewHolder(final ViewHolder holder, final int position) {
        YelpAPI.businessNumber = position;
        YelpAPI.queryAPI(YelpAPI.yelpApi, YelpAPI.yelpApiCli);

    Picasso.with(mContext).load(YelpAPI.picture).into(holder.yelpPicture);
    holder.textName.setText(YelpAPI.name);
    holder.textRating.setText(YelpAPI.rating);
    holder.textReviews.setText(YelpAPI.reviews);
    holder.textAddressDetails.setText(YelpAPI.fullAddress);
    holder.textInfo.setText(YelpAPI.moreInfo);
    holder.textID.setText(YelpAPI.businessID);

----------首先......

    if(position == 0){
        firstBusiness = YelpAPI.businessID; //these three String variables get created above
    } else if(position == 1){
        secondBusiness = YelpAPI.businessID;
    } else if (position == 2) {
        thirdBusiness = YelpAPI.businessID;
    }

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

----------其次...

            int specific = holder.getPosition();

----------最后...

            String saveMe = "";
            if(specific == 0){
                saveMe = firstBusiness;
            } else if(specific == 1){
                saveMe = secondBusiness;
            } else if(specific == 2){
                saveMe = thirdBusiness;
            }
            Log.d("Clicked this item: ", String.valueOf(specific));`