0

アイテムが更新または削除されるたびにリストビューを更新したい。多くのオプションを試しましたが、どれも機能していません。これはリストビューのコードです。

public class HelloBubblesActivity extends Activity {
    private com.warting.bubbles.DiscussArrayAdapter adapter;
    private ListView lv;
    private LoremIpsum ipsum;
    private EditText editText1;
    private static Random random;
    Runnable m_handlerTask;
    TextView tv ;
    LinearLayout ll;
    Handler m_handler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discuss);

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

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

        lv.setAdapter(adapter);

        ll = (LinearLayout)findViewById(R.id.wrapper);
        m_handler = new Handler();
        addItems();
        ReduceTimeIteration();

    }

     public void ReduceTimeIteration()
     {
            adapter.clear();
            ((BaseAdapter) lv.getAdapter()).notifyDataSetChanged();
            adapter.notifyDataSetChanged();
            adapter.notifyDataSetInvalidated();
            lv.invalidate();
            lv.requestLayout();
            Log.d("Adapter: ", "Adapter is changed");
     }
    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;
    static View row ;

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

}

リストビューで、を使用してすべてのビューを削除してから、リストビューadapter.clear()を更新したいのですが、adapter.clear()を呼び出した後でも、リストビューは同じままです。助けてください。前もって感謝します。

4

1 に答える 1

3

次の方法でアダプターを作成することをお勧めします。

// declared in activity
private List<OneComment> countries = new ArrayList<OneComment>(); 

// in onCreate()
adapter = new DiscussArrayAdapter(getApplicationContext(), R.layout.listitem_discuss, countries);

countries.clear(); adapter.notifyDataSetChanged();次に、リスト ビューをクリアするために呼び出します。基本的に、「国」(リスト)を使用して、アダプターのサイズと、そのリストで取得するアイテムを通知しているためです。

于 2012-11-05T09:07:01.923 に答える