-1

I have a listview and I am using custom views for rows and in each row there is a button. I want that when this button was clicked some data is send to another activity. Consider the code below because it will give me exceptions and I tried another method of setTag and getTag and that will cause class cast exception while scrolling. The below code give me exception that frame layout can not be cast into ListView so how I correct this ?

    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;
        }
    }

My row Layout:

<?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>
Md Sufi Khan
  • 1,591
  • 1
  • 13
  • 17
Bilal Hussain
  • 149
  • 1
  • 12
  • share your full adapter code – comeback4you Aug 15 '16 at 14:46
  • I have edited the question with my adapter code – Bilal Hussain Aug 15 '16 at 14:50
  • Your row layout please – MRX Aug 15 '16 at 14:51
  • 1
    There are multiple question like the one you asked. Try to search the web first before posting a question. Here you can see how to send data between activities. http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android – Todor Kostov Aug 15 '16 at 14:52
  • holder.mENDescBtn = (LinearLayout)convertView.findViewById(R.id.read_more_eng_btn); what is this . Shoulbn't this be a button ? – MRX Aug 15 '16 at 14:54
  • Why don't you use `position` value of `getView` method instead of picking position from list view by show many process? Just make your `position` parameter as `final` so that you can access it inside your `onClick` method. – Md Sufi Khan Aug 15 '16 at 15:02
  • I have saw all post and my code gives me exceptions I am just tried alot and now i am trying to ask that what is causing my code to give exceptions because I could not find it . So please do not down vote – Bilal Hussain Aug 15 '16 at 15:02
  • Md Sufi Khan I do not know about what you have said and I tried and it worked thanks :) – Bilal Hussain Aug 15 '16 at 15:07
  • I posted my answer below. If it helps you will you accept the answer? :) – Md Sufi Khan Aug 15 '16 at 15:24

3 Answers3

0
ListView listView = (ListView) parentRow.getParent();

I think this is where cast exception occures. I encourage to use of AdapterView.OnItemClickListener

      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(NewsActivity.this, DescriptionActivity.class);
                intent.putExtra("news_summary", mENList.get(i).getNewsSummary());
                intent.putExtra("news_url", mENList.get(i).getNewsUrl());
                startActivity(intent);
            }
        });

set this in your NewsActivity.

UPDATE

I think the better way it to implement BaseAdapter instead of creating a custom ArrayAdapter.

public class EngNewsAdapter extends BaseAdapter {

    private List<EnglishNews> mENList;
    private int mResource;
    private LayoutInflater inflater;

    private Context context;

    public EngNewsAdapter(Context context, int resource, List<EnglishNews> objects) {
        this.context = context;
        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) {
                Intent intent = new Intent(context, EnglishNewsDescriptionActivity.class);
                intent.putExtra("image_url", mENList.get(position).getNewsImage());
                intent.putExtra("title", mENList.get(position).getNewsTitle());
                intent.putExtra("date", mENList.get(position).getNewsDate());
                startActivity(intent);
            }
        });

        return convertView;
    }

    @Override
    public int getCount() {
        return mENList != null && !mENList.isEmpty() ? mENList.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return mENList.get(i);
    }

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

}
MrOnyszko
  • 756
  • 7
  • 12
0

//Add context

private Context context;

public EngNewsAdapter(Context context, int resource, List<EnglishNews> objects) {
        super(context, resource, objects);
        //context use
        this.context=context;

    }

//click

holder.mENDescBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, EnglishNewsDescriptionActivity.class);
                intent.putExtra("image_url", mENList.get(position).getNewsImage());
                intent.putExtra("title", mENList.get(position).getNewsTitle());
                intent.putExtra("date", mENList.get(position).getNewsDate());
                context.startActivity(intent);
            }
        });
comeback4you
  • 686
  • 8
  • 23
  • You need to set `position` as final. Otherwise line `intent.putExtra("image_url", mENList.get(position).getNewsImage());` will give you compilation error. – Md Sufi Khan Aug 15 '16 at 15:12
0

You may try like below:

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

        ....

        //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) {

                Intent intent = new Intent(context, EnglishNewsDescriptionActivity.class);
                intent.putExtra("image_url", mENList.get(position).getNewsImage());
                intent.putExtra("title", mENList.get(position).getNewsTitle());
                intent.putExtra("date", mENList.get(position).getNewsDate());
                startActivity(intent);
            }
        });

        ......
        return convertView;
    }
Md Sufi Khan
  • 1,591
  • 1
  • 13
  • 17
  • you need context for Intent call Intent intent = new Intent(EnglishNewsActivity.this, EnglishNewsDescriptionActivity.class); //line not working in adapter – comeback4you Aug 15 '16 at 15:10