package com.company.messagesystem;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class AllMessages extends Activity {
private List<Message> _messages = new ArrayList<Message>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateMessageList();
_messages = MessagesDataSource.getAll();
populateListView();
//registerClickCallBack();
Button newMessageBtn = (Button) findViewById(R.id.new_message_btn);
newMessageBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
startActivity(new Intent("com.company.messagesystem.NEWMESSAGE"));
}
});
}
private void populateMessageList() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.HOUR_OF_DAY, -1);
Date date1 = cal.getTime();
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String date= null;
date = format1.format(date1);
cal.setTime(new Date());
Date currDate2 = cal.getTime();
String date2= null;
date2 = format1.format(currDate2);
cal.setTime(new Date());
cal.add(Calendar.HOUR_OF_DAY, -2);
Date currDate3 = cal.getTime();
String date3= null;
date3 = format1.format(currDate3);
cal.setTime(new Date());
cal.add(Calendar.HOUR_OF_DAY, -3);
Date currDate4 = cal.getTime();
String date4= null;
date4 = format1.format(currDate4);
cal.setTime(new Date());
cal.add(Calendar.HOUR_OF_DAY, -2);
Date currDate5 = cal.getTime();
String date5= null;
date5 = format1.format(currDate5);
cal.setTime(new Date());
cal.add(Calendar.HOUR_OF_DAY,- 4);
Date currDate6 = cal.getTime();
String date6= null;
date6 = format1.format(currDate6);
cal.setTime(new Date());
cal.add(Calendar.HOUR_OF_DAY,- 4);
Date currDate7 = cal.getTime();
String date7= null;
date7 = format1.format(currDate7);
MessagesDataSource.add(new Message(1, "Speed to hight", "You must to slow down! ", date,"Georgi Ivanov", R.drawable.ic_launcher, true, true));
MessagesDataSource.add(new Message(1, "Speed to low", "You must to speed up!",date2,"Maria Petrova", R.drawable.ic_launcher,false, true));
MessagesDataSource.add(new Message(1, "Delay", "You are gonna be late", date3,"Georgi Ivanov",R.drawable.ic_launcher, true, true));
MessagesDataSource.add(new Message(1, "Traffic", "There is congestion after 30km", date4, "Maria Petrova",R.drawable.ic_launcher, true, true));
MessagesDataSource.add(new Message(1, "Delay", "You are gonna be late", date5,"Georgi Ivanov",R.drawable.ic_launcher, true, true));
MessagesDataSource.add(new Message(1, "Traffic", "There is congestion after 30km", date6, "Maria Petrova",R.drawable.ic_launcher, true, true));
MessagesDataSource.add(new Message(1, "Speed to hight", "You must to slow down! ", date,"Georgi Ivanov", R.drawable.ic_launcher, true, true));
MessagesDataSource.add(new Message(1, "Speed to low", "You must to speed up!",date7,"Maria Petrova", R.drawable.ic_launcher,false, true));
}
private void populateListView() {
ArrayAdapter<Message> adapter = new MyListAdapter();
ListView list = (ListView)findViewById(R.id.messageslistView);
list.setAdapter(adapter);
}
private void registerClickCallBack() {
ListView list = (ListView)findViewById(R.id.messageslistView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
}
});
private class MyListAdapter extends ArrayAdapter<Message>{
public MyListAdapter(){
super(AllMessages.this,R.layout.item_view,_messages);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
}
//Find the message to work with
Message currentMessage = _messages.get(position);
//Fill the view
ImageView imageView = (ImageView)itemView.findViewById(R.id.item_icon);
imageView.setImageResource(currentMessage.getIconId());
//Content
TextView contentText = (TextView) itemView.findViewById(R.id.item_textContent);
contentText.setText(currentMessage.getContent());
//Subject
TextView subjectText = (TextView) itemView.findViewById(R.id.item_textSubject);
subjectText.setText(currentMessage.getSubject());
//Date
TextView dateText = (TextView) itemView.findViewById(R.id.item_textDate);
dateText.setText(currentMessage.getDate().toString());
return itemView;
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
This is the class Message:
public class Message {
public int Id;
private String subject;
private String content;
private String date;
private String sender;
private int iconId;
private boolean isRead;
private boolean isRecieved;
public Message(int id,String subject,String content,String date,String sender, int iconId, boolean isRead,boolean isRecieved){
super();
this.Id = id;
this.subject = subject;
this.content = content;
this.date = date;
this.sender = sender;
this.iconId = iconId;
this.isRead = isRead;
this.isRead = isRead;
}
public int getId() {
return Id;
}
public void setId(int id) {
this.Id = id;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public int getIconId() {
return iconId;
}
public void setIconId(int iconId) {
this.iconId = iconId;
}
public boolean getIsRead() {
return isRead;
}
public void setIsRead(boolean isRead) {
this.isRead = isRead;
}
public boolean getIsRecieved() {
return isRecieved;
}
public void setIsRecieved(boolean isRecieved) {
this.isRecieved = isRecieved;
}
}
我希望当我从列表视图中单击消息以打开仅包含按摩内容的新活动时。我不想为每条消息进行活动,我想对每条消息使用相同的活动。如果有人可以帮我这个....
答案 0 :(得分:0)
这取决于ListView
项的内容。如果它只是免费短信,那么只需在onItemClickListener
中设置ListView
即可开始新的活动,并在开始活动时传递意图中的文字。在新活动中,获取启动它的意图,从意图中获取消息,在活动中显示消息。您的新活动布局可能只是TextView
,可以捕获整个活动。
这样,它就是一个活动,一旦你回到列表,活动就会被销毁,你又用另一个项目重新启动它。关键是传递ListView
中的数据并将其传递到Activity
。
//这里编辑是一个简单的例子,展示了如何传递意图中的数据:
http://hmkcode.com/android-passing-data-to-another-activities/