0

我想更新项目中存在的文本视图,以便在固定时间后更新,并希望在屏幕/ui 上显示更改。根据调试,文本视图的值正在更改,但更改在屏幕上不可见/ui.这些是 xml:

<?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="wrap_content" >

    <LinearLayout
        android:id="@+id/wrapper"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/comment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="5dip"
            android:background="@drawable/bubble_yellow"
            android:paddingLeft="10dip"
            android:text="Hello bubbles!"
            android:textColor="@android:color/primary_text_light" />
    </LinearLayout>

</LinearLayout>

这是另一个xml:

<?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:orientation="vertical" >

 <include
        layout="@layout/listitem_discuss" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

这是主要课程:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discuss);
        random = new Random();
        ipsum = new LoremIpsum();

        lv = (ListView) findViewById(R.id.listView1);

        adapter = new DiscussArrayAdapter(getApplicationContext(), R.layout.listitem_discuss);

        lv.setAdapter(adapter);

        tv = (TextView)this.findViewById(R.id.comment);
        ll = (LinearLayout)findViewById(R.id.wrapper);

        addItems();
        ReduceTimeIteration();
    }

    public void ReduceTimeIteration() // checks after 2 minutes for any message to delete
    {

        try
        {
            final Handler m_handler = new Handler();
            m_handlerTask = new Runnable()
            {
                @Override 
                public void run() 
                {
                    String a = tv.getText().toString();
                    if(tv.getText() == "Hello bubbles!")
                    {
                                                   tv.setText("hi");                            
                    }
                    else{
                        tv.setText("hello");  // postInvalidate()
                        adapter.notifyDataSetChanged(); 
                    }
                    m_handler.postDelayed(m_handlerTask, 2000); 
                }
            };
            m_handlerTask.run();
        }
        catch(Exception e)
        {

        }
    }
    private String addItems() {
        String a = "Hello bubbles";
        adapter.add(new OneComment(true,a));
        return a;
    }

}

这是数组适配器类:

public class DiscussArrayAdapter extends ArrayAdapter<OneComment> {

    private TextView countryName;
    private List<OneComment> countries = new ArrayList<OneComment>();
    private LinearLayout wrapper;

    @Override
    public void add(OneComment object) {
        countries.add(object);
        super.add(object);
    }

    public DiscussArrayAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    public int getCount() {
        return this.countries.size();
    }

    public OneComment getItem(int index) {
        return this.countries.get(index);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.listitem_discuss, parent, false);
        }

        wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

        OneComment coment = getItem(position);

        countryName = (TextView) row.findViewById(R.id.comment);

        countryName.setText(coment.comment);

        countryName.setBackgroundResource(coment.left ? R.drawable.bubble_yellow : R.drawable.bubble_green);
        wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT);

        return row;
    }

    public Bitmap decodeToBitmap(byte[] decodedByte) {
        return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
    }

}

这是我将数据传递给适配器的类:

public class OneComment {
    public boolean left;
    public String comment;

    public OneComment(boolean left, String comment) {
        super();
        this.left = left;
        this.comment = comment;
    }
}

在这个项目中,我想更新主类中 textview tv 的值。该值实际上正在改变,但在屏幕/ui上看不到。请帮助我。提前致谢。

4

4 回答 4

3

尝试更改此方法

private String addItems() {
        String a = "Hello bubbles";
        adapter.add(new OneComment(true,a));
        return a;
    }

private String addItems() {
        String a = "Hello bubbles";

        adapter.notifyDataSetChanged();

        adapter.add(new OneComment(true,a));

        adapter.notifyDataSetInvalidated();

        return a;
    }
于 2012-11-01T10:11:48.130 回答
0

您没有提到 LinearLayout 的方向。对于LinearLayout默认方向是Horizontal.,所以指定android:orientation="vertical"

于 2012-11-01T09:48:01.153 回答
0

我不确定您到底期待什么变化,但快速浏览一下,您Runnable对我来说似乎有点可疑。我认为notifyDataSetChanged应该在if/else条件之外调用,以便在任何一种情况下都更新适配器。

而且你不应该比较两个Strings使用==. 必须使用它们进行比较equals()

        @Override 
        public void run() 
        {
            String a = tv.getText().toString();
            if(tv.getText().equals("Hello bubbles!"))
                tv.setText("hi");
            else
                tv.setText("hello");  // postInvalidate()

            adapter.notifyDataSetChanged();
            m_handler.postDelayed(m_handlerTask, 2000); 
        }
于 2012-11-01T09:48:53.637 回答
0

您在 ReduceTimeIteration() 中声明您的处理程序。此方法发布一条消息以运行 runnable 然后退出。然后您的处理程序将被销毁,因为它不再在范围内。在类级别声明处理程序。

于 2012-11-01T10:00:30.807 回答