CustomArrayAdapter.add()不会更改我的TextView

时间:2015-08-01 12:11:57

标签: java android textview android-arrayadapter

爪哇:

                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);
}
}

基本上就是这样。

1 个答案:

答案 0 :(得分:1)

选项A:无需调用notifyDataSet或覆盖add-method。只需设置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();
    }
}