Завершите пример с кодом базы данных по вашему желанию и вставьте, просмотрите и ищите функциональность, которая работает должным образом и протестирована.
Всего 6 файлов Java и 4 файла макета.
1.Database.java
public class Database {
public static final String DATABASE_NAME = "Urunler";
public static final String TABLE_NAME = "urunler_tablosu";
public static final int DATABSE_VERSION = 1;
public static final String URUNKODU = "UrunKodu";
public static final String URUNADI = "UrunAdi";
public static final String ADET = "Adet";
public static final String ALISFIYATI = "AlisFiyati";
public static final String SATISFIYATI = "SatisFiyati";
//to create a table
private static final String TABLE_CREATE_URUNLER_TABLOSU = "create table urunler_tablosu(UrunKodu TEXT,UrunAdi TEXT,Adet REAL,AlisFiyati REAL,SatisFiyati REAL);";
private Context context;
SQLiteDatabase db; // manipulation with database
DatabaseHelper dbhelper;
public Database(Context ctx) {
// TODO Auto-generated constructor stub
this.context = ctx;
dbhelper = new DatabaseHelper(ctx);
}
//SQLITEOpenHelper has methods to creae and open
class DatabaseHelper extends SQLiteOpenHelper {
//DatabaseHelper's constructor will create the database
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABSE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(TABLE_CREATE_URUNLER_TABLOSU);
Log.d("table is created..", "URUNLER_TABLOSU");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
public Database Open() throws SQLException {
db = dbhelper.getWritableDatabase();
return this;
}
public void Close() {
dbhelper.close();
}
//==== as mapping REAL in sql is float in java =================
public long insertData(String UrunKodu, String UrunAdi, float Adet, float AlisFiyati, float SatisFiyati) {
ContentValues initialvalues = new ContentValues();
initialvalues.put(URUNKODU, UrunKodu);
initialvalues.put(URUNADI, UrunAdi);
initialvalues.put(ADET, Adet + "");
initialvalues.put(ALISFIYATI, AlisFiyati + "");
initialvalues.put(SATISFIYATI, SatisFiyati + "");
Log.d(URUNKODU, UrunKodu + "");
Toast.makeText(context, "Data inserted", Toast.LENGTH_LONG).show();
return db.insert(TABLE_NAME, null, initialvalues);
}
public Cursor getAllData() {
String query = "SELECT * FROM " + TABLE_NAME;
Log.d("gks query=", query + "");
Cursor cur = db.rawQuery(query, null);
return cur;
}
}
2.POJOUrunKodu.java
public class POJOUrunKodu {
String UrunKodu, UrunAdi, Adet, AlisFiyati, SatisFiyati;
public POJOUrunKodu(String urunKodu, String urunAdi, String adet, String alisFiyati, String satisFiyati) {
UrunKodu = urunKodu;
UrunAdi = urunAdi;
Adet = adet;
AlisFiyati = alisFiyati;
SatisFiyati = satisFiyati;
}
}
3.CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
private ArrayList<POJOUrunKodu> _data;
Context _c;
CustomAdapter( Context c,ArrayList<POJOUrunKodu> data) {
_data = data;
_c = c;
Log.d("inside customAdapter", "inside customAdapter constructor...");
}
public int getCount() {
// TODO Auto-generated method stub
return _data.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return _data.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) _c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.entityurunkodu, null);
}
TextView tvlist_urunKodu = (TextView) v.findViewById(R.id.tvlist_urunKodu);
TextView tvlist_urunAdi = (TextView) v.findViewById(R.id.tvlist_urunAdi);
TextView tvlist_adet = (TextView) v.findViewById(R.id.tvlist_adet);
TextView tvlist_alisFiyati = (TextView) v.findViewById(R.id.tvlist_alisFiyati);
TextView tvlist_satisFiyati = (TextView) v.findViewById(R.id.tvlist_satisFiyati);
Log.d("tvcredit==>", " "+tvlist_urunAdi.getText().toString());
POJOUrunKodu tpj = _data.get(position);
tvlist_urunKodu.setText(tpj.UrunKodu+"");
tvlist_urunAdi.setText(tpj.UrunAdi+"");
tvlist_adet.setText(tpj.Adet+"");
tvlist_alisFiyati.setText(tpj.AlisFiyati+"");
tvlist_satisFiyati.setText(tpj.SatisFiyati+"");
return v;
}
}
4.InsertActivity.java
public class InsertActivity extends AppCompatActivity {
EditText edt_UrunKodu, edt_UrunAdi, edt_Adet, edt_AlisFiyati, edt_SatisFiyati;
Button btn_insert;
Database db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insert);
//======== findviewbyid starts ======
edt_UrunKodu = (EditText) findViewById(R.id.edt_UrunKodu);
edt_UrunAdi = (EditText) findViewById(R.id.edt_UrunAdi);
edt_Adet = (EditText) findViewById(R.id.edt_Adet);
edt_AlisFiyati = (EditText) findViewById(R.id.edt_AlisFiyati);
edt_SatisFiyati = (EditText) findViewById(R.id.edt_SatisFiyati);
btn_insert = (Button) findViewById(R.id.btn_insert);
//======== findviewbyid ends ======
db=new Database(getApplicationContext());
db.Open();
db.Close();
btn_insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (edt_UrunKodu.getText().toString().trim().length() != 0 ||
edt_UrunAdi.getText().toString().trim().length() != 0 ||
edt_Adet.getText().toString().trim().length() != 0 ||
edt_AlisFiyati.getText().toString().trim().length() != 0 ||
edt_SatisFiyati.getText().toString().trim().length() != 0) {
String sUrunKodu=edt_UrunKodu.getText().toString().trim();
String sUrunAdi=edt_UrunAdi.getText().toString().trim();
String sAdet=edt_Adet.getText().toString().trim();
String sAlisFiyati=edt_AlisFiyati.getText().toString().trim();
String sSatisFiyati=edt_SatisFiyati.getText().toString().trim();
db.Open();
db.insertData(sUrunKodu,sUrunAdi,Float.parseFloat(sAdet),Float.parseFloat(sAlisFiyati),Float.parseFloat(sSatisFiyati));
db.Close();
}
}
});
}
}
5.SearchViewActivity.java
public class SearchViewActivity extends AppCompatActivity {
EditText edt_search;
ListView lv_data;
Database db;
ArrayList<POJOUrunKodu> arrayListUrunKodu;
ArrayList<POJOUrunKodu> arrayListUrunKodu_search;
CustomAdapter customAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_view);
edt_search=(EditText)findViewById(R.id.edt_search);
lv_data=(ListView)findViewById(R.id.lv_data);
db=new Database(getApplicationContext());
db.Open();
db.Close();
load_all_data();
edt_search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String text = s.toString();
filter(text);
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
public void load_all_data()
{
arrayListUrunKodu=new ArrayList<>();
arrayListUrunKodu_search=new ArrayList<>();
db.Open();
Cursor cur=db.getAllData();
if(cur.moveToFirst())
{
do {
String sUrunKodu=cur.getString(0);
String sUrunAdi=cur.getString(1);
String sAdet=cur.getString(2);
String sAlisFiyati=cur.getString(3);
String sSatisFiyati=cur.getString(4);
arrayListUrunKodu.add(new POJOUrunKodu(sUrunKodu,sUrunAdi,sAdet,sAlisFiyati,sSatisFiyati));
arrayListUrunKodu_search.add(new POJOUrunKodu(sUrunKodu,sUrunAdi,sAdet,sAlisFiyati,sSatisFiyati));
}while (cur.moveToNext());
}
cur.close();
db.Close();
customAdapter=new CustomAdapter(SearchViewActivity.this,arrayListUrunKodu);
lv_data.setAdapter(customAdapter);
}
// =========Filter method starts=======
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
Log.d("filter",charText+"");
arrayListUrunKodu.clear();
if (charText.length() == 0) {
arrayListUrunKodu.addAll(arrayListUrunKodu_search);
Log.d("data","all");
Log.d("data size=",arrayListUrunKodu.size()+"");
} else {
for (POJOUrunKodu pod : arrayListUrunKodu_search) {
if (pod.UrunKodu.toLowerCase(Locale.getDefault()).contains(charText)||
pod.UrunAdi.toLowerCase(Locale.getDefault()).contains(charText)) {
arrayListUrunKodu.add(pod);
}
}
Log.d("data","filter");
Log.d("filter size=",arrayListUrunKodu.size()+"");
}
customAdapter.notifyDataSetChanged();
}
}
6.MainActivity.java
public class MainActivity extends AppCompatActivity {
Button btn_insert_data,btn_search_view_data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_insert_data=(Button)findViewById(R.id.btn_insert_data);
btn_search_view_data=(Button)findViewById(R.id.btn_search_view_data);
btn_insert_data.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent_insert=new Intent(MainActivity.this,InsertActivity.class);
startActivity(intent_insert);
}
});
btn_search_view_data.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent_insert=new Intent(MainActivity.this,SearchViewActivity.class);
startActivity(intent_insert);
}
});
}
}
Файлы макетов здесь, для customadapter этот файл сущности необходим
1.entityurunkodu.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<TextView
android:id="@+id/tvlist_urunKodu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#f00"
></TextView>
<TextView
android:id="@+id/tvlist_urunAdi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#789"
android:layout_marginTop="5dp"
></TextView>
<TextView
android:id="@+id/tvlist_adet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#00f"
android:layout_marginTop="5dp"
></TextView>
<TextView
android:id="@+id/tvlist_alisFiyati"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#f0f"
android:layout_marginTop="5dp"
></TextView>
<TextView
android:id="@+id/tvlist_satisFiyati"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#000"
android:layout_marginTop="5dp"
></TextView>
== content_search_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".SearchViewActivity"
tools:showIn="@layout/activity_search_view"
android:orientation="vertical">
<EditText
android:id="@+id/edt_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="seach urunkodu"
android:textSize="15sp"
android:textColor="#f00"/>
<ListView
android:id="@+id/lv_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"/>
=====content_insert.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".InsertActivity"
tools:showIn="@layout/activity_insert"
android:orientation="vertical">
<EditText
android:id="@+id/edt_UrunKodu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="UrunKodu"
android:layout_marginTop="20dp"/>
<EditText
android:id="@+id/edt_UrunAdi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="UrunAdi"
android:layout_marginTop="10dp"/>
<EditText
android:id="@+id/edt_Adet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Adet"
android:layout_marginTop="10dp"/>
<EditText
android:id="@+id/edt_AlisFiyati"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="AlisFiyati"
android:layout_marginTop="10dp"/>
<EditText
android:id="@+id/edt_SatisFiyati"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="SatisFiyati"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/btn_insert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="INSERT"
android:textSize="25sp"
android:textColor="#fff"
android:background="#f00"
android:layout_marginTop="10dp"/>
===== activity_main.xml
<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"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/btn_insert_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="INSERT DATA"
android:textSize="25sp"
android:textColor="#fff"
android:background="#f00"/>
<Button
android:id="@+id/btn_search_view_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search and View DATA"
android:layout_marginTop="50dp"
android:textSize="25sp"
android:textColor="#fff"
android:background="#00f"/>
Итак, это отлично работает код с файлами XMLи Java-файлы и файлы деятельности. вы можете добавлять контент и активность так, как вам нужно. В любом месте для выравнивания вы можете использовать ALT + CLTR + L для Android Studio. уверен, что это поможет вам.