Code: Mainactivity part of the code:
package com.example.tongxunlu;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity {
Button btn1,btn2;
ListView listview;
public static MyOpenHelper dbhelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.button1);
btn2=(Button)findViewById(R.id.button2);
listview=(ListView)findViewById(R.id.lv);
dbhelper=new MyOpenHelper(MainActivity.this, "myDict.db10", 1);
Cursor cursor=dbhelper.getReadableDatabase().rawQuery("select * from contacts", null);
inflateList(cursor);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent=new Intent();
intent.setClass(MainActivity.this, BaocunActivity.class);
startActivity(intent);
MainActivity.this.finish();
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent=new Intent();
intent.setClass(MainActivity.this, shanchuActivity.class);
startActivity(intent);
MainActivity.this.finish();
}
});
}
private void inflateList(Cursor cursor) {
SimpleCursorAdapter simpleCursorAdapter=new SimpleCursorAdapter(MainActivity.this,
R.layout.lian, cursor, new String[]{"name","phone"},
new int[]{R.id.text1,R.id.text2}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listview.setAdapter(simpleCursorAdapter);
listview.setOnItemClickListener(new ItemClickListener());
}
private final class ItemClickListener implements OnItemClickListener{
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListView listview = (ListView) parent;
Cursor cursor1 = (Cursor) listview.getItemAtPosition(position);
String personid = cursor1.getString(cursor1.getColumnIndex("name"));
String personid1 = cursor1.getString(cursor1.getColumnIndex("phone"));
String personid2 = cursor1.getString(cursor1.getColumnIndex("mobile"));
String personid3 = cursor1.getString(cursor1.getColumnIndex("email"));
String personid4 = cursor1.getString(cursor1.getColumnIndex("post"));
String personid5 = cursor1.getString(cursor1.getColumnIndex("addr"));
String personid6 = cursor1.getString(cursor1.getColumnIndex("comp"));
Intent intent=new Intent();
intent.putExtra("username1",personid);
intent.putExtra("userphone1",personid1);
intent.putExtra("usermobile1",personid2);
intent.putExtra("useremail1",personid3);
intent.putExtra("userpost1",personid4);
intent.putExtra("useraddr1",personid5);
intent.putExtra("usercomp1",personid6);
intent.setClass(MainActivity.this, DetailActivity.class);
startActivity(intent);
MainActivity.this.finish();
}
}
@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;
}
}
Run Result: () |