0

大家好,我开发了这个聊天屏幕,它首先从 sqllite 获取数据,然后单击按钮将数据添加到 sql lite 并在屏幕上动态显示...

现在所有作品数据都插入到 sqllite 中,但不会出现在列表中。

带有基本适配器的活动是..

public class Chat_Home extends Activity implements OnClickListener {

MsgDataSource source;
EditText editText;
AnimalAdapter adapter;
ListView listView;
List<Msgs> list;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(this);
    editText = (EditText) findViewById(R.id.editText1);
    listView = (ListView) findViewById(R.id.list);
    source = new MsgDataSource(getApplicationContext());
    list = new ArrayList<Msgs>();

    list = source.getAnimals();//get all msgs  from sql lite and save it to list
    adapter = new AnimalAdapter(list);
    listView.setAdapter(adapter);
}


public class AnimalAdapter extends BaseAdapter {
    final List<Row> rows;
    List<Msgs> list=new ArrayList<Msgs>();

    public void add(Msgs msgs) {
        // TODO Auto-generated method stub
        Log.e("pmmmmm",""+list.size());
        list.add(msgs);
        Log.e("pmmmmm",""+list.size());
        notifyDataSetChanged();

    }
    public AnimalAdapter(List<Msgs> msgs) {
        // TODO Auto-generated constructor stub
        list.addAll(msgs);
        rows = new ArrayList<Row>();

        String type;
        for (Msgs msg : list) {
            Log.e("String", msg.get_sender());
            type = msg.get_sender();
            if (type.equalsIgnoreCase("me")) {
                rows.add(new Send_Row(LayoutInflater.from(Chat_Home.this),
                        msg));
            } else {
                rows.add(new Reciv_row(
                        LayoutInflater.from(Chat_Home.this), msg));
            }

        }
    }

    @Override
    public int getViewTypeCount() {
        return RowType.values().length;
    }

    @Override
    public int getItemViewType(int position) {
        return rows.get(position).getViewType();
    }

    public int getCount() {
        return rows.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        return rows.get(position).getView(convertView);
    }


}

@SuppressLint("NewApi")
@Override
public void onClick(View arg0) {

    String msg;
    msg = editText.getEditableText().toString();
    source.addmsg(msg, "text", 888888, "me", 0);
    adapter.add(new Msgs(msg, "text", 888888, "me", 0));
    editText.setText("");
}

当调用添加方法时,项目被添加到列表中..请帮助..提前谢谢你...

4

2 回答 2

0

在你的 onClick 方法中这样做..

list.add(your new object);

添加后通过调用arraylist刷新listview

adapter.notifyDataSetChanged();
于 2013-11-04T06:11:57.653 回答
0

final List<Row> rows to public List<Row>在 notifyDataSetChanged 之后更改您的并添加此集合的项目

于 2013-11-04T06:12:38.843 回答