我们已经实现过了listview的一种item的点击事件,开启一个新的活动,并且传递一些数据到新的活动中,现在我们要在新的活动中设置回退按钮。接着我们需要定义实现另一个item的点击事件。

        不得不说今天这篇文章本来下午预计吃晚饭之前就能发的,结果一个运行时异常活生生耗掉了我三顿晚饭的时间,不过最后总能解决,安卓真的是一个不断遇到问题,解决问题的过程(这个问题既指思路,实现又指异常)。遇到的是空指针异常,确定好是哪几个文件之后,最后是一个一个文件的确定好所有的id,变量名,终于运行正常。所以千万注意好所有的id,不要重名,不要不匹配。

       好了回退没啥好上图的,就上点击事件的。

android 回退fragment_回退到上一个活动

点击之后:

android 回退fragment_回退到上一个活动_02


先上回退的代码(finish()就可以!)

ImageView iv =(ImageView)findViewById(R.id.backmain);
		iv.setOnClickListener(new View.OnClickListener()
        {
        	@Override
        	public void onClick(View v)
        	{
        		finish();
        	}
        });



那么,现在,我们面临的问题是,如何区分是哪一个点击的item呢?我们就会思考,它们有什么不同,很显然是类型不同,一个是Day类型,一个是Point类型。但是它们都装在object类型的list中,这时候就需要用到我们的instanceof操作符了,然后if-else分开处理即可(当然要为跳转的活动建立活动类,布局文件,注册),还是用intent包裹要传递的数据。

主活动:

package com.example.littled;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;


public class MainActivity extends Activity {
	
	private String monthmenu;
	private String yearmenu;
	
	private ArrayList<Object> daylist = new ArrayList<Object>();
	private ArrayList<Object> pointlist = new ArrayList<Object>();
	private ArrayList<Object> data = new ArrayList<Object>();
	
	TextView tv1;
	TextView tv2;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        tv1=(TextView)findViewById(R.id.text_view1);
        monthmenu = "August";
		tv1.setText(monthmenu);
        
		 tv2=(TextView)findViewById(R.id.text_view2);
		 yearmenu = "2015";
		 tv2.setText(yearmenu);

        tv1.setOnClickListener(new View.OnClickListener()
        {
        	@Override
        	public void onClick(View v)
        	{
        		showPopupMenu1(tv1);
        	}
        });
        
        tv2.setOnClickListener(new View.OnClickListener()
        {
        	@Override
        	public void onClick(View v)
        	{
        		showPopupMenu2(tv2);
        	}
        });
		
        initDays();
        DayApater adapter = new DayApater(MainActivity.this,data);
        ListView listView = (ListView)findViewById(R.id.list_view);
        listView.setAdapter(adapter);
        
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
            	Object ob = data.get(position);
            	if(ob instanceof Day)
            	{
	                Day d = (Day)ob;
	                String sdetail = d.getDetail();
	                String sday = d.getDay();
	                String sweek = d.getWeek();
	                Intent intent = new Intent(MainActivity.this, ShowActivity.class);
	                intent.putExtra("detail", sdetail);
	                intent.putExtra("week",sweek);
	                intent.putExtra("day", sday);
	                intent.putExtra("year",yearmenu);
	                intent.putExtra("month", monthmenu);
	                startActivity(intent);
            	}
            	else if(ob instanceof Point)
            	{
            		Point p = (Point)ob;
	                String sday = p.getDay();
	                String sweek = p.getWeek();
	                Intent intent = new Intent(MainActivity.this, EditActivity.class);
	                intent.putExtra("week2",sweek);
	                intent.putExtra("day2", sday);
	                intent.putExtra("year2",yearmenu);
	                intent.putExtra("month2", monthmenu);
	                startActivity(intent);
            	}
            	
            }
        });
    }
        
        private void initDays()
        {
        	Day day1 = new Day("SAT","1","我去超市来着...");
        	data.add(day1);
        	Day day2 = new Day("SUN","2","今天第一次和智恩吃意大利面。");
        	data.add(day2);
        	Day day3 = new Day("MON","3","路两边的树吸走了汽车尾气");
        	data.add(day3);
        	Day day4 = new Day("TUE","4","现在每天都能吃到猪排饭了,好开心啊");
        	data.add(day4);
        	Point point = new Point(R.drawable.blackpoint,"WED","4");
        	//Point point = new Point(R.drawable.blackpoint);
        	data.add(point);
        	Point point2 = new Point(R.drawable.blackpoint,"THR","5");
        	//Point point2 = new Point(R.drawable.blackpoint);
        	data.add(point2);
        	Day day5 = new Day("FRI","7","你,黑咖啡,芝士年糕,羽毛球,成功");
        	data.add(day5);
        	Day day6 = new Day("SAT","8","DGA 7 最爱");
        	data.add(day6);
        }
        private void showPopupMenu1(View view) {
        	 PopupMenu popupMenu = new PopupMenu(this, view);
        	 popupMenu.getMenuInflater().inflate(R.menu.text1, popupMenu.getMenu());
        	 popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        	  @Override
        	  public boolean onMenuItemClick(MenuItem item) {
        		  monthmenu = (String) item.getTitle();
        		  tv1.setText(monthmenu);
        	  return false;
        	  }
        	 });
        	 popupMenu.show();
        }
        private void showPopupMenu2(View view) {
       	 PopupMenu popupMenu = new PopupMenu(this, view);
       	 popupMenu.getMenuInflater().inflate(R.menu.text2, popupMenu.getMenu());
       	 popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
       	  @Override
       	  public boolean onMenuItemClick(MenuItem item) {
       		  yearmenu = (String) item.getTitle();
       		  tv2.setText(yearmenu);
       	  return false;
       	  }
       	 });
       	 popupMenu.show();
       }
}

跳转的目的活动:

package com.example.littled;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class EditActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit);
		Intent intent = getIntent();
		
		String w = intent.getStringExtra("week2");
		
		if(w == "MON")
			w = "MONDAY";
		else if(w == "TUE")
			w = "TUESDAY";
		else if(w == "WED")
			w = "WEDNESDAY";
		else if(w == "THR")
			w = "THRUTHDAY";
		else if(w == "FRI")
			w = "FRIDAY";
		else if(w == "SAT")
			w = "SATURDAY";
		else if(w == "SUN")
			w = "SUNDAY";
			
			
		TextView tv1 = (TextView)findViewById(R.id.show_week2);
		tv1.setText(w);
	
		String m = intent.getStringExtra("month2");
		TextView tv2 = (TextView)findViewById(R.id.show_month2);
		tv2.setText(m);
		
		String da = intent.getStringExtra("day2");
		TextView tv3 = (TextView)findViewById(R.id.show_day2);
		tv3.setText(da);
	
		String y = intent.getStringExtra("year2");		
		TextView tv4 = (TextView)findViewById(R.id.show_year2);
		tv4.setText(y);
		
		ImageView iv =(ImageView)findViewById(R.id.backmain2);
		iv.setOnClickListener(new View.OnClickListener()
        {
        	@Override
        	public void onClick(View v)
        	{
        		finish();
        	}
        });
	}
}



跳转的目的活动的界面(未完善)

<?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" >
    
   <LinearLayout
      		android:layout_marginTop="12dip"
            android:id="@+id/showup2"
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:orientation="horizontal" >
   
        <View
            android:layout_width="0dp"
            android:layout_weight="2.5"
            android:layout_height="0dp"
            />
    	     
        <TextView 
       	    android:id="@+id/show_week2"
            android:layout_height="20dip"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:textStyle="bold"
            />
        
        <ImageView
                android:layout_height="15dip"
                android:layout_width="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/divi2"
                />
        
        <TextView 
       	    android:id="@+id/show_month2"
            android:layout_height="20dip"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:textStyle="bold"
            />
        <View
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="0dp"
            />
        
        <TextView 
       	    android:id="@+id/show_day2"
            android:layout_height="20dip"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:textStyle="bold"
            />
        
        
         <ImageView
                android:layout_height="15dip"
                android:layout_width="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/divi2"
                />
         
          <TextView 
       	    android:id="@+id/show_year2"
            android:layout_height="20dip"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:textStyle="bold"
            />
        
        <View
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="0dp"
            />
       
   </LinearLayout>
   
    <ImageView
        android:layout_height="5dip"
       	android:gravity="center"
        android:layout_width="wrap_content"
        android:src="@drawable/longlong"
        />

	
	<ImageView
	    android:id="@+id/backmain2"
        android:layout_height="5dip"
        android:layout_marginLeft="150dip"
       	android:gravity="center"
        android:layout_width="wrap_content"
        android:src="@drawable/backgray"
        android:clickable="true"
        />
    
</LinearLayout>



另外就是,Point是要增加week,day等属性的,因为新的活动需要这些数据,并不是在主界面中不显示就不保有了,当年还是太天真。。。