4.TableLayout、回调接口
会话详情页
listview条目布局
TableLayout是一行几列的意思
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- android:shrinkColumns="0" 压缩第0列,当第0列的内容过多时,不至于将其他列的内容,挤出屏幕 -->
<TableLayout
android:id="@+id/tl_receive"
android:layout_width="match_parent"
android:shrinkColumns="0"
android:layout_height="wrap_content" >
<TableRow android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv_msg_receive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/receive_msg_bubble"
android:text="asdfasdfasdfasdfasdfsdfadfadfasdfasfsdfadffad" />
<TextView
android:id="@+id/tv_date_receive"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom"
android:text="2014/10/10" />
</TableRow>
</TableLayout>
<!-- android:shrinkColumns="1" 压缩第0列,当第0列的内容过多时,不至于将其他列的内容,挤出屏幕 -->
<TableLayout
android:id="@+id/tl_send"
android:layout_width="match_parent"
android:shrinkColumns="1"
android:layout_height="wrap_content" >
<TableRow android:layout_height="wrap_content"
android:gravity="right">
<TextView
android:id="@+id/tv_date_send"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom"
android:text="2014/10/10" />
<TextView
android:id="@+id/tv_msg_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/send_msg_bubble"
android:text="sfsdfadadsadsfasdfadfffad" />
</TableRow>
</TableLayout>
<!-- 另一种实现 方式 -->
<!-- <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom"
android:text="2014/10/10" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@drawable/send_msg_bubble"
android:text="asdfdf" />
</LinearLayout>
</LinearLayout> -->
</LinearLayout>
代码:
public class ConversationDetail extends Activity implements OnClickListener{
/**
*联系人的电话号码
*/
private String address;
private Context ctx;
private ListView listView;
private EditText inputMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
address = getIntent().getStringExtra("address");
if(address == null){
throw new RuntimeException("联系人是空,我不知道显示哪个会话记录");
}
setContentView(R.layout.activity_conversation_detail);
init();
listView = (ListView) findViewById(R.id.lv_conversation_detail);
adapter = new MyListAdapter(this, null);
listView.setAdapter(adapter);
//设置listView条目之间的分隔线为null ,即,不要分隔线
listView.setDivider(null);
prepareData();
}
private void prepareData() {
MyQueryHandler
public class MyQueryHandler extends AsyncQueryHandler{
public MyQueryHandler(ContentResolver cr) {
super(cr);
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
System.out.println("onQueryComplete : token:"+token);
System.out.println("onQueryComplete : cookie:"+cookie);
Tools.printCursor(cursor);
if(cookie!=null && cookie instanceof CursorAdapter){
CursorAdapter adapter = (CursorAdapter) cookie;
// 给adapter 设置新的cursor
adapter.changeCursor(cursor);
}
if(cursorChangedListener!=null){
cursorChangedListener.onCursorChanged(token, cookie, cursor);
}
}
public IOnCursorChangedListener getCursorChangedListener() {
return cursorChangedListener;
}
public void setOnCursorChangedListener(IOnCursorChangedListener cursorChangedListener) {
this.cursorChangedListener = cursorChangedListener;
}
private IOnCursorChangedListener cursorChangedListener;
/**
* 声明,cursor改变时的监听接口
* @author Administrator
*
*/
public interface IOnCursorChangedListener{
void onCursorChanged(int token, Object cookie, Cursor cursor);
}
}
//回调接口写法:这样就把adapter有回传回来了MyQueryHandler myQueryHandler = new MyQueryHandler(getContentResolver());
myQueryHandler.setOnCursorChangedListener(new MyQueryHandler.IOnCursorChangedListener() {
@Override
/**
* 当adapter 获得 cursor 的时候,回调此方法
*/
public void onCursorChanged(int token, Object cookie, Cursor cursor) {
// 让listview 显示最后一行
listView.setSelection(adapter.getCount()-1);
}
});
myQueryHandler.startQuery(99, adapter, MyConstants.URI_SMS, projection, " address="+address, null, " date ");
}
/**
* 显示会话详情,所需要的列
*/
private String[] projection={
"_id","body","date","type"
};
/**
* 短信内容所在列的索引值 为 1
*/
private final int INDEX_BODY = 1;
private final int INDEX_DATE = 2;
private final int INDEX_TYPE = 3;
private void init() {
TextView title = (TextView) findViewById(R.id.tv_title_conversation_detail);
String name = Tools.findNameByNumber(ctx, address);
if(name !=null){ // 有此联系人
title.setText(name);
}else{ // 无此联系人
title.setText(address);
}
findViewById(R.id.btn_back).setOnClickListener(this);
findViewById(R.id.btn_ok).setOnClickListener(this);
inputMsg = (EditText) findViewById(R.id.et_input_msg_conversation_detail);
}
@Override
/**
* 响应按钮的点击事件
*/
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_back: // 后退按钮
finish();
break;
case R.id.btn_ok: // 确定按钮
//先判断输入的是否有内容,
//如果有内容的话,就将内容以短信的形式发送出去,
String msg = inputMsg.getText().toString();
if(TextUtils.isEmpty(msg.trim())){
Toast.makeText(ctx, "请输入短信内容", 0).show();
return ;
}
// 发送短信
Tools.sendMessage(ctx,msg,address);
//清空输入框
inputMsg.setText("");
// 隐藏输入法键盘
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// 隐藏输入法的 API
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
break;
}
}
private MyListAdapter adapter;
private class MyListAdapter extends CursorAdapter{
public MyListAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
/**
* 当内容发生改变的时候,回调此方法
*/
protected void onContentChanged() {
// super 里面,做了重新查询的动作
super.onContentChanged();
// 让listView 显示最后一行
listView.setSelection(getCount()-1);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view =View.inflate(ctx, R.layout.list_item_conversation_detail, null);
ViewHolder vh = new ViewHolder();
vh.tlReceive = (TableLayout) view.findViewById(R.id.tl_receive);
vh.msgReceive = (TextView) view.findViewById(R.id.tv_msg_receive);
vh.dateReceive = (TextView) view.findViewById(R.id.tv_date_receive);
vh.tlSend = (TableLayout) view.findViewById(R.id.tl_send);
vh.msgSend = (TextView) view.findViewById(R.id.tv_msg_send);
vh.dateSend = (TextView) view.findViewById(R.id.tv_date_send);
view.setTag(vh);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder vh = (ViewHolder) view.getTag();
// 给listView条目设置内容
int type = cursor.getInt(INDEX_TYPE);// 获得短信类型
String text = cursor.getString(INDEX_BODY);//获得短信内容
long when = cursor.getLong(INDEX_DATE);// 获得日期
String dateStr = DateFormat.getDateFormat(ctx).format(when);
if(type == MyConstants.TYPE_RECEIVE){ // 接收到的短信
vh.tlReceive.setVisibility(View.VISIBLE);
vh.tlSend.setVisibility(View.GONE);
//设置短信内容
vh.msgReceive.setText(text);
//设置日期
vh.dateReceive.setText(dateStr);
}else{
vh.tlReceive.setVisibility(View.GONE);
vh.tlSend.setVisibility(View.VISIBLE);
//设置短信内容
vh.msgSend.setText(text);
vh.dateSend.setText(dateStr);
}
}
}
private class ViewHolder {
public TableLayout tlReceive;
public TextView msgReceive;
public TextView dateReceive;
public TableLayout tlSend;
public TextView msgSend;
public TextView dateSend;
}
}