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类:
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;
}
}
现在,
TextView
永远不会像应有的那样更改为finalDateShown
。MyItem类:
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>
每次使用
myArraAdapter.add(finalDateShown)
时,将其与XML中分配的android:Text="SomeText"
而不是finalDateShown
一起添加。那这怎么了?
编辑:
活动:
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);
}
}
基本上就是这样。
最佳答案
选项A:无需调用notifyDataSet或覆盖添加方法。只需设置myAdapter.setNotifyOnChange(true),它将完成添加,插入,清除和删除的所有操作。 See here。默认值是true。所以我想知道为什么您的UI不会自动更新。适配器的列表应在其构造函数中设置,并传递给超级构造函数。我看到你这样做了。然后应通过调用list.setAdapter将适配器设置为ListView。
选项B:首先添加项目,然后调用notifyDataSetChanged。
附带说明:就面向对象编程而言,应将对notifyDataSetChanged的调用移到ArrayAdapter的实现中。
private class MyArrayAdapter extends ArrayAdapter<MyItem>
{
@Override
public void add(MyItem object)
{
// add
super.add(object);
// then notify UI
this.notifyDataSetChanged();
}
}