main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/white">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip">
<EditText
android:id="@+id/edtKeyWord"
android:hint="关键字"
android:layout_height="wrap_content"
android:layout_width="0px"
android:layout_weight="3"
android:layout_marginRight="5dip">
</EditText>
<Spinner android:id="@+id/status"
android:layout_height="wrap_content"
android:layout_width="0px"
android:layout_weight="2"
android:prompt="@string/search_sort"
android:entries="@array/status"></Spinner>
</LinearLayout>
<Button
android:text="搜索"
android:id="@+id/btnSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></Button>
<ListView android:id="@+id/myList" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:drawSelectorOnTop="false"
android:scrollbars="vertical" android:cacheColorHint="#00000000"/>
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:paddingLeft="10dip"
android:paddingRight="10dip" android:paddingTop="1dip"
android:paddingBottom="1dip">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/u_jqm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/survey"
android:singleLine="true" android:text="cus_jqm"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/u_dwdm"
android:layout_width="0px"
android:layout_weight="5"
android:layout_height="wrap_content"
style="@style/min_survey"
android:singleLine="true" android:text="cus_dwdm" />
<TextView
android:id="@+id/u_dwmc"
android:layout_width="0px"
android:layout_weight="2"
android:layout_height="wrap_content"
style="@style/min_survey"
android:text="cus_dwmc">
</TextView>
</LinearLayout>
</LinearLayout>
Java代码:
package zhuce.activity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import zhuce.sqlite.db.DataBaseHelper;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private Spinner spinner;
private EditText edtKeyWord;
private SQLiteDatabase db;
private List<HashMap<String, Object>> list;
private String cus_id;
private Button btnSearch;
private String search_sort="cus_jqm";
private ListView myList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myList=(ListView)findViewById(R.id.myList);
//短按事件
myList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
HashMap<String, Object> map=list.get(position);
String ID=map.get("cus_id").toString();
String cus_jqm=map.get("cus_jqm").toString();
setTitle("点击第"+cus_jqm+"个项目");
}
});
myList.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
menu.setHeaderTitle("长按菜单-ContextMenu");
menu.add(0, 0, 0, "编辑");
menu.add(0, 1, 0, "删除");
}
});
spinner=(Spinner)findViewById(R.id.status);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long id) {
if (position == 0) {
search_sort = "cus_jqm";
}
if (position == 1) {
search_sort = "cus_dwdm";
}
if(position==2){
search_sort = "cus_dwmc";
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
edtKeyWord=(EditText)findViewById(R.id.edtKeyWord);
btnSearch=(Button)findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ShowUser();
}
});
ShowUser();
}
//长按菜单响应函数
@Override
public boolean onContextItemSelected(MenuItem item) {
// setTitle("点击了长按菜单里面的第"+item.getItemId()+"个项目");
AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
HashMap<String, Object> map=list.get(menuInfo.position);
if(item.getItemId()==0)
{
setTitle("编辑"+map.get("cus_jqm").toString());
}else
{
setTitle("删除"+map.get("cus_id").toString());
}
return super.onContextItemSelected(item);
}
}