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>