Reputation: 1248
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.
Upvotes: 0
Views: 48
Reputation: 5764
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();
}
}
Upvotes: 1