parent
ea4c90438d
commit
554c5fb676
@ -0,0 +1,177 @@
|
||||
package com.example.myapplication;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ListView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import Util.myutil;
|
||||
|
||||
public class ElemeMainPage extends Activity implements View.OnClickListener {
|
||||
MyHelper myHelper ;
|
||||
SQLiteDatabase sqLiteDatabase;
|
||||
private List<Shop> ShopLists = new ArrayList<Shop>();
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.main_layout);
|
||||
Button search_Icon = findViewById(R.id.search_Icon);
|
||||
search_Icon.setOnClickListener(this);
|
||||
myHelper= new MyHelper(this);
|
||||
sqLiteDatabase = myHelper.getWritableDatabase();
|
||||
initShops(sqLiteDatabase);
|
||||
ShopAdapter adapter = new ShopAdapter(ElemeMainPage.this, R.layout.food_shops, ShopLists);
|
||||
ListView listView = (ListView) findViewById(R.id.list_view);
|
||||
listView.setAdapter(adapter);
|
||||
Button order_btn = (Button) findViewById(R.id.orderbtn);
|
||||
myutil util = new myutil();
|
||||
util.setListener(order_btn, ElemeMainPage.this, News.class);
|
||||
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
Shop shop = (Shop) listView.getItemAtPosition(position);
|
||||
Intent intent = new Intent(ElemeMainPage.this, OrderProcessingPage.class);
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("shop_name", shop.getShop_name());
|
||||
intent.putExtras(bundle);
|
||||
ElemeMainPage.this.startActivity(intent);
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void initShops(SQLiteDatabase sqLiteDatabase) {
|
||||
Cursor cursor = sqLiteDatabase.query("SHOPS", null, null, null, null, null, null);
|
||||
if (cursor.getCount() == 0) {
|
||||
return;
|
||||
|
||||
} else {
|
||||
cursor.moveToFirst();
|
||||
|
||||
ShopLists.add(new Shop(cursor.getInt(0),
|
||||
cursor.getString(1),cursor.getString(2)
|
||||
,cursor.getString(3),cursor.getInt(4),
|
||||
cursor.getInt(5),cursor.getInt(6),
|
||||
cursor.getString(7),cursor.getString(8),
|
||||
cursor.getString(9),cursor.getString(10),
|
||||
cursor.getString(11),cursor.getString(12)));
|
||||
}
|
||||
while (cursor.moveToNext()) {
|
||||
ShopLists.add(new Shop(cursor.getInt(0),
|
||||
cursor.getString(1),cursor.getString(2)
|
||||
,cursor.getString(3),cursor.getInt(4),
|
||||
cursor.getInt(5),cursor.getInt(6),
|
||||
cursor.getString(7),cursor.getString(8),
|
||||
cursor.getString(9),cursor.getString(10),
|
||||
cursor.getString(11),cursor.getString(12)));
|
||||
}
|
||||
cursor.close();
|
||||
sqLiteDatabase.close();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
switch (v.getId()){
|
||||
case R.id.search_Icon:
|
||||
EditText search_EditText = findViewById(R.id.search_EditText);
|
||||
String keyWord = search_EditText.getText().toString();
|
||||
if (keyWord.trim().equals(""))
|
||||
{
|
||||
SQLiteDatabase sqLiteDatabase;
|
||||
MyHelper myHelper = new MyHelper(ElemeMainPage.this);
|
||||
sqLiteDatabase=myHelper.getReadableDatabase();
|
||||
LinearLayout linearLayout = findViewById(R.id.mylist);
|
||||
linearLayout.setBackgroundResource(R.color.white);
|
||||
List<Shop> ShopList = new ArrayList<Shop>();
|
||||
Cursor cursor = sqLiteDatabase.query("SHOPS", null,null,null,null,null,null);
|
||||
if (cursor.getCount() == 0) {
|
||||
Toast.makeText(this,"很遗憾,没有搜索到", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
|
||||
}
|
||||
cursor.moveToFirst();
|
||||
ShopList.add(new Shop(cursor.getInt(0),
|
||||
cursor.getString(1),cursor.getString(2)
|
||||
,cursor.getString(3),cursor.getInt(4),
|
||||
cursor.getInt(5),cursor.getInt(6),
|
||||
cursor.getString(7),cursor.getString(8),
|
||||
cursor.getString(9),cursor.getString(10),
|
||||
cursor.getString(11),cursor.getString(12)));
|
||||
while (cursor.moveToNext()) {
|
||||
ShopList.add(new Shop(cursor.getInt(0),
|
||||
cursor.getString(1),cursor.getString(2)
|
||||
,cursor.getString(3),cursor.getInt(4),
|
||||
cursor.getInt(5),cursor.getInt(6),
|
||||
cursor.getString(7),cursor.getString(8),
|
||||
cursor.getString(9),cursor.getString(10),
|
||||
cursor.getString(11),cursor.getString(12)));
|
||||
}
|
||||
ShopAdapter adapter = new ShopAdapter(ElemeMainPage.this, R.layout.food_shops, ShopLists);
|
||||
ListView listView = (ListView) findViewById(R.id.list_view);
|
||||
listView.setAdapter(adapter);
|
||||
return;
|
||||
}
|
||||
if (!keyWord.trim().equals("")){
|
||||
List<Shop> ShopLists ;
|
||||
SQLiteDatabase sqLiteDatabase ;
|
||||
MyHelper myHelper = new MyHelper(ElemeMainPage.this);
|
||||
sqLiteDatabase = myHelper.getReadableDatabase();
|
||||
ContentValues values = new ContentValues();
|
||||
values.put("shop_name",keyWord);
|
||||
|
||||
LinearLayout linearLayout = findViewById(R.id.mylist);
|
||||
Cursor cursor = sqLiteDatabase.query("SHOPS", null, "shop_name like ?",new String[]{"%"+keyWord+"%"},null,null,null);
|
||||
if (cursor.getCount()==0) {
|
||||
Toast.makeText(this, "很遗憾,没有搜索到", Toast.LENGTH_SHORT).show();
|
||||
linearLayout.setBackgroundResource(R.mipmap.havenofood);
|
||||
ShopLists = new ArrayList<Shop>();
|
||||
ShopAdapter adapter = new ShopAdapter(ElemeMainPage.this, R.layout.food_item, ShopLists);
|
||||
ListView listView = (ListView) findViewById(R.id.list_view);
|
||||
listView.setAdapter(adapter);
|
||||
return;
|
||||
}else {
|
||||
ShopLists = new ArrayList<Shop>();
|
||||
cursor.moveToFirst();
|
||||
ShopLists.add(new Shop(cursor.getInt(0),
|
||||
cursor.getString(1),cursor.getString(2)
|
||||
,cursor.getString(3),cursor.getInt(4),
|
||||
cursor.getInt(5),cursor.getInt(6),
|
||||
cursor.getString(7),cursor.getString(8),
|
||||
cursor.getString(9),cursor.getString(10),
|
||||
cursor.getString(11),cursor.getString(12)));
|
||||
while (cursor.moveToNext()) {
|
||||
ShopLists.add(new Shop(cursor.getInt(0),
|
||||
cursor.getString(1),cursor.getString(2)
|
||||
,cursor.getString(3),cursor.getInt(4),
|
||||
cursor.getInt(5),cursor.getInt(6),
|
||||
cursor.getString(7),cursor.getString(8),
|
||||
cursor.getString(9),cursor.getString(10),
|
||||
cursor.getString(11),cursor.getString(12)));
|
||||
}
|
||||
linearLayout = findViewById(R.id.mylist);
|
||||
linearLayout.setBackgroundResource(R.color.white);
|
||||
ShopAdapter adapter = new ShopAdapter(ElemeMainPage.this, R.layout.food_shops, ShopLists);
|
||||
ListView listView = (ListView) findViewById(R.id.list_view);
|
||||
listView.setAdapter(adapter);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue