将动态项添加到listview

时间:2015-02-24 21:04:01

标签: android

您好我想将一个项目添加到列表视图中。

这是我的新消息活动,我想将项目传递给我的主要活动。我不太确定如何通过意图传递这些数据,我们将非常感谢任何帮助。

public class NewMessage extends ActionBarActivity {

    EditText new_message;
    Button post_new_message_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_message);

        new_message = (EditText) findViewById(R.id.message_content);
        post_new_message_button = (Button) findViewById(R.id.message_send);


        post_new_message_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                TextView username = (TextView) findViewById(R.id.conversation_username2);

                itemAdapter.add(new MessageItem(1656,"Bill Smith", "image", DateTime.now(), new_message.getText().toString()));

                itemAdapter.notifyDataSetChanged();

                if (v.getId() == R.id.message_send);
                new_message.setText("");

            }
        });

    }

}

这是我想传递数据的主要活动

公共类MainActivity扩展了ActionBarActivity {

TextView threadId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button newMessage = (Button) findViewById(R.id.new_message_button);


    newMessage.setOnClickListener (new View.OnClickListener(){

        @Override
        public void onClick(View v){
            Intent newMessage = new Intent(MainActivity.this, NewMessage.class);
            startActivity(newMessage);
        }

    });

    final ListView listView = (ListView) this.findViewById(R.id.messagingListView);
    final ActivityAdapter itemAdapter = new ActivityAdapter(getApplicationContext(), this.MessageFeedData());

    listView.setAdapter(itemAdapter);

    listView.setTextFilterEnabled(true);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            listView.getAdapter().getItem(position);


        }
    });
}
// Get dummy data for Activity Feed

    public ArrayList<MessageItem> MessageFeedData() {
        ArrayList<MessageItem> items = new ArrayList<MessageItem>();

        items.add(new MessageItem(1, "Bob Doe", "image", DateTime.now(), "Hello how are you?"));
        items.add(new MessageItem(200, "John Smith", "image", DateTime.now(), "Hello what are you doing"));


        return items;
    }


    class ActivityFeedTask extends AsyncTask<Integer, Void, Void> {

        ArrayList<MessageItem> recentTracks;


    }


    public class ActivityAdapter extends ArrayAdapter<MessageItem> {
        private final Context context;
        private final ArrayList<MessageItem> items;
        //private int currentPage = 0;

        public ActivityAdapter(Context context, ArrayList<MessageItem> recentTrackArrayList) {
            super(context, 0, recentTrackArrayList);
            this.context = context;
            this.items = recentTrackArrayList;
        }


        public View getView(int position, View convertView, ViewGroup parent) {
            View rowView;


            {
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                convertView = getLayoutInflater().inflate
                        (R.layout.message_list_item, parent, false);

                //final MessageItem item = items.get(position);

                rowView = convertView;

                TextView comment2 = (TextView) rowView
                        .findViewById(R.id.messaging_username);
                comment2.setText(items.get(position).Username);

                ImageView comment3 = (ImageView) rowView
                        .findViewById(R.id.messaging_photo);
                if (items.get(position).Image == null) {
                    comment3.setImageResource(R.drawable.ic_launcher);
                }

                TextView comment4 = (TextView) rowView
                        .findViewById(R.id.messaging_date);
                comment4.setText(items.get(position).DateTimeStamp.toString());

                TextView comment5 = (TextView) rowView
                        .findViewById(R.id.messaging_string);
                comment5.setText(items.get(position).MessageString);


            }
            return convertView;
        }

    }

}

1 个答案:

答案 0 :(得分:0)

如果您只想在两个活动之间来回传递数据,也许您应该使用:

startActivityForResult(Intent intent, int requestCode)

onActivityResult(int requestCode, int resultCode, Intent data)

因此,当NewMessageActivity完成时,它可以将数据发送回主要活动。