聊天界面

核心代码

public class MyDatabaseHelper extends SQLiteOpenHelper{
	
	public static final String CREATE_BOOK = "create table Book("
			+"id integer primary key autoincrement,"
			+"author text," +
			"price real," +
			"pages integer," +
			"name text)";
	public static final String CREARE_CATEGORY = "create table Category("
			+"id integer primary key autoincrement," +
			"category_name text," +
			"category_code integer)";
	
	private Context mContext;
	
	public MyDatabaseHelper(Context context,String name,CursorFactory factory,int version){
		super(context,name,factory,version);
		mContext = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL(CREATE_BOOK);
		db.execSQL(CREARE_CATEGORY);
		Toast.makeText(mContext, "create succeeded", Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("drop table if exists Book");
		db.execSQL("drop table if exists Category");
		onCreate(db);
	}
			

}
2.msg_item.xml
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical"  
    android:padding="10dp" >  
  
    <LinearLayout  
        android:id="@+id/left_layout"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_gravity="left"  
        android:background="@drawable/left" >  
  
        <TextView  
            android:id="@+id/left_msg"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_gravity="center"  
            android:layout_margin="10dp"  
            android:textColor="#fff" />  
    </LinearLayout>  
  
    <LinearLayout  
        android:id="@+id/right_layout"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_gravity="right"  
        android:background="@drawable/right" >  
  
        <TextView  
            android:id="@+id/right_msg"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_gravity="center"  
            android:layout_margin="10dp" />  
    </LinearLayout>  
  
</LinearLayout>  


MainActivity

public class MainActivity extends Activity {  
    private ListView msgListView;  
    private EditText inputText;  
    private Button send;  
    private MsgAdapter adapter;  
    private List<Msg>msgList=new ArrayList<Msg>();  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        setContentView(R.layout.activity_main);  
        initMsgs();  
        adapter=new MsgAdapter(MainActivity.this,R.layout.msg_item,msgList);  
        inputText=(EditText) findViewById(R.id.input_text);  
        send=(Button) findViewById(R.id.send);  
        msgListView=(ListView) findViewById(R.id.msg_list_view);  
        msgListView.setAdapter(adapter);  
        send.setOnClickListener(new OnClickListener(){  
            public void onClick(View v){  
                String content=inputText.getText().toString();  
                if(!"".equals(content)){  
                    Msg msg=new Msg(content,Msg.TYPE_SEND);  
                    msgList.add(msg);  
                    adapter.notifyDataSetChanged();  
                    msgListView.setSelection(msgList.size());  
                    inputText.setText("");  
                }  
            }  
        });  
    }  
    private void initMsgs(){  
        Msg msg1=new Msg("Hello guy.",Msg.TYPE_RECEIVED);  
        msgList.add(msg1);  
        Msg msg2=new Msg("Hello.Who is that?",Msg.TYPE_SEND);  
        msgList.add(msg2);  
        Msg msg3=new Msg("This is Tom.Nice talking to you.",Msg.TYPE_RECEIVED);  
        msgList.add(msg3);  
    }  
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.main, menu);  
        return true;  
    }  
  
    @Override  
    public boolean onOptionsItemSelected(MenuItem item) {  
        // Handle action bar item clicks here. The action bar will  
        // automatically handle clicks on the Home/Up button, so long  
        // as you specify a parent activity in AndroidManifest.xml.  
        int id = item.getItemId();  
        if (id == R.id.action_settings) {  
            return true;  
        }  
        return super.onOptionsItemSelected(item);  
    }  
}  
Msg

public class Msg {  
    public static final int TYPE_RECEIVED = 0;  
    public static final int TYPE_SEND = 1;  
    private String content;  
    private int type;  
  
    public Msg(String content, int type) {  
        this.content = content;  
        this.type = type;  
  
    }  
  
    public String getContent() {  
        return content;  
    }  
  
    public int getType() {  
        return type;  
    }  
  
}  


MsgAdapter.java
public class MsgAdapter extends ArrayAdapter<Msg>{  
    private int resourceId;  
    public MsgAdapter(Context context,int textViewResourceId,List<Msg>objects){  
        super(context,textViewResourceId,objects);  
        resourceId=textViewResourceId;  
    }  
    public View getView(int position,View convertView,ViewGroup parent){  
        Msg msg=getItem(position);  
        View view;  
        ViewHolder viewHolder;  
        if(convertView==null){  
            view=LayoutInflater.from(getContext()).inflate(resourceId, null);  
            viewHolder=new ViewHolder();  
            viewHolder.leftLayout=(LinearLayout)view.findViewById(R.id.left_layout);  
            viewHolder.rightLayout=(LinearLayout)view.findViewById(R.id.right_layout);  
            viewHolder.leftMsg=(TextView)view.findViewById(R.id.left_msg);  
            viewHolder.rightMsg=(TextView)view.findViewById(R.id.right_msg);  
            view.setTag(viewHolder);  
        }else{  
            view=convertView;  
            viewHolder=(ViewHolder)view.getTag();  
        }  
        if(msg.getType()==Msg.TYPE_RECEIVED){  
            viewHolder.leftLayout.setVisibility(View.VISIBLE);  
            viewHolder.rightLayout.setVisibility(View.GONE);  
            viewHolder.leftMsg.setText(msg.getContent());  
        }else if(msg.getType()==Msg.TYPE_SEND){  
            viewHolder.rightLayout.setVisibility(View.VISIBLE);  
            viewHolder.leftLayout.setVisibility(View.GONE);  
              
            viewHolder.rightMsg.setText(msg.getContent());  
        }  
        return view;  
    }  
    class ViewHolder{  
        LinearLayout leftLayout;  
        LinearLayout rightLayout;  
        TextView leftMsg;  
        TextView rightMsg;  
          
    }  
  
}  


猜你喜欢

转载自blog.csdn.net/asdaosidasu/article/details/53455405