Listview
package com.example.hp.list_1;
import android.arch.lifecycle.ViewModelStoreOwner;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity
{
EditText cl,nm,mz;
Button button;
ListView listView;
List<String> list=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cl=(EditText)findViewById(R.id.bj);
nm=(EditText)findViewById(R.id.xh);
mz=(EditText)findViewById(R.id.xm);
listView=(ListView)findViewById(R.id.list);
button=(Button)findViewById(R.id.send);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
list.add(" "+cl.getText().toString()+" "+nm.getText().toString()+" "+mz.getText().toString());
//ArrayAdapter<String>adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,list);
ArrayAdapter<String>adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
}
});
listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener(){
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.add(0,0,0,"删除");
}
});
}
public boolean onContextItemSelected(MenuItem item){
AdapterView.AdapterContextMenuInfo info=(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
int id=(int)info.id;
list.remove(id);
ArrayAdapter<String>adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
return true;
}
}数据库
DBAdapter
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DBAdapter
{
public static final String dbname="student.db";
public static final String Sno="Sno";
public static final String Sname="Snmae";
public static final String Sclass="Sclass";
public static final String Id="_id";
public static final String Table="studentinfo";
public static SQLiteDatabase db;
public final Context context;
public DBhelper dBhelper;
public DBAdapter(Context context1)
{
context=context1;
}
public void open() throws SQLiteException
{
dBhelper=new DBhelper(context,dbname,null,1);
try
{
db=dBhelper.getWritableDatabase();
}
catch (Exception e)
{
db=dBhelper.getReadableDatabase();
}
}
public long insert(Student s)
{
ContentValues cv=new ContentValues();
cv.put(Sno,s.sno);
cv.put(Sname,s.sname);
cv.put(Sclass,s.Class);
return db.insert(Table,null,cv);
}
public Student[] find()
{
Cursor c=db.query(Table,new String []{Sclass,Sno,Sname},null,null,null,null,null);
if(c==null)
return null;
return convert(c);
}
public long delete()
{
return db.delete(Table,null,null);
}
public long deleteOne(String sno)
{
return db.delete(Table,"Sno="+sno,null);
}
public long updateBySno(String sno,Student s)
{
ContentValues cv=new ContentValues();
cv.put(Sno,s.sno);
cv.put(Sname,s.sname);
cv.put(Sclass,s.Class);
return db.update(Table,cv,"Sno="+sno,null);
}
public Student[]convert(Cursor c)
{
int count=c.getCount();
if(count==0||!c.moveToFirst())
{
return null;
}
Student students[]=new Student[count];
for(int i=0;i<count;i++)
{
students[i]=new Student();
students[i].sno=c.getString(c.getColumnIndex(Sno));
students[i].sname=c.getString(c.getColumnIndex(Sname));
students[i].Class=c.getString(c.getColumnIndex(Sclass));
c.moveToNext();
}
return students;
}
private static class DBhelper extends SQLiteOpenHelper
{
public DBhelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
{
super(context,name,factory,version);
}
private static final String create="create table "+Table+"("+Id+" integer primary key autoincrement, "+Sclass
+" text, "+Sno+" text, "+Sname+" text)";
public void onCreate(SQLiteDatabase db)
{
db.execSQL(create);
}
public void onUpgrade(SQLiteDatabase db,int oldversion,int newversion)
{
db.execSQL("drop table if exists "+Table);
onCreate(db);
}
}
public void close()
{
if(db!=null)
{
db.close();
db=null;
}
}
}
MainActivity
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
Button add,update,clear,deleteAll,show,deleteone; EditText t1,t2,t3,input; ListView listView;List<String> list=new ArrayList<String>();
DBAdapter adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(EditText)findViewById(R.id.e1);
t2=(EditText)findViewById(R.id.e2);
t3=(EditText)findViewById(R.id.e3);
input=(EditText)findViewById(R.id.input);
listView=(ListView)findViewById(R.id.listview);
add=(Button)findViewById(R.id.add);
update=(Button)findViewById(R.id.update);
clear=(Button)findViewById(R.id.clear);
deleteAll=(Button)findViewById(R.id.deleteAll);
deleteone=(Button)findViewById(R.id.deleteone);
show=(Button)findViewById(R.id.show);
adapter=new DBAdapter(this);
adapter.open();
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Student s=new Student();
s.Class=t1.getText().toString();
s.sno=t2.getText().toString();
s.sname=t3.getText().toString();
adapter.insert(s);
list.add(" "+t1.getText().toString()+" "+t2.getText().toString()+" "+t3.getText().toString());
ArrayAdapter<String>adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
list.clear();
ArrayAdapter<String>adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
}
});
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Student s=new Student();
s.Class=t1.getText().toString();
s.sno=t2.getText().toString();
s.sname=t3.getText().toString();
adapter.updateBySno(input.getText().toString(),s);
}
});
deleteone.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
adapter.deleteOne(input.getText().toString());
}
});
deleteAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
adapter.delete();
list.clear();
ArrayAdapter<String>adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
}
});
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
list.clear();
ArrayAdapter<String>adapters=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapters);
Student students[]=adapter.find();
for(int i=0;i<students.length;i++)
{
list.add(students[i].toString());
}
listView.setAdapter(adapters);
}
});
listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
contextMenu.add(0,0,0,"删除");
}
});
}
public boolean onContextItemSelected(MenuItem item)
{
AdapterView.AdapterContextMenuInfo info=(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
int id=(int)info.id;
list.remove(id);
String s[]=list.get(id).split(" ");
t1.setText(""+s[1]);
// adapter.deleteOne(s[1]);
ArrayAdapter<String>adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
return true;
}
}
Student
public class Student {
public String sno;
public String sname;
public String Class;
public String toString()
{
return Class+" "+sno+" "+sname;
}
}
秒表
MainActivity
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements Button.OnClickListener {
Button clear,start,stop;
TextView textView;
boolean isStop=false,isStart=true;
String result;
int sec=0,min=0,hour=0;
Handler handler=new Handler();
Runnable run=new Runnable() {
@Override
public void run() {
if(!isStop){
sec=sec+1;
if(sec==60){
min=min+1;
sec=0;
}
if(min==60){
hour++;
min=0;
}
if(hour<10)
result="0"+hour;
else
result=""+hour;
if(min<10)
result=result+":0"+min;
else
result=result+":"+min;
if(sec<10)
result=result+":0"+sec;
else
result=result+":"+sec;
textView.setText(result);
handler.postDelayed(this,1000);
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clear=(Button)findViewById(R.id.clear);
start=(Button)findViewById(R.id.start);
stop=(Button)findViewById(R.id.stop);
textView=(TextView)findViewById(R.id.show);
clear.setOnClickListener(this);
start.setOnClickListener(this);
stop.setOnClickListener(this);
}
public void onClick(View view){
switch (view.getId()){
case R.id.clear:
clear();
break;
case R.id.start:
if(isStart){
clock().run();
isStart=false;
break;
}
break;
case R.id.stop:
isStart=true;
isStop=true;
break;
}
}
public Thread clock()
{
isStop=false;
return new Thread(run);
}
public void clear()
{
isStart=true;
isStop=true;
textView.setText("00:00:00");
sec = min = hour = 0;
}
}
sy3
最新推荐文章于 2024-04-25 14:07:54 发布