如何在这个基础适配器android中添加项目

时间:2013-11-04 06:01:37

标签: android

大家好我放弃了这个聊天屏幕,首先从sqllite获取数据,然后点击按钮添加数据到sql lite并在屏幕上显示dynemically ...

现在所有作品数据都插入到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("");
}
当调用add方法时,

项是添加到列表中的.. 请帮忙.. 提前谢谢你......

2 个答案:

答案 0 :(得分:0)

你的onClick方法中的

就是这样的..

list.add(your new object);

添加arraylist后,通过调用

刷新listview
adapter.notifyDataSetChanged();

答案 1 :(得分:0)

更改您的final List<Row> rows to public List<Row>,然后在notifyDataSetChanged

之后添加此集合中的项目