Country.java
[code]
package com.example.test;
import java.io.Serializable;
public class Country implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String countryName;
private String countryCode;
public Country() {
super();
}
public Country(String countryName, String countryCode) {
super();
this.countryName = countryName;
this.countryCode = countryCode;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
}
[/code]
DBHelper.java
[code]
package com.example.test;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "turkcellDB";
// Contacts table name
private static final String TABLE_COUNTRIES = "countries";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
public List<Country> getAllCountries() {
List<Country> countries = new ArrayList<Country>();
SQLiteDatabase db = this.getWritableDatabase();
// String sqlQuery = "SELECT * FROM " + TABLE_COUNTRIES;
// Cursor cursor = db.rawQuery(sqlQuery, null);
Cursor cursor = db.query(TABLE_COUNTRIES, new String[]{"id", "country_name", "country_code"}, null, null, null, null, null);
while (cursor.moveToNext()) {
Country country = new Country();
country.setId(cursor.getInt(0));
country.setCountryName(cursor.getString(1));
country.setCountryCode(cursor.getString(2));
countries.add(country);
}
return countries;
}
public void insertCountry(Country country) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("country_name", country.getCountryName());
values.put("country_code", country.getCountryCode());
db.insert(TABLE_COUNTRIES, null, values);
db.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + TABLE_COUNTRIES + "(id INTEGER PRIMARY KEY,country_name TEXT,country_code TEXT" + ")";
Log.d("DBHelper", "SQL : " + sql);
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_COUNTRIES);
onCreate(db);
}
}
[/code]
MainActivity.java
[code]
package com.example.test;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView customListView = (ListView) findViewById(R.id.listview);
DBHelper dbHelper = new DBHelper(getApplicationContext());
SharedPreferences settings = getSharedPreferences("SQL", 0);
boolean firstTime = settings.getBoolean("firstTime", true);
if (firstTime) {
dbHelper.insertCountry(new Country("Turkiye", "90"));
dbHelper.insertCountry(new Country("Amerika", "1"));
dbHelper.insertCountry(new Country("Ingiltere", "44"));
dbHelper.insertCountry(new Country("Almanya", "49"));
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstTime", false);
editor.commit();
}
List<Country> countries = dbHelper.getAllCountries();
MyListAdapter myListAdapter = new MyListAdapter(MainActivity.this, countries);
customListView.setAdapter(myListAdapter);
}
}
[/code]
MyListAdapter.java
[code]
package com.example.test;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyListAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<Country> countryList;
public MyListAdapter(Activity activity, List<Country> countries) {
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
countryList = countries;
}
@Override
public int getCount() {
return countryList.size();
}
@Override
public Object getItem(int position) {
return countryList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(com.example.test.R.id.listview, null); // create layout from
TextView textView = (TextView) vi.findViewById(R.id.row_textview); // user name
Country country = countryList.get(position);
textView.setText(country.getCountryName());
return vi;
}
}
[/code]
activity_main.xml
[code]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<TextView
android:id="@+id/row_textview"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_gravity="center" />
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_gravity="center" />
</LinearLayout>
[/code]
aldığım hata ise
Sorry!
The application test(process com.
example.test) has stopped
unexpectedly. Please try again.
Force close