i have a custom adapter to place items inside listview. To customize it i have a listitem.xml file where i set the elements that compose a listitem.
They are as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:scaleType="fitXY" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView1"
android:layout_marginLeft="23dp"
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="" />
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:text=""/>
</RelativeLayout>
I have a classe Settings.java that uses the following layout settings.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayoutSettings"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#666666"
android:orientation="vertical" >
<ListView
android:id="#+id/listViewSettings"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
Class Settings.java is like this:
public class Settings extends Activity {
private SensorAdapter sensorAdapter;
private Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.settings);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.title_bar);
context = getApplicationContext();
sensorAdapter = new SensorAdapter();
ListView sensorListview = (ListView) findViewById(R.id.listViewSettings);
sensorListview.setAdapter(sensorAdapter);
sensorListview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
sensorItem sensor = sensorAdapter.getCodeLearnChapter(arg2);
}
});
}
public class sensorItem {
String sensorName;
String sensorDescription;
Drawable sensorImage;
}
public List<sensorItem> getDataForListView() {
List<sensorItem> sensorsList = new ArrayList<sensorItem>();
sensorItem sensorGps = new sensorItem();
sensorGps.sensorName = "Location";
sensorGps.sensorDescription = "Collect location";
sensorGps.sensorImage = getResources().getDrawable(R.drawable.location);
sensorItem sensorPhoto = new sensorItem();
sensorPhoto.sensorName = "Photos";
sensorPhoto.sensorDescription = "Collect photos";
sensorPhoto.sensorImage = getResources().getDrawable(R.drawable.photos);
sensorItem sensorAmplitude = new sensorItem();
sensorAmplitude.sensorName = "Sound";
sensorAmplitude.sensorDescription = "Collect amplitude";
sensorAmplitude.sensorImage = getResources().getDrawable(
R.drawable.amplitude);
sensorItem sensorOrientation = new sensorItem();
sensorOrientation.sensorName = "Orientation";
sensorOrientation.sensorDescription = "Collect orientation";
sensorOrientation.sensorImage = getResources().getDrawable(
R.drawable.compass);
sensorItem sensorSms = new sensorItem();
sensorSms.sensorName = "Messages";
sensorSms.sensorDescription = "Collect messages";
sensorSms.sensorImage = getResources().getDrawable(R.drawable.sms);
sensorItem sensorBattery = new sensorItem();
sensorBattery.sensorName = "Battery";
sensorBattery.sensorDescription = "Collect battery";
sensorBattery.sensorImage = getResources().getDrawable(
R.drawable.battery);
sensorItem sensorCalendar = new sensorItem();
sensorCalendar.sensorName = "Calendar";
sensorCalendar.sensorDescription = "Collect calendar";
sensorCalendar.sensorImage = getResources().getDrawable(
R.drawable.calendar);
sensorItem sensorAccelerometer = new sensorItem();
sensorAccelerometer.sensorName = "Accelerometer";
sensorAccelerometer.sensorDescription = "Collect accelerometer";
sensorAccelerometer.sensorImage = getResources().getDrawable(
R.drawable.accelerometer);
sensorItem sensorLight = new sensorItem();
sensorLight.sensorName = "Light";
sensorLight.sensorDescription = "Collect luminosity";
sensorLight.sensorImage = getResources().getDrawable(R.drawable.light);
sensorItem sensorContacts = new sensorItem();
sensorContacts.sensorName = "Contacts";
sensorContacts.sensorDescription = "Collect contact";
sensorContacts.sensorImage = getResources().getDrawable(
R.drawable.contacts);
sensorsList.add(sensorGps);
sensorsList.add(sensorPhoto);
sensorsList.add(sensorAmplitude);
sensorsList.add(sensorOrientation);
sensorsList.add(sensorSms);
sensorsList.add(sensorBattery);
sensorsList.add(sensorCalendar);
sensorsList.add(sensorAccelerometer);
sensorsList.add(sensorLight);
sensorsList.add(sensorContacts);
return sensorsList;
}
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class SensorAdapter extends BaseAdapter {
List<sensorItem> sensorList = getDataForListView();
private ToggleButton locationToggle, photosToggle, soundToggle,
orientationToggle, messagesToggle, batteryToggle, calendarToggle,
accelerometerToggle, lightToggle, contactsToggle;
#Override
public int getCount() {
return sensorList.size();
}
#Override
public sensorItem getItem(int arg0) {
return sensorList.get(arg0);
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
if (arg1 == null) {
LayoutInflater inflater = (LayoutInflater) Settings.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
arg1 = inflater.inflate(R.layout.listitem, arg2, false);
}
TextView sensorDescription = (TextView) arg1
.findViewById(R.id.textView2);
ImageView sensorImage = (ImageView) arg1
.findViewById(R.id.imageView1);
locationToggle = (ToggleButton) arg1.findViewById(R.id.toggleButton1);
locationToggle.setChecked(true);
locationToggle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "CARREGOU", Toast.LENGTH_LONG).show();
}
});
sensorItem sensor = sensorList.get(arg0);
sensorDescription.setText(sensor.sensorDescription);
sensorImage.setBackground(sensor.sensorImage);
return arg1;
}
public sensorItem getCodeLearnChapter(int position) {
return sensorList.get(position);
}
}
}
The problem with this is that now every element in the listitem.xml has exactly the same ID. So every toggle button has the same ID. I dont know which togglebutton the user pressed.
How can i know which togglebutton the user pressed? Do i have to change the way i populate the listview?
Thx very much.
EDIT:
This is how it looks right now and the problem is that the ArrayList that i have to check whether a toggleButton in a row was clicked or not it seems to all set every element to true when i click in another row.
Example:
I have
true | true | true
i click in the third button and i get
true | false | true
but if i click in the first button for example i get
false | true | true
when i should get instead
false | false | true.
Here is the code adapter so someone can take a look.
public class SensorAdapter extends BaseAdapter {
List<sensorItem> sensorList = getDataForListView();
private ToggleButton settingsToggle;
private ArrayList<Boolean> checkedSensors;
#Override
public int getCount() {
return sensorList.size();
}
#Override
public sensorItem getItem(int arg0) {
return sensorList.get(arg0);
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
if (arg1 == null) {
LayoutInflater inflater = (LayoutInflater) Settings.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
arg1 = inflater.inflate(R.layout.listitem, arg2, false);
}
// TextView sensorName = (TextView)
// arg1.findViewById(R.id.textView1);
TextView sensorDescription = (TextView) arg1
.findViewById(R.id.textView2);
ImageView sensorImage = (ImageView) arg1
.findViewById(R.id.imageView1);
settingsToggle = (ToggleButton) arg1.findViewById(R.id.toggleButton1);
settingsToggle.setChecked(true);
checkedSensors = new ArrayList<Boolean>();
int i = 0;
while(i < 10){
checkedSensors.add(i,true);
i++;
}
settingsToggle.setTag(new Long(getItemId(arg0)));
settingsToggle.setOnClickListener(mOnToggleListener);
/*settingsToggle.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
if(isChecked){
Toast.makeText(Settings.this, "on "+buttonView.getId(), Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(Settings.this, "off "+buttonView.getId(), Toast.LENGTH_LONG).show();
}
}
});*/
sensorItem sensor = sensorList.get(arg0);
// sensorName.setText(sensor.sensorName);
sensorDescription.setText(sensor.sensorDescription);
sensorImage.setBackground(sensor.sensorImage);
return arg1;
}
public OnClickListener mOnToggleListener = new OnClickListener(){
#Override
public void onClick(View v){
long id = (Long) v.getTag();
if (checkedSensors.get((int)id)){
//enableSensor(id);
checkedSensors.add((int)id, false);
}else{
//disableSensor(id);
checkedSensors.add((int)id, true);
}
for(int i = 0; i<10 ; i++){
System.out.println(i + " " + checkedSensors.get(i));
}
}
};
public sensorItem getSensor(int position) {
return sensorList.get(position);
}
}
}
You can set the index in a tag on the toggle button so the onClick event knows which index got clicked.
Before you set the onClick listener add this line:
locationToggle.setTag(arg0);
And inside your onClick listener:
int position = (Integer)v.getTag();
you can create your listener ouside of getView like this:
public OnClickListener mOnToggleListener = new OnClickListener() {
#Override
public void onClick(View v) {
//get id of your item from view
//v is locationToggleView
long id = (Long)v.getTag();
}
};
and make these change in getView method:
//set id as view tag when create or reuse view
//arg0 is position
locationToggle.setTag(new Long(getItemId(arg0)));
locationToggle.setOnClickListener(mOnToggleListener);
by this way you can get view related item id when locationToggle is clicked.
Related
I have a recycerview. it has item click event in onCreateViewHolder method and an imageview click event on onBindViewHolder method. Surprisingly while recyclerview loads for the first time first and last item click works after second click then after it works on every click if it is not scrolled.Again when i scroll the recyclerview then same happen for the first and last item like first load. I can not figure out what is the problem
Below is my recyclerview layout #+id/recyclerViewForFilteredCourses is the recyclerview
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activities.CoursesActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/toolbar_with_search"
android:id="#+id/toolbar" />
<com.github.ybq.android.spinkit.SpinKitView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
app:SpinKit_Color="#color/textColorGrey"
android:visibility="gone"
android:elevation="200dp"
style="#style/SpinKitView.Circle"/>
<!--This textview and the recycler view is responsible for filtered course section-->
<TextView
android:id="#+id/filterResultTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Showing X Courses"
android:layout_below="#+id/toolbar"
style="#style/headerTitleLabel"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewForFilteredCourses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/filterResultTitle"
android:layout_above="#+id/bottomNavigationView"
android:paddingHorizontal="14dp"
android:clipToPadding="false"
android:layout_marginTop="10dp"
android:overScrollMode="never"/>
<!--This is the floating filter button-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/floatingFilterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_alignParentRight="true"
android:layout_marginRight="25dp"
android:layout_marginBottom="25dp"
android:layout_alignParentBottom="true"
android:backgroundTint="#273647"
android:elevation="6dp"
app:fabSize="normal"
app:borderWidth="0dp"
android:src="#drawable/filter"
android:onClick="handleFilterButton" />
</RelativeLayout>
<androidx.core.widget.NestedScrollView
android:id="#+id/bottomSheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FEFFFF"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
app:behavior_hideable="true"
app:behavior_peekHeight = "0dp">
<include layout="#layout/course_filter_page" />
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
And below is my onCreateVieHolder Item click
public CoursesAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.course_cell2, parent, false);
final ViewHolder holder = new ViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Course currentCourse = mCourses.get(holder.getAdapterPosition());
switchToCourseDetailsActivity(currentCourse);
}
});
return holder;
}
private void switchToCourseDetailsActivity(Course currentCourse) {
Intent intent = new Intent(mContext, CourseDetailsActivity .class);
intent.putExtra("Course", currentCourse);
mContext.startActivity(intent);
}
And Below is my complete onBindViewHolder
public class CoursesAdapter extends RecyclerView.Adapter<CoursesAdapter.ViewHolder> {
private static final String TAG = "Courses List Adapter";
private static final String TAG2 = "Checker";
//vars
private Context mContext;
private ArrayList<Course> mCourses = new ArrayList<>();
Matcher matcher;
public CoursesAdapter(Context context, ArrayList<Course> courses) {
mCourses = courses;
mContext = context;
}
#NonNull
#Override
public CoursesAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.course_cell2, parent, false);
final ViewHolder holder = new ViewHolder(view);
return holder;
}
private void switchToCourseDetailsActivity(Course currentCourse) {
Intent intent = new Intent(mContext, CourseDetailsActivity .class);
intent.putExtra("Course", currentCourse);
mContext.startActivity(intent);
}
#Override
public void onBindViewHolder(#NonNull final CoursesAdapter.ViewHolder holder, final int position) {
final Course currentCourse = mCourses.get(position);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switchToCourseDetailsActivity(currentCourse);
}
});
holder.name.setText(currentCourse.getTitle());
// holder.coursePrice.setText(currentCourse.getPrice());
holder.totalNumberOfRating.setText("( "+currentCourse.getTotalNumberRating()+" )");
//holder.instructorName.setText("by "+currentCourse.getInstructor()+" "+currentCourse.getBiography());
holder.starRating.setRating(currentCourse.getRating());
holder.rating.setText((" "+currentCourse.getRating()+" "));
if(currentCourse.getIs_bestseller().equals("yes")){
holder.tvBestseller.setVisibility(View.VISIBLE);
}else{
holder.tvBestseller.setVisibility(View.GONE);
}
if(currentCourse.getCourseOverviewUrl()!=null && currentCourse.getCourseOverviewProvider()!=null && currentCourse.getCourseOverviewProvider().equals("youtube")){
//Extract video id from url
String pattern = "(?<=watch\\?v=|/videos/|embed\\/|youtu.be\\/|\\/v\\/|\\/e\\/|watch\\?v%3D|watch\\?feature=player_embedded&v=|%2Fvideos%2F|embed%\u200C\u200B2F|youtu.be%2F|%2Fv%2F)[^#\\&\\?\\n]*";
Pattern compiledPattern = Pattern.compile(pattern);
matcher = compiledPattern.matcher(currentCourse.getCourseOverviewUrl()); //url is youtube url for which you want to extract the id.
if (matcher.find()) {
holder.play_video.setVisibility(View.VISIBLE);
holder.play_video1.setVisibility(View.VISIBLE);
} else{
holder.play_video.setVisibility(View.GONE);
holder.play_video1.setVisibility(View.GONE);
}
} else{
holder.play_video.setVisibility(View.GONE);
holder.play_video1.setVisibility(View.GONE);
}
holder.play_video.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final BottomSheetDialog dialog3 = new BottomSheetDialog(mContext);
dialog3.setContentView(R.layout.bottom_videoplay);
if(!dialog3.isShowing()) {
dialog3.show();
}
dialog3.setCancelable(true);
final WebView web_view = (WebView) dialog3.findViewById(R.id.youtube_web_view);
final ProgressBar progressBar2 = (ProgressBar) dialog3.findViewById(R.id.progressBar2);
final TextView tvTitle2 = (TextView) dialog3.findViewById(R.id.tvTitle2);
final TextView tvEnroll = (TextView) dialog3.findViewById(R.id.tvEnroll);
tvTitle2.setText(currentCourse.getTitle());
tvEnroll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switchToCourseDetailsActivity(currentCourse);
}
});
//String myVideoYoutubeId = "Wwy9aibAd54";
String myVideoYoutubeId;
WebSettings webSettings = web_view.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
myVideoYoutubeId=matcher.group();
web_view.loadUrl("https://www.youtube.com/embed/" + myVideoYoutubeId);
web_view.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
web_view.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar2.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar2.setVisibility(View.GONE);
//to enable autoplay ref:https://stackoverflow.com/questions/28039209/android-webview-youtube-embed-video-autoplay-not-working/45655979#45655979
long delta = 100;
long downTime = SystemClock.uptimeMillis();
float x = view.getLeft() + (view.getWidth()/2);
float y = view.getTop() + (view.getHeight()/2);
MotionEvent tapDownEvent = MotionEvent.obtain(downTime, downTime + delta, MotionEvent.ACTION_DOWN, x, y, 0);
tapDownEvent.setSource(InputDevice.SOURCE_CLASS_POINTER);
MotionEvent tapUpEvent = MotionEvent.obtain(downTime, downTime + delta + 2, MotionEvent.ACTION_UP, x, y, 0);
tapUpEvent.setSource(InputDevice.SOURCE_CLASS_POINTER);
view.dispatchTouchEvent(tapDownEvent);
view.dispatchTouchEvent(tapUpEvent);
}
});
}
});
holder.play_video1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.play_video.performClick();
}
});
if(currentCourse.getIs_mentor()!=null && currentCourse.getIs_mentor().equals("1")){
holder.instructorName.setVisibility(View.VISIBLE);
holder.tvMentor.setVisibility(View.GONE);
if(currentCourse.getBiography()!=null) {
holder.instructorName.setText("by " + currentCourse.getInstructor() + " " + currentCourse.getBiography());
}else{
holder.instructorName.setText("by " + currentCourse.getInstructor());
}
} else{
//holder.tvMentor.setVisibility(View.GONE);
holder.instructorName.setVisibility(View.GONE);
holder.tvMentor.setVisibility(View.VISIBLE);
if(currentCourse.getBiography()!=null) {
holder.tvMentor.setText("by " + currentCourse.getInstructor() + " " + currentCourse.getBiography());
} else{
holder.tvMentor.setText("by " + currentCourse.getInstructor());
}
}
if(currentCourse.getMain_price()!=null && !currentCourse.getPrice().equals("Free")){
String c_price = currentCourse.getPrice().substring(0, currentCourse.getPrice().length() - 1);
Double main_price=Double.parseDouble(currentCourse.getMain_price());
Double current_price=Double.parseDouble(c_price);
if(current_price<main_price){
holder.tvcompare.setVisibility(View.VISIBLE);
holder.tvDiscountprice.setVisibility(View.VISIBLE);
holder.tvDiscountprice.setText(Double.toString(main_price)+"Tk");
Double diff=main_price-current_price;
Double percent=diff*100/main_price;
holder.tvcompare.setText(percent+"% OFF");
}else{
holder.tvDiscountprice.setVisibility(View.GONE);
holder.tvcompare.setVisibility(View.GONE);
}
} else{
holder.tvcompare.setVisibility(View.GONE);
holder.tvDiscountprice.setVisibility(View.GONE);
}
if(!currentCourse.getPrice().equals("Free")) {
String c_price = currentCourse.getPrice().substring(0, currentCourse.getPrice().length() - 1);
holder.coursePrice.setText(c_price + "Tk");
} else{
holder.coursePrice.setText(currentCourse.getPrice());
}
if(currentCourse.getCourse_type()!=null){
if(currentCourse.getCourse_type().equals("Course")){
holder.tvCategory.setText("Lifetime Access");
holder.image.setVisibility(View.VISIBLE);
holder.play_video.setVisibility(View.VISIBLE);
holder.monthly_couching.setVisibility(View.GONE);
Glide.with(mContext)
.asBitmap()
.load(currentCourse.getThumbnail())
.into(holder.image);
}
if (currentCourse.getCourse_type().equals("MonthlyCouching")){
holder.tvCategory.setText("Monthly Couching");
holder.coursePrice.setText(holder.coursePrice.getText()+"/month");
holder.image.setVisibility(View.GONE);
holder.play_video.setVisibility(View.GONE);
holder.monthly_couching.setVisibility(View.VISIBLE);
Picasso.get().load(currentCourse.getThumbnail()).into(holder.image);
Picasso.get().load(currentCourse.getThumbnail()).into(holder.image1);
if(currentCourse.getOnline_classschedule()!=null){
holder.layout_classschedule.setVisibility(View.VISIBLE);
holder.tvClass_schedule.setText(currentCourse.getOnline_classschedule());
} else{
holder.layout_classschedule.setVisibility(View.GONE);
}
}
if(currentCourse.getCourse_type().equals("FixedTermCouching")){
holder.image.setVisibility(View.GONE);
holder.play_video.setVisibility(View.GONE);
holder.monthly_couching.setVisibility(View.VISIBLE);
if(currentCourse.getDuration()!=null && currentCourse.getStart_date()!=null){
long l = Long.parseLong(currentCourse.getStart_date());
Date date = new Date(l);
holder.tvCategory.setText(currentCourse.getDuration()+" Couching starting on "+date);
}
if(currentCourse.getOnline_classschedule()!=null){
holder.layout_classschedule.setVisibility(View.VISIBLE);
holder.tvClass_schedule.setText(currentCourse.getOnline_classschedule());
} else{
holder.layout_classschedule.setVisibility(View.GONE);
}
}
if(currentCourse.getCourse_type().equals("Workshop")){
holder.image.setVisibility(View.VISIBLE);
holder.play_video.setVisibility(View.VISIBLE);
holder.monthly_couching.setVisibility(View.GONE);
if(currentCourse.getDuration()!=null && currentCourse.getStart_date()!=null){
long l = Long.parseLong(currentCourse.getStart_date());
Date date = new Date(l);
holder.tvCategory.setText(currentCourse.getDuration()+" workshop starting on "+date);
}
}
}
if(currentCourse.getWeekly_class()!=null && currentCourse.getWeekly_class().equals("1")){
holder.tvweekly_class.setVisibility(View.VISIBLE);
if(currentCourse.getWeeklyonline_class()!=null && Integer.parseInt(currentCourse.getWeeklyonline_class())>0){
holder.tvweekly_class.setText(holder.tvweekly_class.getText()+" "+currentCourse.getWeeklyonline_class());
}
} else{holder.tvweekly_class.setVisibility(View.GONE);}
if(currentCourse.getWeekly_exam()!=null && currentCourse.getWeekly_exam().equals("1")){
holder.tvweekly_exam.setVisibility(View.VISIBLE);
if(currentCourse.getWeeklyonline_test()!=null && Integer.parseInt(currentCourse.getWeeklyonline_test())>0){
holder.tvweekly_exam.setText(holder.tvweekly_exam.getText()+" "+currentCourse.getWeeklyonline_test());
}
} else{holder.tvweekly_exam.setVisibility(View.GONE);}
if(currentCourse.getReview_class()!=null && currentCourse.getReview_class().equals("1")){
holder.tvreview_class.setVisibility(View.VISIBLE);
} else{holder.tvreview_class.setVisibility(View.GONE);}
if(currentCourse.getCourse_upload()!=null && currentCourse.getCourse_upload().equals("1")){
holder.tvcourse_upload.setVisibility(View.VISIBLE);
} else{holder.tvcourse_upload.setVisibility(View.GONE);}
if(currentCourse.getRecord_upload()!=null && currentCourse.getRecord_upload().equals("1")){
holder.tvrecord_upload.setVisibility(View.VISIBLE);
} else{holder.tvrecord_upload.setVisibility(View.GONE);}
if(currentCourse.getCourse_type()!=null && currentCourse.getCourse_type().equals("FixedTermCouching") && currentCourse.getLifetime_access()!=null && currentCourse.getLifetime_access().equals("1")){
holder.tvlifetime_access.setVisibility(View.VISIBLE);
} else{holder.tvlifetime_access.setVisibility(View.GONE);}
}
#Override
public int getItemCount() {
return mCourses.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView image,image1,play_video,play_video1;
TextView name;
TextView coursePrice;
TextView instructorName;
TextView rating;
TextView totalNumberOfRating;
RatingBar starRating;
TextView tvweekly_class,tvweekly_exam,tvreview_class,tvcourse_upload,tvrecord_upload,tvlifetime_access,tvCategory,tvDiscountprice,tvMentor,tvcompare,tvBestseller,tvClass_schedule;
RelativeLayout monthly_couching;
LinearLayout layout_classschedule;
public ViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.courseThumbnail);
image1 = itemView.findViewById(R.id.courseThumbnail1);
name = itemView.findViewById(R.id.courseTitle);
coursePrice = itemView.findViewById(R.id.tvcoursePrice);
instructorName = itemView.findViewById(R.id.instructorName);
rating = itemView.findViewById(R.id.numericRating);
totalNumberOfRating = itemView.findViewById(R.id.totalNumberOfRatingByUsers);
starRating = itemView.findViewById(R.id.starRating);
tvweekly_class=itemView.findViewById(R.id.tvweekly_class);
tvweekly_exam=itemView.findViewById(R.id.tvweekly_exam);
tvreview_class=itemView.findViewById(R.id.tvreview_class);
tvcourse_upload=itemView.findViewById(R.id.tvcourse_upload);
tvrecord_upload=itemView.findViewById(R.id.tvrecord_upload);
tvlifetime_access=itemView.findViewById(R.id.tvlifetime_access);
tvCategory=itemView.findViewById(R.id.tvCategory);
tvDiscountprice=itemView.findViewById(R.id.tvDiscountprice);
tvMentor=itemView.findViewById(R.id.tvMentor);
play_video=itemView.findViewById(R.id.play_video);
play_video1=itemView.findViewById(R.id.play_video1);
tvcompare=itemView.findViewById(R.id.tvcompare);
tvBestseller=itemView.findViewById(R.id.tvBestseller);
monthly_couching=itemView.findViewById(R.id.monthly_couching);
tvClass_schedule=itemView.findViewById(R.id.tvClass_schedule);
layout_classschedule=itemView.findViewById(R.id.layout_classschedule);
}
}
}
RecyclerView reuses view holders to improve performance. You can also say it recycles view holders - this is where it gets its name from.
RecyclerView adapter will not create more view holders than it requires. The number of view holders can be roughly equal to:
// consider it pseudocode
viewHoldersCount = recyclerView.height / viewHolder.height + 1
So it will generate the number of view holders that fill the screen + 1 more item that is drawn off-screen (e.g. at the bottom of the recycler view) to smooth out scrolling animation and remove any glitches.
Following that: onCreateViewHolder is used only to create a view holder!
Do not ever bind any data to a view holder from onCreateViewHolder.
If you have 100 of items in your adapter that you want to display but the screen of a device can display only 10 you will have 11 view holders that are reused when you scroll the list.
onBindViewHolder is called every time an item is going to be displayed - this is the place to attach data to your view holders.
public CoursesAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.course_cell2, parent, false);
return new ViewHolder(view);
}
public void onBindViewHolder(#NonNull final CoursesAdapter.ViewHolder holder, final int position) {
final Course currentCourse = mCourses.get(position);
holder.play_video.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// ...
}
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
switchToCourseDetailsActivity(currentCourse);
}
});
}
At last I found a solution. My recyclerview layout has a nestedscrollview
adding android:nestedScrollingEnabled="false" to my recyclerview solved my problem. I don't know why but it solved my problem
I have a listview for a note application set with my adapter. The list view component consists of some textviewsand a checkbox which pops up delete icon when a listviewitem is long clicked.
Am automatically making the checkboxes visible from the adapter when the delete icon becomes visible so that the user could select the items he wants to delete.
The problem is i want a single list item to be automatically checked when long clicked. How do i go about this? My logic does not work.
listcomponent.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_marginTop="6dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:background="#F5F5F5"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/verticallineview"
android:layout_width="4dp"
android:layout_height="match_parent"
android:background="#color/toast_color"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/list_note_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:padding="1dp"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/light_black" />
<TextView
android:id="#+id/list_note_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/colorPrimary"
android:text=""
/>
<TextView
android:id="#+id/list_note_content_preview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:textColor="#color/light_black"
/>
</LinearLayout>
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:layout_marginEnd="5dp"
android:theme="#style/checkBoxStyle"
android:visibility="gone"
android:gravity="center"
android:layout_gravity="center"
android:layout_marginRight="5dp" />
</LinearLayout>
</LinearLayout>
Adapter
class NoteListAdapter extends ArrayAdapter<Note>{
private List<Note> objects;
private List<Note> originalList = new ArrayList<>();
boolean isLongPressed;
// boolean isChecked;
boolean[] isChecked;
private boolean isItemsChecked;
NoteListAdapter(Context context, int resource, List<Note> objects) {
super(context, resource, objects);
this.objects = objects;
this.originalList.addAll(objects);
isLongPressed = false;
// isChecked = false;
isChecked = new boolean[objects.size()];
for(int i=0; i<isChecked.length; i++) isChecked[i] = false;
}
#Override
public int getCount() {
return objects.size();
}
#Nullable
#Override
public Note getItem(int position) {
return objects.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_component, parent, false);
}
Note note = getItem(position);
if (note != null) {
TextView title = convertView.findViewById(R.id.list_note_title);
TextView content = convertView.findViewById(R.id.list_note_content_preview);
TextView date = convertView.findViewById(R.id.list_note_date);
// setting checkbox logic on the adapter
CheckBox checkBox = convertView.findViewById(R.id.checkbox);
// now i wanna toggle checked items from a checkbox on my header
if (isItemsChecked) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
if (isLongPressed) {
checkBox.setVisibility(View.VISIBLE);
} else {
checkBox.setVisibility(View.GONE);
}
// also handle checks for all list view items
checkBox.setChecked(isChecked[position]);
checkBox.setTag(position);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
int checkedPosition = (int)cb.getTag();
isChecked[checkedPosition] = cb.isChecked();
notifyDataSetChanged();
}
});
}
return convertView;
}
void showCheckbox(int clickedPosition) {
isLongPressed = true;
for(int i=0; i<isChecked.length; i++) isChecked[i] = false;
isChecked[clickedPosition] = true;
notifyDataSetChanged(); // Required for update
}
void removeCheckbox() {
isLongPressed = false;
notifyDataSetChanged(); // Required for update
}
void Check() {
isItemsChecked = true;
notifyDataSetChanged(); // Required for update
}
void Uncheck() {
isItemsChecked = false;
notifyDataSetChanged(); // Required for update
}
NoteActivity.java
private void listViewLongClick() {
mListNotes.setLongClickable(true);
mListNotes.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// universal button controls all checked items
Checkbox universalCheckBox = findViewById(R.id.check_all);
universalCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
na.Check();
} else {
na.Uncheck();
}
}
});
mDeleteButton = findViewById(R.id.delete_icon);
na.showCheckbox();
deleteButtonIn();
return true;
}
});
}
The problem here is you have set the visibility of the checkBox from only one holder. Your logic is right but you haven't changed the visibility of checkBoxes from all the holders. You have to create a arrayList for holders and initialize them with null items.
private ArrayList<View> collectionOfViews = new ArrayList<>();
for (int i = 0; i < objects.size(); i++) {
collectionOfViews.add(null);
}
This is my getView() from the adapter. Also to store checkStatus from all the boxes add a element checkStatus in your Note object.
if (note != null) {
final TextView textView = convertView.findViewById(R.id.textView);
final CheckBox checkBox = convertView.findViewById(R.id.checkBox);
checkBox.setChecked(note.getCheckStatus());
textView.setText(note.getTextView());
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (checkBox.getVisibility() != View.GONE) {
note.changeCheckStatus();
checkBox.setChecked(note.getCheckStatus());
}
}
});
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
note.changeCheckStatus();
checkBox.setChecked(note.getCheckStatus());
}
});
//setting holder in your array
collectionOfViews.set(position, convertView);
convertView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
note.changeCheckStatus();
checkBox.setChecked(note.getCheckStatus());
return true;
}
});
checkBoxStatus();
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkBoxStatus();
}
});
}
The visibility of the checkBox is handled from checkBoxStatus()
void checkBoxStatus(){
int count = 0;
for (Note noteCheck : mData) {
if (!noteCheck.getCheckStatus()) {
count++;
}
}
if (count == mData.size()) {
for (View view : collectionOfViews) {
if (view != null) {
CheckBox checkBoxInside = view.findViewById(R.id.checkBox);
checkBoxInside.setVisibility(View.GONE);
}
}
}else{
for (View view : collectionOfViews) {
if (view != null) {
CheckBox checkBoxInside = view.findViewById(R.id.checkBox);
checkBoxInside.setVisibility(View.VISIBLE);
}
}
}
notifyDataSetChanged();
}
Read this article this may help you. I got this idea of storing views in array from here.
I hope this helps.
Try the code below:
Adapter:
class NoteListAdapter extends ArrayAdapter<Note>{
private List<Note> objects;
private List<Note> originalList = new ArrayList<>();
boolean isLongPressed;
// boolean isChecked;
boolean[] isChecked;
NoteListAdapter(Context context, int resource, List<Note> objects) {
super(context, resource, objects);
this.objects = objects;
this.originalList.addAll(objects);
isLongPressed = false;
// isChecked = false;
isChecked = new boolean[objects.size()];
for(int i=0; i<isChecked.length; i++) isChecked[i] = false;
}
#Override
public int getCount() {
return objects.size();
}
#Nullable
#Override
public Note getItem(int position) {
return objects.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_component, parent, false);
}
Note note = getItem(position);
if (note != null) {
TextView title = convertView.findViewById(R.id.list_note_title);
TextView content = convertView.findViewById(R.id.list_note_content_preview);
TextView date = convertView.findViewById(R.id.list_note_date);
// setting checkbox logic on the adapter
CheckBox checkBox = convertView.findViewById(R.id.checkbox);
// now i wanna toggle checked items from a checkbox on my header
// if (isItemsChecked) {
// checkBox.setChecked(true);
// } else {
// checkBox.setChecked(false);
// }
if (isLongPressed) {
checkBox.setVisibility(View.VISIBLE);
} else {
checkBox.setVisibility(View.GONE);
}
// also handle checks for all list view items
checkBox.setChecked(isChecked[position]);
checkBox.setTag(position);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
int checkedPosition = (int)cb.getTag();
isChecked[checkedPosition] = cb.isChecked();
notifyDataSetChanged();
}
});
}
return convertView;
}
void showCheckbox(int clickedPosition) {
isLongPressed = true;
for(int i=0; i<isChecked.length; i++) isChecked[i] = false;
isChecked[clickedPosition] = true;
notifyDataSetChanged(); // Required for update
}
void removeCheckbox() {
isLongPressed = false;
notifyDataSetChanged(); // Required for update
}
void checkboxAllChange(boolean state) {
isLongPressed = true;
for(int i=0; i<isChecked.length; i++) isChecked[i] = state;
notifyDataSetChanged(); // Required for update
}
// void Check() {
// isChecked = true;
// notifyDataSetChanged(); // Required for update
// }
// void Uncheck() {
// isChecked = false;
// notifyDataSetChanged(); // Required for update
// }
}
In Activity:
mListNotes.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
deleteButtonIn();
na.showCheckBox(position);
return true;
}
});
Checkbox universalCheckBox = findViewById(R.id.check_all);
universalCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
na.checkboxAllChange(buttonView.isChecked());
}
});
Java:
dateInserted = getDateFromDatePicker(datePicker);
calendarDateInserted.setTime(dateInserted);
finalDateShown = getStringRepresentationOfDate(calendarDateInserted.get(Calendar.DAY_OF_WEEK)) + " " + (calendarDateInserted.get(Calendar.MONTH) + 1) + "/" + (calendarDateInserted.get(Calendar.DATE)) + "/" + (calendarDateInserted.get(Calendar.YEAR));
Log.d("debug",finalDateShown); // Print the string to the LogCat
;
myArrayAdapter.add(new MyItem(finalDateShown));
MyArrayAdapter class:
private class MyArrayAdapter extends ArrayAdapter<MyItem> // My custom array adapter class
{
private int myResourceId = 0;
private LayoutInflater myLayoutInflater;
private RadioButton mySelectedRadioButton;
private int mSelectedPosition = -1;
private ButtonClickListener myClickListener = null;
public MyArrayAdapter(Context context, int myResourceId, List<MyItem> objects,ButtonClickListener myClickListener)
{
super(context, myResourceId, myItemList);
this.myResourceId = myResourceId;
myLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.myClickListener = myClickListener;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View view = convertView;
final ViewHolder holder;
if (view == null)
{
view = myLayoutInflater.inflate(myResourceId, parent, false);
holder = new ViewHolder();
holder.dateTextView = (TextView) view.findViewById(R.id.dates_id);
holder.addDateButton = (Button) view.findViewById(R.id.add_date_button_id);
holder.addCommentButton = (Button)view.findViewById(R.id.add_comment_button_id);
holder.selectDateRadioButton = (RadioButton) view.findViewById(R.id.select_date_radio_button_id);
holder.addDateButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if(position != mSelectedPosition && mySelectedRadioButton != null)
{
mySelectedRadioButton.setChecked(false);
}
mSelectedPosition = position;
mySelectedRadioButton = holder.selectDateRadioButton;
Log.d("debug", finalDateShown);
add(new MyItem(finalDateShown));
}
});
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
}
if(mSelectedPosition != position)
{
holder.selectDateRadioButton.setChecked(false);
}
else
{
holder.selectDateRadioButton.setChecked(true);
if(mySelectedRadioButton != null && holder.selectDateRadioButton != mySelectedRadioButton)
{
mySelectedRadioButton = holder.selectDateRadioButton;
}
}
return view;
} // End of getView() method
#Override
public void add(MyItem object)
{
super.add(object);
this.setNotifyOnChange(true);
}
private class ViewHolder
{
TextView dateTextView;
Button addDateButton;
Button addCommentButton;
RadioButton selectDateRadioButton;
}
}
Now the TextView is never changed to finalDateShown as it should be.
MyItem class:
class MyItem
{
public String date;
public boolean isRadioButtonChecked;
public MyItem(String date)
{
this.date = date;
this.isRadioButtonChecked = false;
}
}
listViewSingleRow:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/dates_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/date_text"
/>
<Button
android:id="#+id/add_comment_button_id"
android:layout_width="105sp"
android:layout_height="wrap_content"
android:text="#string/add_comment_button_text"
android:layout_toRightOf="#+id/add_date_button_id"
android:layout_toEndOf="#id/add_date_button_id"
/>
<Button
android:id="#+id/add_date_button_id"
android:layout_width="80sp"
android:layout_height="wrap_content"
android:text="#string/add_date_button_text"
android:layout_toRightOf="#id/dates_id"
android:layout_toEndOf="#id/dates_id"
/>
<RadioButton
android:id="#+id/select_date_radio_button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/add_comment_button_id"
android:layout_toEndOf="#id/add_comment_button_id"
/>
</RelativeLayout>
Every time i use myArraAdapter.add(finalDateShown) its adding it with the android:Text="SomeText" i assigned in the XML instead of finalDateShown.
So whats wrong here?
EDIT:
Activity:
public class SexAcivity extends AppCompatActivity
{
ListView listView;
MyArrayAdapter myArrayAdapter;
List<MyItem> myItemList = new ArrayList<SexAcivity.MyItem>();
public interface ButtonClickListener
{
public abstract void onButtonClick(int position);
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sex_acivity);
listView = (ListView) findViewById(R.id.list_view_id);
listView.setLayoutParams(layoutParams);
headerView = ((LayoutInflater)SexAcivity.this.getSystemService(SexAcivity.this.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.list_view_header, null, false);
listView.addHeaderView(headerView);
myArrayAdapter = new MyArrayAdapter(SexAcivity.this,R.layout.list_view_single_row,myItemList,new ButtonClickListener()
{
#Override
public void onButtonClick(int position)
{
}
});
listView.setAdapter(myArrayAdapter);
finalDateShown = getStringRepresentationOfDate(calendarDateInserted.get(Calendar.DAY_OF_WEEK)) + " " + (calendarDateInserted.get(Calendar.MONTH) + 1) + "/" + (calendarDateInserted.get(Calendar.DATE)) + "/" + (calendarDateInserted.get(Calendar.YEAR));
Log.d("debug",finalDateShown); // Print the string to the LogCat
myArrayAdapter.add(new MyItem(finalDateShown));
myArrayAdapter.setNotifyOnChange(true);
}
}
Thats basically it.
Option A: No need to call notifyDataSet or override of the add-method. Just set myAdapter.setNotifyOnChange(true) and it will do all the magic for add, insert, clear and remove. See here. The default value is true. So I wonder why your UI does not update automatically. The list of the adapter should be set in it's constructor and passed to the super-constructor. I see that you do this. And then the adapter should be set to the ListView by calling list.setAdapter.
Option B: First add the item, then call notifyDataSetChanged.
Just a side note: In terms of object oriented programming, you should move the call to notifyDataSetChanged into your implementation of the ArrayAdapter.
private class MyArrayAdapter extends ArrayAdapter<MyItem>
{
#Override
public void add(MyItem object)
{
// add
super.add(object);
// then notify UI
this.notifyDataSetChanged();
}
}
I have a ListView in my MainActivity which has a customAdapter to it. I am implementing a slider on each of the list items. I have also attached a footerView to my listView. Now my problem is that, the setOnClickListener is not fired, and I am not able to print the position's of the listView. It only print's for the footerView alone, which I have attached.
The code for MainActivity is
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private Context mContext;
private ListView mainListView;
private TextView addContact;
private EditText inputName;
private List<String> contactList = new ArrayList<String>();
private MainListAdapter mainAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mContext = this;
init();
}
private void init() {
mainListView = (ListView)findViewById(R.id.lv_main);
initListData();
View footerView = getLayoutInflater().inflate(R.layout.listview_item_main, null);
footerView.setBackgroundColor(getResources().getColor(R.color.gold));
addContact = (TextView)footerView.findViewById(R.id.tv_item_name);
addContact.setText(getString(R.string.plus));
mainListView.addFooterView(footerView);
mainAdapter = new MainListAdapter(mContext, contactList);
mainListView.setAdapter(mainAdapter);
mainListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e(TAG, "on click item " + position);
if (position == contactList.size()) {
addContact.setVisibility(View.GONE);
inputName.setVisibility(View.VISIBLE);
}
}
});
inputName = (EditText)footerView.findViewById(R.id.et_input_name);
inputName.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_GO) {
InputMethodManager imm = (InputMethodManager)mContext
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
}
String strInput = inputName.getText().toString();
contactList.add(strInput);
addContact.setVisibility(View.VISIBLE);
inputName.getText().clear();
inputName.setVisibility(View.GONE);
}
return false;
}
});
}
private void initListData() {
// temp data
contactList.add("Jacob");
contactList.add("Nicolas");
contactList.add("David");
contactList.add("Jacob");
contactList.add("Nicolas");
contactList.add("David");
}
}
Then the code for my CustomAdapter is
public class MainListAdapter extends BaseAdapter {
private static final String TAG = "MainListAdapter";
private Context mContext;
private LayoutInflater layoutInflater;
private MyViewPager itemViewPager;
private View viewMain;
private View viewSlide;
private TextView cancel;
private TextView delete;
private ArrayList<View> views;
private PagerAdapter pagerAdapter;
private List<String> mList;
public MainListAdapter(Context context, List<String> list) {
mContext = context;
layoutInflater = LayoutInflater.from(mContext);
mList = list;
}
#Override
public int getCount() {
return mList != null ? mList.size() : 0;
}
#Override
public Object getItem(int position) {
return mList != null ? mList.get(position) : null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.listview_item, null);
}
viewMain = layoutInflater.inflate(R.layout.listview_item_main, null);
viewSlide = layoutInflater.inflate(R.layout.listview_item_slide, null);
cancel = (TextView)viewSlide.findViewById(R.id.tv_menu_cancel);
delete = (TextView)viewSlide.findViewById(R.id.tv_menu_delete);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.v(TAG, "on click cancel");
MyViewPager currPagerView = (MyViewPager)v.getParent().getParent();
currPagerView.setCurrentItem(0, true);
notifyDataSetChanged();
}
});
delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.v(TAG, "on click cancel");
itemViewPager.setCurrentItem(0, true);
notifyDataSetChanged();
MyViewPager currPagerView = (MyViewPager)v.getParent().getParent();
deleteContact(currPagerView.getSelfIndex());
}
});
views = new ArrayList<View>();
views.add(viewMain);
views.add(viewSlide);
itemViewPager = (MyViewPager)convertView.findViewById(R.id.vp_list_item);
itemViewPager.setSelfIndex(position);
pagerAdapter = new PagerAdapter() {
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((MyViewPager)container).removeView(views.get(position));
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
((MyViewPager)container).addView(views.get(position));
// Log.v("PagerAdapter", "curr item positon is" + position);
return views.get(position);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
#Override
public int getCount() {
return views.size();
}
};
fillItemData(convertView, position, viewMain);
itemViewPager.setAdapter(pagerAdapter);
itemViewPager.setCurrentItem(0);
itemViewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
Log.v(TAG, "onPageSelected");
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
Log.v(TAG, "onPageScrolled, " + "arg0=" + arg0 + ", arg1=" + arg1 + ", arg2="
+ arg2);
}
#Override
public void onPageScrollStateChanged(int arg0) {
Log.v(TAG, "onPageScrollStateChanged");
}
});
// notifyDataSetChanged();
// pagerAdapter.notifyDataSetChanged();
return convertView;
}
private void fillItemData(View convertView, int position, View viewMain) {
int[] colorCollection = {
R.color.green, R.color.royalblue, R.color.violet
};
for (int i = 0; i < colorCollection.length; i++) {
colorCollection[i] = mContext.getResources().getColor(colorCollection[i]);
}
int currColor = colorCollection[position % colorCollection.length];
convertView.setBackgroundColor(currColor);
TextView itemName = (TextView)viewMain.findViewById(R.id.tv_item_name);
itemName.setText(mList.get(position));
// Log.v(TAG, "item name is " + itemName.getText());
}
private void deleteContact(int postion) {
mList.remove(postion);
}
}
Then finally the ViewPagerAdapter code is
public class MyViewPager extends ViewPager {
private static final String TAG = "MyViewPager";
private float xDown;
private float xMove;
private float yDown;
private float yMove;
private boolean isScroll = false;
private int selfIndex;
public MyViewPager(Context context) {
super(context);
}
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setSelfIndex(int index) {
selfIndex = index;
}
public int getSelfIndex() {
return selfIndex;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
Log.v(TAG, "on intercept");
if (event.getAction() == MotionEvent.ACTION_UP) {
}
return super.onInterceptTouchEvent(event);
}
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.v(TAG, "on dispatch");
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
xDown = ev.getRawX();
yDown = ev.getRawY();
Log.v(TAG, "action down: " + xDown + ", " + yDown);
} else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
xMove = ev.getRawX();
yMove = ev.getRawY();
Log.v(TAG, "action move: " + xMove + ", " + yMove);
if (isScroll) {
getParent().requestDisallowInterceptTouchEvent(true);
return super.dispatchTouchEvent(ev);
}
if (Math.abs(yMove - yDown) < 5 && Math.abs(xMove - xDown) > 20) {
isScroll = true;
} else {
return false;
}
} else if (ev.getAction() == MotionEvent.ACTION_UP) {
isScroll = false;
}
return super.dispatchTouchEvent(ev);
}
}
The MainActivity layout is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background"
tools:context="${packageName}.${activityClass}" >
<ListView
android:id="#+id/lv_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent" />
</RelativeLayout>
The layout for listview_item is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="100dp" >
<com.example.yo.view.MyViewPager
android:id="#+id/vp_list_item"
android:layout_width="fill_parent"
android:layout_height="100dp" />
</RelativeLayout>
Added all XML Layouts
listview_item_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:minHeight="100dp" >
<TextView
android:id="#+id/tv_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/main_list_text"
android:textSize="48sp"
android:textStyle="bold" />
<EditText
android:id="#+id/et_input_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/add_contact_hint"
android:textColorHint="#color/main_list_text"
android:textColor="#color/main_list_text"
android:textSize="30sp"
android:textStyle="bold"
android:inputType="textCapCharacters"
android:background="#null"
android:textCursorDrawable="#null"
android:singleLine="true"
android:imeOptions="actionDone"
android:visibility="gone" />
</RelativeLayout>
listview_item_slide
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="100dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/tv_menu_cancel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:focusableInTouchMode="true"
android:background="#color/gold"
android:gravity="center"
android:text="#string/cancel"
android:textColor="#color/main_list_text"
android:textSize="28sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_menu_delete"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#color/green"
android:gravity="center"
android:text="#string/delete"
android:textColor="#color/main_list_text"
android:textSize="28sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_menu_block"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#color/royalblue"
android:gravity="center"
android:text="#string/block"
android:textColor="#color/main_list_text"
android:textSize="28sp"
android:textStyle="bold" />
</LinearLayout>
Please help me to listen to the onItemClickListener in the MainActivity's list. I am not able to figure out where I went wrong. Thanks in advance.
you have to put your "setOnItemClickListener" to your Adapter class insted of MainActivity.
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
ViewHolder holder;
View iView = convertView;
if (convertView == null) {
iView = inflater.inflate(R.layout.listview_item, parent, false);
holder = new ViewHolder();
holder.txt_name = (TextView) iView.findViewById(R.id.name);
Typeface custom_fontG = Typeface.createFromAsset(
context.getAssets(), "KozGoPr6N-Regular.otf");
holder.txt_name.setTypeface(custom_fontG);
holder.txt_desc = (TextView) iView.findViewById(R.id.detail);
holder.ivProfile = (ImageView) iView.findViewById(R.id.flag);
iView.setTag(holder);
} else {
holder = (ViewHolder) iView.getTag();
}
// Get the position
resultp = data.get(position);
// Locate the TextViews in listview_item.xml
// Capture position and set results to the TextViews
holder.txt_name.setText(resultp.get(OurTeam.TAG_NAME));
holder.txt_desc.setText(resultp.get(OurTeam.TAG_DETAIL));
imageLoader.displayImage(TAG_IMAGEURL + resultp.get((OurTeam.TAG_IMG)),
holder.ivProfile, options);
// Capture ListView item click
iView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Get the position
// your code
}
});
return iView;
}
At a time you can handle click event either in Adapter class of in Activity. in your case you are doing both.
And to manage
addContact.setVisibility(View.GONE);
inputName.setVisibility(View.VISIBLE);
Write above on click event of what ever is main view in listview_item layout (Do not forget to pass addContact and inputName views to your CustomAdapter).
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private Context mContext;
private ListView mainListView;
private TextView addContact;
private EditText inputName;
private List<String> contactList = new ArrayList<String>();
private MainListAdapter mainAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mContext = this;
init();
}
private void init() {
mainListView = (ListView)findViewById(R.id.lv_main);
initListData();
View footerView = getLayoutInflater().inflate(R.layout.listview_item_main, null);
footerView.setBackgroundColor(getResources().getColor(R.color.gold));
addContact = (TextView)footerView.findViewById(R.id.tv_item_name);
addContact.setText(getString(R.string.plus));
mainListView.addFooterView(footerView);
mainAdapter = new MainListAdapter(mContext, contactList);
mainListView.setAdapter(mainAdapter);
/* mainListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e(TAG, "on click item " + position);
if (position == contactList.size()) {
addContact.setVisibility(View.GONE);
inputName.setVisibility(View.VISIBLE);
}
}
});*/
inputName = (EditText)footerView.findViewById(R.id.et_input_name);
inputName.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_GO) {
InputMethodManager imm = (InputMethodManager)mContext
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
}
String strInput = inputName.getText().toString();
contactList.add(strInput);
addContact.setVisibility(View.VISIBLE);
inputName.getText().clear();
inputName.setVisibility(View.GONE);
}
return false;
}
});
}
private void initListData() {
// temp data
contactList.add("Jacob");
contactList.add("Nicolas");
contactList.add("David");
contactList.add("Jacob");
contactList.add("Nicolas");
contactList.add("David");
}
}
and Adapter Class was..
public class MainListAdapter extends BaseAdapter {
private static final String TAG = "MainListAdapter";
private Context mContext;
private LayoutInflater layoutInflater;
private MyViewPager itemViewPager;
private View viewMain;
private View viewSlide;
private TextView cancel;
private TextView delete;
private ArrayList<View> views;
private PagerAdapter pagerAdapter;
private List<String> mList;
public MainListAdapter(Context context, List<String> list) {
mContext = context;
layoutInflater = LayoutInflater.from(mContext);
mList = list;
}
#Override
public int getCount() {
return mList != null ? mList.size() : 0;
}
#Override
public Object getItem(int position) {
return mList != null ? mList.get(position) : null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.listview_item, null);
}
viewMain = layoutInflater.inflate(R.layout.listview_item_main, null);
viewSlide = layoutInflater.inflate(R.layout.listview_item_slide, null);
cancel = (TextView)viewSlide.findViewById(R.id.tv_menu_cancel);
delete = (TextView)viewSlide.findViewById(R.id.tv_menu_delete);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.v(TAG, "on click cancel");
MyViewPager currPagerView = (MyViewPager)v.getParent().getParent();
currPagerView.setCurrentItem(0, true);
notifyDataSetChanged();
}
});
delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.v(TAG, "on click cancel");
itemViewPager.setCurrentItem(0, true);
notifyDataSetChanged();
MyViewPager currPagerView = (MyViewPager)v.getParent().getParent();
deleteContact(currPagerView.getSelfIndex());
}
});
views = new ArrayList<View>();
views.add(viewMain);
views.add(viewSlide);
itemViewPager = (MyViewPager)convertView.findViewById(R.id.vp_list_item);
itemViewPager.setSelfIndex(position);
pagerAdapter = new PagerAdapter() {
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((MyViewPager)container).removeView(views.get(position));
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
((MyViewPager)container).addView(views.get(position));
// Log.v("PagerAdapter", "curr item positon is" + position);
return views.get(position);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
#Override
public int getCount() {
return views.size();
}
};
fillItemData(convertView, position, viewMain);
itemViewPager.setAdapter(pagerAdapter);
itemViewPager.setCurrentItem(0);
itemViewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
Log.v(TAG, "onPageSelected");
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
Log.v(TAG, "onPageScrolled, " + "arg0=" + arg0 + ", arg1=" + arg1 + ", arg2="
+ arg2);
}
#Override
public void onPageScrollStateChanged(int arg0) {
Log.v(TAG, "onPageScrollStateChanged");
}
});
// notifyDataSetChanged();
// pagerAdapter.notifyDataSetChanged();
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// your code here for list item click listener...
}
})
return convertView;
}
private void fillItemData(View convertView, int position, View viewMain) {
int[] colorCollection = {
R.color.green, R.color.royalblue, R.color.violet
};
for (int i = 0; i < colorCollection.length; i++) {
colorCollection[i] = mContext.getResources().getColor(colorCollection[i]);
}
int currColor = colorCollection[position % colorCollection.length];
convertView.setBackgroundColor(currColor);
TextView itemName = (TextView)viewMain.findViewById(R.id.tv_item_name);
itemName.setText(mList.get(position));
// Log.v(TAG, "item name is " + itemName.getText());
}
private void deleteContact(int postion) {
mList.remove(postion);
}
}
add this below line in your listview_item parent layout only.
android:descendantFocusability="blocksDescendants"
actually when the list item contains a element which can get focus then list item click listener not work.
Add this to the parent of your Listview and parent of your listview_item and try again:
android:descendantFocusability="blocksDescendants"
Like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:descendantFocusability="blocksDescendants"
android:layout_height="match_parent"
android:background="#color/background"
tools:context="${packageName}.${activityClass}" >
<ListView
android:id="#+id/lv_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent" />
and this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:descendantFocusability="blocksDescendants"
android:minHeight="100dp" >
<com.example.yo.view.MyViewPager
android:id="#+id/vp_list_item"
android:layout_width="fill_parent"
android:layout_height="100dp" />
</RelativeLayout>
If this does not work, call isFocusable() on your EditText and TextView
addContact.isFocusable(false);
inputName.isFocusable(false);
I am guessing you made a common error. Please check/try both changes.
1) add android:descendantFocusability="blocksDescendants" into the ListView UI ("#+id/lv_main").
Note: If this and 2nd suggestion does not work, then the Listview layout may need rework.
2) mainListView.setItemsCanFocus(false);
Note: The Listview may be confused with focusing on an item in the UI.
I think the suggested code by "Nirmal Shethwala" looks good. It has the if/else block for if (convertView == null).
EDIT: I noticed you inflated 3 xml layout files in getView(). You can only have one, and that is listview_item in your code. I know this is not well documented. I have tried this in the past. The reason is getView() method has to return only one view, and your code return convertView; like me.
Sorry I know this will take some modifications in your layout.
I'm trying to write a simple checklist application that contains a LinkedList view that has text and two radio buttons that indicate yes/no. The problem is that when I check one radio button as I scroll down the LinkedList changes the answers in my radio buttons. I'd appreciate any suggestions you have.
My code is as follows
My primary layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:divider="#b5b5b5"
android:dividerHeight="1dp" />
</RelativeLayout>
My row item layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioGroup
android:id="#+id/radio_group1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/mRadio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="radio1" />
<RadioButton
android:id="#+id/mRadio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="radio2" />
</RadioGroup>
</LinearLayout>
My backend java class for the main checklist activity
public class Checklist2 extends Activity {
//Class Values
AlertHandler ah; // object that manages the database of alerts
QuestionHandler qh; // object that manages the database of questions
List <Question>questions ; //List that holds all the Question items in the program
List <Alert> alerts; //List that holds all the Alert items in the program
private ArrayList<RowObject> mSource;
TextView mCountTextView; // from internet example
ListView mListView;
RadioGroupAdapter adapter;
private ListView listView1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checklist2);
Log.w("Test", "setList()");
setList();
RadioGroup rg = (RadioGroup) findViewById(R.id.radio_group1);
}
//sets the ListView with questions
private void setList(){
mSource = new ArrayList<RowObject>();
Log.w("Test", "ArrayList<RowObject> created");
for (int i =0; i<questions.size(); i++){
Question q = questions.get(i);
mSource.add(new RowObject());
mSource.get(i).setID(q.getID());
mSource.get(i).setQuestion(q.getQuestion());
mSource.get(i).setYes(false);
mSource.get(i).setNo(false);
}
Log.w("Test", "array list filled");
mListView = (ListView) findViewById(R.id.list);
adapter = new RadioGroupAdapter(this,
R.layout.listitem, mSource);
Log.w("Test", "adapter created");
mListView.setAdapter(adapter);
Log.w("Test", "adapter set");
}}
My RowObject.java class
package com.randstad.jpmcchecklist;
public class RowObject {
private int ID;
private boolean yes;
private boolean no;
public String question;
public RowObject(){
super();
}
public RowObject(int iD, String q,boolean y) {
super();
ID = iD;
question = q;
this.yes = y;
}
public RowObject( String question) {
super();
this.question = question;
}
public void setYes(boolean y){
yes = y;
}
public boolean getYes(){
return yes;
}
public void setNo(boolean n){
no = n;
}
public boolean getNo(){
return no;
}
public void setQuestion(String q){
question = q;
}
public String getQuestion(){
return question;
}
public void setID(int id){
ID = id;
}
public int getID(){
return ID;
}
}
My Adapter class
public class RadioGroupAdapter extends ArrayAdapter<RowObject> {
Context context;
int layoutResourceId;
ArrayList <RowObject> data = null;
int position;
ViewHolder holder;
public RadioGroupAdapter(Context context, int layoutResourceId,
ArrayList <RowObject> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
holder = null;
this.position = position;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.textView1);
holder.yes = (RadioButton) row.findViewById(R.id.mRadio1);
holder.no = (RadioButton) row.findViewById(R.id.mRadio2);
holder.group = (RadioGroup) row.findViewById(R.id.radio_group1);
row.setTag(holder);
//final RadioButton[] rb = new RadioButton[2];
/*
for(int i=0; i<2; i++){
rb[i] = new RadioButton(context);
//rb[i].setButtonDrawable(R.drawable.single_radio_chice);
rb[i].setId(i);
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
0, LayoutParams.WRAP_CONTENT);
params.weight=1.0f;
params.setMargins(15, 0, 5, 10);
holder.group.addView(rb[i],params); //the RadioButtons are added to the radioGroup instead of the layout
}
rb[0].setText("Yes");
rb[1].setText("No");
row.setTag(holder);*/
} else {
holder = (ViewHolder) row.getTag();
}
holder.group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId){
case R.id.mRadio1:
data.get(position).setYes(true);
data.get(position).setNo(false);
break;
case R.id.mRadio2:
data.get(position).setNo(true);
data.get(position).setYes(false);
}
if(data.get(position).getYes()==true){
holder.yes.setChecked(true);
holder.no.setChecked(false);
}else if(data.get(position).getNo() == true){
holder.yes.setChecked(false);
holder.no.setChecked(true);
}else{
holder.yes.setChecked(false);
holder.yes.setChecked(false);
}
}
});
if(data.get(position).getYes()==true){
holder.yes.setChecked(true);
holder.no.setChecked(false);
}else if(data.get(position).getNo() == true){
holder.yes.setChecked(false);
holder.no.setChecked(true);
}else{
holder.yes.setChecked(false);
holder.yes.setChecked(false);
}
//RowObject option = data[position];
//holder.txtTitle.setText(option.title);
return row;
}
static class ViewHolder {
TextView txtTitle;
RadioGroup group;
RadioButton yes;
RadioButton no;
int position;
}}
Thanks, I really appreciate your help.
I guess the error is in
else{
holder.yes.setChecked(false);
holder.yes.setChecked(false);
}
Which should be
else {
holder.yes.setChecked(false);
holder.no.setChecked(false);
}
And for performance - call data.get(position) once in getView() and store it as a local variable.
Try calling row.setTag(holder); right before return row.
Also, you have holder.yes.setChecked(false) twice in your else case.