Android の Contacts に似たアプリケーションを作成しました。リストにいくつかのアイテムがあり、それらをクリックすると、データベースから情報が取得され、別のビューに表示されます。 . 助けてください、私はアンドロイドプログラミングが初めてです。すべての提案を歓迎
ありがとうございました
public class Contacts extends Activity implements OnClickListener {
int NewContact_Request_Code = 1;
Button newcontact;
ListView listview;
public static final String LOG_TAG = "Contacts";
int mInt = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contactview);
// Set the content to contactview.xml
// newcontact = NEW CONTACT button
// listview = MyList List View
newcontact = (Button) findViewById(R.id.baddcontact);
listview = (ListView) findViewById(R.id.mylist);
// Make a New Database
DBContact info = new DBContact(this);
// Open , get Information from database and close it
info.open();
String[] data = info.queryAll();
info.close();
// listview = getListView();
listview.setTextFilterEnabled(true);
// Display the names
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Contacts.this,
android.R.layout.simple_list_item_1, data);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView adapterView, View view,
int position, long id) {
String nameclicked = adapterView.getItemAtPosition(position)
.toString();
Intent viewintent = new Intent(Contacts.this, ViewContact.class);
viewintent.putExtra("name_clicked", nameclicked);
startActivity(viewintent);
}
});
newcontact.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
Intent newintent = new Intent(Contacts.this, AddNewContact.class);
// start actiivity for result - to get the name of the new contact
startActivityForResult(newintent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
// pass the value of the string via cursor and update the list
}
}
ViewCONtact.class
public class ViewContact extends Activity implements OnClickListener {
Button ViewPPhone, ViewHPhone, ViewOPhone, EditContact;
TextView ViewName;
TextView ViewPersonalPhone;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.viewcontact);
Bundle extras = getIntent().getExtras();
String name = extras.getString("name_clicked");
Long l = Long.parseLong(name);
DBContact getdetails = new DBContact(this);
getdetails.open();
String returnedname = getdetails.getName(l);
String returnedpphone = getdetails.getPphone(l);
String returnedhphone = getdetails.getHphone(l);
String returnedophone = getdetails.getOphone(l);
getdetails.close();
ViewName.setText(returnedname);
ViewPPhone.setText(returnedpphone);
ViewHPhone.setText(returnedhphone);
ViewOPhone.setText(returnedophone);
EditContact = (Button) findViewById(R.id.bEditContact);
EditContact.setOnClickListener(this);
ViewPPhone = (Button) findViewById(R.id.ViewPersonalPhoneNumber);
ViewPPhone.setOnClickListener(this);
ViewHPhone = (Button) findViewById(R.id.ViewHomePhoneNumber);
ViewHPhone.setOnClickListener(this);
ViewOPhone = (Button) findViewById(R.id.ViewOfficePhoneNumber);
ViewOPhone.setOnClickListener(this);
}
public void onClick(View view) {
// TODO Auto-generated method stub
switch (view.getId()) {
case R.id.ViewPersonalPhoneNumber:
Intent dialpersonalphone = new
Intent(android.content.Intent.ACTION_DIAL,
Uri.parse("returnedpphone"));
startActivity(dialpersonalphone );
break;
case R.id.ViewHomePhoneNumber:
Intent dialhome = new
Intent(android.content.Intent.ACTION_DIAL,
Uri.parse("returnedhphone"));
startActivity(dialhome);
break;
case R.id.ViewOfficePhoneNumber:
Intent dialoffice = new
Intent(android.content.Intent.ACTION_DIAL,
Uri.parse("returnedophone"));
startActivity(dialoffice);
break;
case R.id.bEditContact:
startActivity(new Intent("com.example.contactlist.EDITCONTACT"));
break;
}
}
}
ログキャット
09-26 02:52:11.527: W/dalvikvm(1605): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-26 02:52:11.558: E/AndroidRuntime(1605): FATAL EXCEPTION: main
09-26 02:52:11.558: E/AndroidRuntime(1605): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.contactlist/com.example.contactlist.ViewContact}: java.lang.NumberFormatException: Invalid long: ""
09-26 02:52:11.558: E/AndroidRuntime(1605): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-26 02:52:11.558: E/AndroidRuntime(1605): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-26 02:52:11.558: E/AndroidRuntime(1605): at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-26 02:52:11.558: E/AndroidRuntime(1605): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-26 02:52:11.558: E/AndroidRuntime(1605): at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 02:52:11.558: E/AndroidRuntime(1605): at android.os.Looper.loop(Looper.java:137)
09-26 02:52:11.558: E/AndroidRuntime(1605): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-26 02:52:11.558: E/AndroidRuntime(1605): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 02:52:11.558: E/AndroidRuntime(1605): at java.lang.reflect.Method.invoke(Method.java:511)
09-26 02:52:11.558: E/AndroidRuntime(1605): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-26 02:52:11.558: E/AndroidRuntime(1605): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-26 02:52:11.558: E/AndroidRuntime(1605): at dalvik.system.NativeStart.main(Native Method)
09-26 02:52:11.558: E/AndroidRuntime(1605): Caused by: java.lang.NumberFormatException: Invalid long: ""
09-26 02:52:11.558: E/AndroidRuntime(1605): at java.lang.Long.invalidLong(Long.java:125)
09-26 02:52:11.558: E/AndroidRuntime(1605): at java.lang.Long.parseLong(Long.java:346)
09-26 02:52:11.558: E/AndroidRuntime(1605): at java.lang.Long.parseLong(Long.java:319)
09-26 02:52:11.558: E/AndroidRuntime(1605): at com.example.contactlist.ViewContact.onCreate(ViewContact.java:24)
09-26 02:52:11.558: E/AndroidRuntime(1605): at android.app.Activity.performCreate(Activity.java:5008)
09-26 02:52:11.558: E/AndroidRuntime(1605): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-26 02:52:11.558: E/AndroidRuntime(1605): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-26 02:52:11.558: E/AndroidRuntime(1605): ... 11 more
データベースクラス
public class DBContact {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "Name";
public static final String KEY_PERSONALPHONE = "Personalnumber";
public static final String KEY_HOMEPHONE = "Homenumber";
public static final String KEY_OFFICEPHONE = "Officenumber";
private static final String DATABASE_NAME = "Contacts";
private static final String DATABASE_TABLE = "ContactList";
private static final int DATABASE_VERSION = 1;
// Instance of the class DbHelper
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
public static final String[] KEYS_ALL = { DBContact.KEY_ROWID,
DBContact.KEY_NAME, DBContact.KEY_PERSONALPHONE,
DBContact.KEY_HOMEPHONE, DBContact.KEY_OFFICEPHONE };
private static class DbHelper extends SQLiteOpenHelper {
private static final String DATABASE_CREATE = "create table ContactList (_id integer primary key autoincrement, "
+ "Name text not null, Personalnumber text not null, Homenumber text not null, Officenumber text not null); ";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase ourDatabase) {
// TODO Auto-generated method stub
try {
ourDatabase.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase ourDatabase, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
ourDatabase.execSQL("DROP TABLE IF EXISTS ContactList");
onCreate(ourDatabase);
}
}
// Context of constructor withhin our Class
public DBContact(Context c) {
ourContext = c;
}
// Opens the database
public DBContact open() throws SQLException {
// Constructor for DB Helper class takes in a Context
// Context is passed in is "ourContext" for within our class
ourHelper = new DbHelper(ourContext);
// Passes in DB Name and Version
// ourDatabase is of type SQLite DataBase
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
// Closes the database connection
public void close() {
// refer to our DbHelper and close the SQLite DataBase Helper
ourHelper.close();
ourHelper = null;
ourDatabase = null;
}
// Deletes the Row
public boolean deleteRow(long rowId) {
return ourDatabase.delete(DATABASE_TABLE, DBContact.KEY_ROWID + "="
+ rowId, null) > 0;
}
public String[] queryAll() {
String[] columns = new String[] { KEY_NAME };
Cursor cursor = ourDatabase.query(DATABASE_TABLE, columns, null, null,
null, null, null);
if (cursor != null) {
try {
final int nameColumnIndex = cursor.getColumnIndex(KEY_NAME);
List<String> names = new ArrayList<String>();
cursor.moveToFirst();
while (cursor.moveToNext()) {
names.add(cursor.getString(nameColumnIndex));
}
return names.toArray(new String[names.size()]);
} finally {
cursor.close();
}
}
return null;
//return columns;
}
/*public Cursor queryAll(){
String[] columns = new String[] {KEY_NAME};
return ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
if(data == null){
columns[0] = "NO CONTACTS PRESENT";
return columns;
}else{
return columns;
}
}*/
public long newRow(String name, String pphone, String hphone, String ophone) {
// TODO Auto-generated method stub
ContentValues newvalue = new ContentValues();
newvalue.put(KEY_NAME, name);
newvalue.put(KEY_PERSONALPHONE, pphone);
newvalue.put(KEY_HOMEPHONE, hphone);
newvalue.put(KEY_OFFICEPHONE, ophone);
return ourDatabase.insert(DATABASE_TABLE, null, newvalue);
}
public String getName(Long l) {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ROWID, KEY_NAME,
KEY_PERSONALPHONE, KEY_HOMEPHONE, KEY_OFFICEPHONE };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "="
+ 1, null, null, null, null);
if (c != null) {
c.moveToFirst();
String name = c.getString(1);
return name;
}
return null;
}
public String getPphone(Long l) {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ROWID, KEY_NAME,
KEY_PERSONALPHONE, KEY_HOMEPHONE, KEY_OFFICEPHONE };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "="
+ 1, null, null, null, null);
if (c != null) {
c.moveToFirst();
String Pphone = c.getString(2);
return Pphone;
}
return null;
}
public String getHphone(Long l) {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ROWID, KEY_NAME,
KEY_PERSONALPHONE, KEY_HOMEPHONE, KEY_OFFICEPHONE };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "="
+ 1, null, null, null, null);
if (c != null) {
c.moveToFirst();
String Hphone = c.getString(3);
return Hphone;
}
return null;
}
public String getOphone(Long l) {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ROWID, KEY_NAME,
KEY_PERSONALPHONE, KEY_HOMEPHONE, KEY_OFFICEPHONE };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "="
+ 1, null, null, null, null);
if (c != null) {
c.moveToFirst();
String Ophone = c.getString(4);
return Ophone;
}
return null;
}
}