效果图:
代码实现:
1、main.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" >
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
2、MainActivity
里面包括了读取通话记录中一些表的URI、以及打电话、发短信的SCHEMA,要是忘了的话,可以在可以查一下
但建议还是到android中去查
package com.njupt.contextmenu1;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog.Calls;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.text.ClipboardManager;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final int MENU_DIAL_ID = 0;
private static final int MENU_SMS_ID = 1;
private static final int MENU_COPY_ID = 2;
private MyAdapter adapter;
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listview = (ListView) findViewById(R.id.listview);
Cursor c = getContentResolver().query(Calls.CONTENT_URI ,new String[]{Calls._ID,Calls.NUMBER}, null,null, null);
adapter = new MyAdapter(this,c);
listview.setAdapter(adapter);
registerForContextMenu(listview);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(0,MENU_DIAL_ID,0,"复制号码到拨号盘");
menu.add(0,MENU_SMS_ID,0,"发送短信");
menu.add(0,MENU_COPY_ID,0,"复制号码");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) item.getMenuInfo();
int position = acmi.position;
Cursor cursor = (Cursor) adapter.getItem(position);
String number = cursor.getString(cursor.getColumnIndex(Calls.NUMBER));
int id = item.getItemId();
Intent intent = null;
switch (id) {
case MENU_DIAL_ID:
intent = new Intent();
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + number));
startActivity(intent);
break;
case MENU_SMS_ID:
intent = new Intent();
intent.setAction(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:" + number));
startActivity(intent);
break;
case MENU_COPY_ID:
ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(number);
default:
break;
}
return super.onContextItemSelected(item);
}
private class MyAdapter extends CursorAdapter{
public MyAdapter(Context context , Cursor cursor) {
super(context, cursor);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
TextView tv = new TextView(context);
tv.setTextSize(22);
tv.setTextColor(Color.GREEN);
return tv;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
String number = cursor.getString(cursor.getColumnIndex(Calls.NUMBER));
TextView tv = (TextView) view;
tv.setText(number);
}
}
@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;
}
}
3、AndroidManifest.xml
<uses-permission android:name="android.permission.READ_CONTACTS"/>
在表视图的行删除操作中,用deleteRowsAtIndexPaths方法删除一个indexPath数组时,抛出一个错误NSInternalInconsistencyException。将出错前后的代码摘录如下。
[self beginUpdates];
[self deleteRowsAtIndexPaths:@[[self indexPathForCell:cell]]withRowAnimation:UITableViewRowAnimationFade];
[self endUpdates];
[self.dsItems removeObject:_originalShpItem];
使用断点追踪时,发现抛错代码在endUpdates方法上。
错误信息如下:
** Terminating app due to uncaught exception'NSInternalInconsistencyException', reason: 'Unable to resolve row for indexpath: 2 indexes [0, 1]'
先确定一点,这个错是在对于uitableview的机制使用上不熟悉导致的,一定不是uitableview的自生问题,别动不动怀疑ios的bug。
有人这么建议的:
The table view data source needs to be consistent with yourinsert/delete calls. Set a break point in numberOfRowsInSection to assure thatthe number of rows in a section is correct after the insert/delete rows.
表视图的数据源需要和你插入或者删除的操作保持一致。在numberOfRowsInSection上做一个断点,确认插入或者删除记录后结果集总数要和deleteRowsAtIndexPaths操作一致。
通过设置numberOfRowsInSection断点,可以很清楚看到deleteRowsAtIndexPaths的执行流程,它在endUpdates之前,是要调用numberOfRowsInSection的,从而得到删除cell后的记录数。
如果这里没有beginUpdates和endUpdates的封装,那么必须在deleteRowsAtIndexPaths之前就将datasource进行删除。否则就会报这个错误。
至于有人提到,不用deleteRowsAtIndexPaths操作,而是使用reloaddata,全屏
Android3.0后出来的新控件,主要是为了在平板和手机屏幕的兼容
实现效果:
点击Sd卡,出现SD目录下的所有文件和文件夹,点击外置Sd卡,出现外置Sd卡目录下的文件和文件夹.点击U盘,出现U盘目录下所有文件和文件夹。
实现如上图的效果,左边点击SD卡,右边显示gridView的内容,需要定义一个主Activity,main.xml中定义好布局,如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="@+id/root"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
>
</LinearLayout>
<LinearLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
Activity中加载方式:
private FragmentManager manager;
private FragmentTransaction transacion;
manager = getFragmentManager();
transacion = manager.beginTransaction();
rootfragment root = new rootfragment();
contentfragment content = new contentfragment();
transacion.add(R.id.root, root,"root");
transacion.add(R.id.content,content,"content");
transacion.commit();
接着定义左边和右边的fragment,先定义左边布局文件的内容,在rootfragment.java中进行加载,
public class rootfragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_root, null);
}
}
同样的方式加载右边显示的内容,接着需要将两个fragment之间进行数据的传递:
contentfragment details = (contentfragment)getFragmentManager().findFragmentByTag("content");
获取到右边的fragment,通过该对象就可以将数据传递给contentfragment类,操作contentfragment里面的函数。这里需要传递文件路径给contentfragment,再让contentfragment进行刷新显示就可以了