java - 自定义 ArrayAdapter.add() 不改变我的 TextView

标签 java android textview android-arrayadapter

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 .

那么这里出了什么问题?

编辑:

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

基本上就是这样。

最佳答案

选项A:无需调用notifyDataSet或重写add-方法。只需设置 myAdapter.setNotifyOnChange(true) ,它将完成添加、插入、清除和删除的所有魔力。 See here 。默认值是true。所以我想知道为什么你的用户界面不会自动更新。适配器的列表应在其构造函数中设置并传递给 super 构造函数。我看到你这样做。然后通过调用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();
    }
}

关于java - 自定义 ArrayAdapter.add() 不改变我的 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31761711/

相关文章:

java - 难以从其他 Activity/类(class)控制 webview

android - genymotion 蓝屏死机 windows 10

java - hibernate Eclipse 插件

Java编程访问对象变量

android - 使用密码切换显示或隐藏文本

android - 失败 : Build failed with an exception. React Native

java - 使用网格布局将不同长度的垂直 TextView 与相同的右边缘对齐

java - TextView setContentDescription 不起作用

Android TextView SingleLine 字段隐藏长文本

java - OWASP ZAP 代理卡住