私はリストビューを持っており、私は行のためにカスタムビューを使用しています、そして、各行にボタンがあります。このボタンがクリックされたときに、あるデータが別のアクティビティに送られたかったのです。私は例外を与えるので、私は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>
あなたの完全なアダプターコード – comeback4you
を共有し、私が編集しました私のアダプタコードで質問 –
あなたの行レイアウトしてください – MRX