在listview中单击按钮时,如何将数据发送到另一个活动和行的位置?

时间:2016-08-15 14:41:39

标签: android listview

我有一个listview,我正在使用行的自定义视图,每行都有一个按钮。我希望当单击此按钮时,某些数据会发送到另一个活动。考虑下面的代码,因为它会给我异常,我尝试了另一种setTag和getTag方法,这将导致滚动时出现类转换异常。下面的代码给我一个例外,即框架布局无法转换为ListView,所以我如何纠正这个?

    public class EngNewsAdapter extends ArrayAdapter {
    private List<EnglishNews> mENList;
    private int mResource;
    private LayoutInflater inflater;

    public EngNewsAdapter(Context context, int resource, List<EnglishNews> objects) {
            super(context, resource, objects);

            mResource = resource;
            mENList = objects;
            inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        }

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

            ViewHolder holder = null;

            if(convertView == null){

                holder = new ViewHolder();
                convertView = inflater.inflate(mResource, null);

                holder.mENDate = (TextView)convertView.findViewById(R.id.pub_date);
                holder.mENTitle = (TextView)convertView.findViewById(R.id.eng_news_title);
                holder.mENSummary = (TextView)convertView.findViewById(R.id.eng_news_summary);
                holder.mENImage = (ImageView)convertView.findViewById(R.id.eng_news_image);
                holder.mENDescBtn = (LinearLayout)convertView.findViewById(R.id.read_more_eng_btn);
                holder.shareButton = (ImageView)convertView.findViewById(R.id.eng_fb_share_button);

                holder.mENTitle.setTypeface(headingStyle);
                holder.mENSummary.setTypeface(contentStyle);

                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }

            //associating the data position to the button through intent to give the desc activity
            holder.mENDescBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    View parentRow = (View) view.getParent();
                    ListView listView = (ListView) parentRow.getParent();
                    int pos = (Integer) listView.getPositionForView(parentRow);

                    Intent intent = new Intent(EnglishNewsActivity.this, EnglishNewsDescriptionActivity.class);
                    intent.putExtra("image_url", mENList.get(pos).getNewsImage());
                    intent.putExtra("title", mENList.get(pos).getNewsTitle());
                    intent.putExtra("date", mENList.get(pos).getNewsDate());
                    startActivity(intent);
                }
            });
            holder.mENTitle.setText(mENList.get(position).getNewsTitle());
            });

            return convertView;
        }
    }

我的行布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:layout_marginLeft="6dp"
    android:id="@+id/read_more_eng_btn"
    android:orientation="vertical">

   <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/eng_news_image"
            android:layout_width="match_parent"
            android:layout_height="210dp"
            />

        <ImageView
            android:id="@+id/eng_fb_share_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/eng_news_title"
            android:layout_width="match_parent"
            android:padding="3dp"
            android:layout_height="wrap_content"
            />
    </FrameLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="110dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/pub_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/eng_news_summary"
            android:layout_width="match_parent"
            android:layout_height="69dp"/>

    </LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方式:

[0] = "A"
[1] = "B"
[2] = "C"
[3] = ""
[4] = ""