查找

原创 2015年11月17日 16:06:26

想实现的是:第一个界面搜索(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);
	}
	
	
}


相关文章推荐

SQLServer数据库查找替换工具

  • 2017年11月15日 14:13
  • 124KB
  • 下载

C# LinqXML查找指定名称的元素

  • 2017年10月27日 10:35
  • 39KB
  • 下载

Tair LDB基于Prefixkey的范围查找性能优化项目测试及完成总结报告

项目这周就截止了,这算是我第一个有导师指导的真正意义上的C++项目,项目基本完成,想要实现的功能也已经实现,并做了大量的性能测试。不过这对于业界来说,可能完成的还不够成熟,还有许多待改进的地方,还不能...

PB9查找工具

  • 2017年10月25日 15:30
  • 943KB
  • 下载

查找阻塞进程sql

  • 2017年10月23日 22:45
  • 3KB
  • 下载

在UltraEdit的查找和替换中使用正则表达式

转:http://tech.ddvip.com/2007-10/119380968536861.html 文本编辑工作非常常用!   语法集合一:   符号 功能   %  匹配行的开始 -...
  • foart
  • foart
  • 2012年01月11日 12:03
  • 1233

查找每个词的编号

  • 2017年11月17日 09:33
  • 293KB
  • 下载

unity 不通过gameobject寻找物体 查找指定父物体的指定名称的子物体,指定组件,指定类型的组件

unity 不通过gameobject寻找物体 查找指定父物体的指定名称的子物体,指定组件,指定类型的组件...

DLL依赖查找工具

  • 2017年11月05日 23:15
  • 742KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:查找
举报原因:
原因补充:

(最多只允许输入30个字)