当前位置:首页 > 嵌入式课程设计之android聊天室 - 图文
课设题目:Android聊天室
private ListView itemList; private TextView filePath; private Button sendButton;
private List
protected void onCreate(Bundle savedInstanceState) { }
// 刷新ListView
private void refreshListItems(String path) { // TODO Auto-generated method stub
// TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.files);
findViews();
refreshListItems(path);
filePath.setText(path);
adapterList = buildListForSimpleAdapter(path);
SimpleAdapter listAdapter = new SimpleAdapter(this, adapterList, R.layout.file_item,
}
new String[]{\, \, \},
new int[]{R.id.file_name, R.id.file_path, R.id.file_img});
itemList.setAdapter(listAdapter);
itemList.setOnItemClickListener(this); itemList.setSelection(0);
private List
// TODO Auto-generated method stub File nowFile = new File(path);
adapterList = new ArrayList
//放上根目录
Map
26
课设题目:Android聊天室
root.put(\, \);
root.put(\, R.drawable.file_root); root.put(\, \回根目录\); adapterList.add(root);
//放上父目录
Map
pMap.put(\, R.drawable.file_parent); pMap.put(\, \上一级\); adapterList.add(pMap);
if(!nowFile.isDirectory()){ //若是当前路径对应的是文件,则返回 sendButton.setEnabled(true); //发送按钮可用 return adapterList; }
File[] files = nowFile.listFiles(); //得到path下所有文件
for(File file:files){
Map
map.put(\, file.getName()); map.put(\, file.getPath()); if(file.isDirectory()){
map.put(\, R.drawable.file_directory); }else{
map.put(\, R.drawable.file_doc);
}
adapterList.add(map); }
sendButton.setEnabled(false); //当前路径对应的是文件夹,发送按钮不可return adapterList;
用
}
private void findViews() {
// TODO Auto-generated method stub
itemList = (ListView) findViewById(R.id.file_detail); filePath = (TextView) findViewById(R.id.file_path); sendButton = (Button) findViewById(R.id.file_send);
sendButton.setOnClickListener(this); sendButton.setEnabled(false); //开始时不可点击,只有选中的路径是文件时才可以点击
27
课设题目:Android聊天室
}
@Override
public void onItemClick(AdapterView> parent, View v, int position, long id) {
}
// TODO Auto-generated method stub
Log.i(TAG, \位置[\ + position + \上的item被点击\); if(position == 0){ //回根目录 path = \;
refreshListItems(path); goToParent();
}else if(position == 1){ //回到上一级
}else{
path = (String) adapterList.get(position).get(\); refreshListItems(path); }
private void goToParent() { }
@Override
public void onClick(View v) {
// TODO Auto-generated method stub }
@Override
28
// TODO Auto-generated method stub File file = new File(path);
File pFile = file.getParentFile(); //得到父文件 if(pFile == null){
Toast.makeText(this, \当前路径已经是根目录,不存在上一级\, Toast.LENGTH_SHORT).show(); refreshListItems(path); }else{
path = pFile.getAbsolutePath(); refreshListItems(path); }
Intent intent = new Intent();
intent.putExtra(\, path); setResult(RESULT_OK, intent); finish();
课设题目:Android聊天室
public void processMessage(Message msg) { // TODO Auto-generated method stub }
}
2、NetTcpFileSendThread类 /**
* Tcp发送文件线程,实现文件发送的主要代码 * @author 钟玉文 *
* 2012/7/3 */
public class NetTcpFileSendThread implements Runnable{
private final static String TAG = \; private String[] filePathArray; //保存发送文件路径的数组
public static ServerSocket server;
private Socket socket;
private byte[] readBuffer = new byte[1024];
public NetTcpFileSendThread(String[] filePathArray){ this.filePathArray = filePathArray;
try {
server = new ServerSocket(IpMessageConst.PORT); } catch (IOException e) { // TODO Auto-generated catch block
e.printStackTrace();
Log.e(TAG, \监听tcp端口失败\); }
}
@Override
public void run() { // TODO Auto-generated method stub
for(int i = 0; i < filePathArray.length; i ++){ try {
socket = server.accept();
Log.i(TAG, \与
IP为 29
\ +
共分享92篇相关文档