查找

想实现的是:第一个界面搜索(SearchActivity.java)--->第二个界面搜索结果(SearchShowActivity.java)---->第三个界面点击界面2中的搜索结果后显示详细信息(LookUpMenuActivity.java)。其中,第3个界面也会用于查看功能中。第三个界面就是对于菜品详细信息的显示。

1.对应的.xml:

1.1search_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mymenu"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/hua" >


    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#666666"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/sscp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="搜索菜谱" >
        </Button>

        <Button
            android:id="@+id/ckcp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:layout_weight="1"
            android:text="查看菜谱" />

        <Button
            android:id="@+id/tjcp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="添加菜谱" />
    </LinearLayout>

    <EditText
        android:id="@+id/editText1"
        android:layout_width="230dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout1"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="117dp"
        android:ems="10" />

    <Button
        android:id="@+id/search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText1"
        android:layout_alignBottom="@+id/editText1"
        android:layout_toRightOf="@+id/editText1"
        android:text="搜索" />

</RelativeLayout>

1.2searchmenu_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@drawable/hua">
<ListView
         android:id="@+id/listView2_search"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginLeft="20dp"
         android:layout_marginRight="20dp"
         >
      </ListView>    

</LinearLayout>

1.3 lookupmenu_activity.java

<ScrollView   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:background="@drawable/hua"
      >
     
<RelativeLayout
    android:id="@+id/mymenu"
    android:layout_width="fill_parent"
    android:layout_height="163dp" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#666666"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/sscp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="搜索菜谱" >
        </Button>

        <Button
            android:id="@+id/ckcp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:layout_weight="1"
            android:text="查看菜谱" />

        <Button
            android:id="@+id/tjcp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="添加菜谱" />
    </LinearLayout>        
    
          <TextView
        android:id="@+id/cming"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout1"
        android:layout_marginLeft="21dp"
        android:layout_marginTop="30dp"

        android:text="菜名:"
        android:textAppearance="?android:attr/textAppearanceLarge" />
          
          <TextView
         android:id="@+id/show_name_t"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout1"
        android:layout_toRightOf="@+id/cming"    
        android:layout_marginLeft="40dp"
        android:layout_marginTop="35dp"   
         android:textAppearance="?android:attr/textAppearanceLarge"  
          />
          
          <TextView
        android:id="@+id/cxi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="21dp"
		android:layout_below="@+id/cming"
		android:layout_marginTop="20dp"
        android:text="菜系:"
        android:textAppearance="?android:attr/textAppearanceLarge" />
          
          <TextView
         android:id="@+id/show_classification_t"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
        android:layout_below="@+id/show_name_t"
        android:layout_toRightOf="@+id/cxi"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="35dp" 
         android:textAppearance="?android:attr/textAppearanceLarge"
          />
          
          
            <TextView
        android:id="@+id/prenfangfa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="21dp"
		android:layout_below="@+id/cxi"
		android:layout_marginTop="20dp"
        android:text="烹饪方法:"
        android:textAppearance="?android:attr/textAppearanceLarge" />
          
          <TextView
         android:id="@+id/show_describe_t"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
        android:layout_below="@+id/prenfangfa"
        android:layout_marginLeft="20dp"
         android:textAppearance="?android:attr/textAppearanceLarge"
          />
          
    
    
</RelativeLayout>
</ScrollView>

2 .注意:在searchmenu_activity.xml中,定义了:

<ListView
         android:id="@+id/listView2_search"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginLeft="20dp"
         android:layout_marginRight="20dp"
         >
      </ListView>    

这个listView会用来显示查找出来的结果。需要定义一个list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
     <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
    <TextView 
        android:id="@+id/name"
        android:layout_width="100dp"
        android:layout_height="20dip"
        android:gravity="center"
        android:textStyle="bold|italic"
        android:textColor="#312080"
        android:layout_weight="1"
        />
    <TextView 
        android:id="@+id/classification"
        android:layout_width="180dp"
        android:layout_height="20dip"
        android:padding="3dip" 
        android:textStyle="bold|italic"
        android:textColor="#312080"
        android:layout_weight="1"
        />
  
        </TableRow>
        </TableLayout>

</LinearLayout>

这段代码用来表示刚刚那个listView中显示什么东西。也放在layout下。这里只是让它显示菜名和菜系。

3.3个相应的activity:

3.1SearchActivity.java:用于实现搜索功能:

package com.androidhomework.caipu;


import java.util.ArrayList;
import java.util.HashMap;




import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class SearchActivity extends Activity{
	
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_activity);
		
		//当点击屏幕上的“搜索键的时候”,Button对象的btnsearch被建立,然后对它进行监听
		Button btnsearch = (Button)findViewById(R.id.search);  // 点击桌面上方的“搜索”键
		btnsearch.setOnClickListener(new View.OnClickListener() {

			
			@Override
			public void onClick(View v) {
				
				//定义id属性,foodname,classification,describe
				int num = 0;   //id
				String foodname = null;  //foodname
				String classification = null;  //classification
				String describe = null;  //describe
				// TODO Auto-generated method stub
				
				//editText1定义的是搜索的输入栏,当在输入栏内操作时,就是下面这句
				EditText editText = (EditText) findViewById(R.id.editText1);    //搜索的那个输入栏
				String name = editText.getText().toString();   //这句用来取输入的结果
				DBFoodService foods = new DBFoodService(SearchActivity.this);   //我的是DBFoodService,建立一个foods的实体
				ArrayList<DBFood> list = foods.queryAll();        //queryAll是“查看所有”         建立一个ArrayList叫做list,里面存放的是DBFood类型的数据
				ArrayList<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();  //建立一个ArrayList叫做data,存放的是HashMap类型的数据
				
				

				//如果提供的名字跟已有菜谱的名字相同
				for(DBFood food:list){
					food.getFoodName().toString();  //取值。
					/*if(name.equals(food.getFoodName().toString())){
						HashMap<String,Object> foodh = new HashMap<String,Object>();
						foodh.put("num", food.getId());
						foodh.put("foodName", food.getFoodName());
						foodh.put("foodClassification", food.getFoodClassification());
						foodh.put("describe", food.getDescribe());
						data.add(foodh);
						num = food.getId();
						foodname = food.getFoodName();
						classification = food.getFoodClassification();
						describe = food.getDescribe();
						break;
					}*/
					
			
					if(name.equals(food.getFoodName().toString())){       		//如果输入的name值与food中取到的值相等,将对应的id,foodname,foodclassification,describe返回。
						num = food.getId();                  
						foodname = food.getFoodName();
						classification = food.getFoodClassification();
						describe = food.getDescribe();
						break;
					}
					/*if(name.equals(food.getFoodClassification().toString())){
						num = food.getId();
						foodname = food.getFoodName();
						classification = food.getFoodClassification();
						describe = food.getDescribe();
						break;
					}*/
					
				}
				DBFood food1 = new DBFood();            //新建的DBFood类型的food1将刚刚的值传入。
				food1.setId(num);
				food1.setFoodName(foodname);
				food1.setFoodClassification(classification);
				food1.setDescribe(describe);
				
				
				//如果输入的是菜谱种类
				/*for(Food food:list){
					food.getFoodName().toString();
					if(name.equals(food.getFoodClassification().toString())){
						HashMap<String,Object> foodh = new HashMap<String,Object>();
						foodh.put("num", food.getId());
						foodh.put("foodName", food.getFoodName());
						foodh.put("foodClassification", food.getFoodClassification());
						foodh.put("describe", food.getDescribe());
						data.add(foodh);						
					}
				}*/
				
				
				/*SimpleAdapter adapter = new SimpleAdapter(SearchActivity.this,data,R.layout.item                   //SimpleAdapter:适配器
				                                          ,new String[]{"foodName","foodClassification"}
		                                                  ,new int[]{R.id.name,R.id.classification});
				listView = (ListView) findViewById(R.id.listView2_search);   //listView2_search是显示搜索结果的listview
				//listView.setTextFilterEnabled(true);
				listView.setAdapter(adapter);
				listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> adapterView, View view,
							int position, long id) {
						// TODO Auto-generated method stub
						HashMap<String,Object> itemMap = (HashMap<String, Object>) adapterView.getItemAtPosition(position);
						//int num = (Integer) itemMap.get("id");
						String foodName = (String) itemMap.get("foodName");
						String classification = (String) itemMap.get("foodClassification");
						String describe = (String) itemMap.get("describe");
						
						Food food = new Food();
						//food.setId(num);
						food.setFoodName(foodName);
						food.setFoodClassification(classification);
						food.setDescribe(describe);
						Intent intent = new Intent();
						intent.putExtra("food", food);
						intent.setClass(SearchActivity.this, ShowInfoActivity.class);  //跳转到显示的页面
						SearchActivity.this.startActivity(intent);
					}
					
				});*/
				
				
				//建立新的意图,将food1打包成food用putExtra()传出去,跳转到SearchShowActivity.class中去。
				
				Intent intent = new Intent();     //建立新的意图,
				intent.putExtra("food", food1);         //将food1打包成food用putExtra()传出去
				intent.setClass(SearchActivity.this, SearchShowActivity.class);     //跳转到SearchShowActivity.class中去。
				SearchActivity.this.startActivity(intent);  //开启意图
				finish();
			}
		});
				
				
		
			
			
			
			
				
				
				
		
		
		
		
		
		
		
		
		
	gotoMain();
	}
	
	public void gotoMain()
	{
		
		Button bOk1=(Button)this.findViewById(R.id.sscp);
		bOk1.setOnClickListener(
		new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(SearchActivity.this,SearchActivity.class);      //如果点击的是搜索菜谱,则不变。
				startActivity(intent);
				finish();		
			}
		});
		
		Button bOk2=(Button)this.findViewById(R.id.ckcp);
		bOk2.setOnClickListener(
		new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.d("Caipu", "cha kan cai pu");????这句是干嘛的?
				Intent intent=new Intent(SearchActivity.this,LookupMenuActivity.class);      //如果点击的是查看菜谱,跳到查看菜谱
				startActivity(intent);
				finish();		
			}
		});
		
		Button bOk3=(Button)this.findViewById(R.id.tjcp);
		bOk3.setOnClickListener(
		new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(SearchActivity.this,AddMenuActivity.class);      //创建Intent对象
				startActivity(intent);
				finish();		
			}
		});
		
	}
}


3.2  SearchShowActivity.java用于显示搜索的结果,点击其中ListView将会跳转到LookUpMenuActivity.java中显示具体信息。

package com.androidhomework.caipu;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class SearchShowActivity extends Activity{
	
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.searchshow_activity);
		
		//获得SearchActivity中传过来的数据     intent.putExtra("food", food1);         //将food1打包成food用putExtra()传出去
		Intent intent = this.getIntent();
		DBFood food = (DBFood) intent.getSerializableExtra("food");
		
		
		ArrayList<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();
		HashMap<String,Object> foodh = new HashMap<String,Object>();
		foodh.put("num", food.getId());
		foodh.put("foodName", food.getFoodName());
		foodh.put("foodClassification", food.getFoodClassification());
		foodh.put("describe", food.getDescribe());
		data.add(foodh);
		
		
		//一个简单的适配器,!!!!!这里就是将刚刚建的 list_item导进来,表示那个是用在这里的listView2_search中的!!!!。
		SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.list_item, new String[]{"foodName","foodClassification"}, new int[]{R.id.name,R.id.classification});
		ListView listview = (ListView) findViewById(R.id.listView2_search);
		listview.setAdapter(adapter);
		
		
		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				HashMap<String,Object> itemMap = (HashMap<String, Object>) adapterView.getItemAtPosition(position);
				//int num = (Integer) itemMap.get("id");
				String foodName = (String) itemMap.get("foodName");
				String classification = (String) itemMap.get("foodClassification");
				String describe = (String) itemMap.get("describe");
				
				DBFood food = new DBFood();
				//food.setId(num);
				food.setFoodName(foodName);
				food.setFoodClassification(classification);
				food.setDescribe(describe);
				
				
				Intent intent = new Intent();
				intent.putExtra("food", food);
				intent.setClass(SearchShowActivity.this, LookupMenuActivity.class);  //跳转到显示的页面
				SearchShowActivity.this.startActivity(intent);
			}
			
		});
	}
}

3.3 LookUpMenuActivity.java用来实现显示具体信息

package com.androidhomework.caipu;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class LookupMenuActivity extends Activity{
	
	
	TextView textViewN;
	TextView textViewC;
	TextView textViewD;

	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lookupmenu_activity);
		textViewN = (TextView) findViewById(R.id.show_name_t);       //xml里定义了show_name_t、show_classification_t、show_describe_t用来显示结果
		textViewC = (TextView) findViewById(R.id.show_classification_t);
		textViewD = (TextView) findViewById(R.id.show_describe_t);
		Intent intent = this.getIntent();
		DBFood food = (DBFood) intent.getSerializableExtra("food");  //将SearchShowActivity.java中打包传过来的food接受。
		
		textViewN.setText(food.getFoodName());      //将food传来的名字、类型、描述与显示结果的textview绑定,显示在上面。
		textViewC.setText(food.getFoodClassification());
		textViewD.setText(food.getDescribe());
	}
	
	
	
	
	
	//下面的代码用来实现删除功能,建立了optionmenu,在手机右上角或者右下角的下拉列表(竖着的三个点)中显示
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		menu.add(0, 1, 1, "删除该菜品");      //表示只有一项,删除该项菜品
		//menu.add(0,2,2,"返回");					
		return super.onCreateOptionsMenu(menu);
	}
	
	
	
	//对于删除功能的实现
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		DBFoodService foods = new DBFoodService(this);
		Intent intent = new Intent();
		switch(item.getItemId()){
		case 1:
			//Toast.makeText(MainActivity.this, "删除菜品", Toast.LENGTH_SHORT).show();
			foods.delete(textViewN.getText().toString());            //调用删除将其删除                           
			intent.setClass(LookupMenuActivity.this, MyMenuActivity.class);     //删除后跳转到MyMenuActivity.class
			LookupMenuActivity.this.startActivity(intent);
			finish();
			break;
		/*case 2:
        	
        	//Toast.makeText(MainActivity.this, "清空菜谱", Toast.LENGTH_SHORT).show();
        	intent.setClass(ShowInfoActivity.this, MainActivity.class);
        	ShowInfoActivity.this.startActivity(intent);
			finish();
			break;		*/	
		}
		
		return super.onOptionsItemSelected(item);
	}
	
	
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值