在第二个页面修改第一个页面listView条目的数据

下面是代码
布局文件
第一个页面的布局
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    
    <ListView 
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></ListView>

</RelativeLayout>
//第二个页面的布局
<pre name="code" class="html"><?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" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp" />

    <EditText
        android:id="@+id/et"
        android:layout_width="200dp"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确认修改"
        android:textSize="24sp" />

</LinearLayout>
ListView条目的布局
<?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" >


    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="heheh"
        android:textSize="18sp" />


    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="heheh"
        android:textSize="18sp" />


</LinearLayout>
第一个页面
<pre name="code" class="java">package us.mifeng.jihe.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

import us.mifeng.jihe.R;
import us.mifeng.jihe.activity.adapter.MyAdapter;
import us.mifeng.jihe.bean.Students;
import android.R.integer;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {

    private ListView lv;
    private ArrayList<Students> aList=new ArrayList<Students>();
    private HashMap<Integer, Students> hMap=new HashMap<Integer, Students>();
	private Students Student ;
	private MyAdapter myAdapter;

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        iniData();
        myAdapter = new MyAdapter(aList, MainActivity.this);
        lv.setAdapter(myAdapter);
        initEvent();
    }

	private void initEvent() {
		
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				Intent intent = new Intent();
//				Bundle bundle = new Bundle();
//				bundle.putString("name", aList.get(position).getName());
//				bundle.putInt("id", aList.get(position).getId());
//				bundle.putString("notes", aList.get(position).getNotes());
//				intent.putExtras(bundle);
				intent.putExtra("name", aList.get(position).getName());
				intent.putExtra("id", aList.get(position).getId());
				intent.putExtra("notes", aList.get(position).getNotes());
				intent.setClass(MainActivity.this, SecondActivity.class);
				startActivityForResult(intent, 1);
				
				
				
			}
		});
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if(data!=null){
			if(resultCode==5){
				String notes = data.getStringExtra("notes");
				int id = data.getIntExtra("id", 0);
				if(hMap.containsKey(id)){
					hMap.get(id).setNotes(notes);
				}else {
					Students student1 = new Students();
					student1.setNotes(notes);
					student1.setId(id);
					hMap.put(id, student1);
					
				}
				
				Set<Entry<Integer, Students>> entrySet = hMap.entrySet();
				for (Entry<Integer, Students> entry : entrySet) {
					System.out.println(entry);
				}
				
				for (Students st : aList) {
					int id2 = st.getId();
					if(hMap.containsKey(id2)){
						String notes1 = hMap.get(id2).getNotes();
						aList.get(id2).setNotes(notes1);
						
					}
					
				}
				myAdapter.notifyDataSetChanged();
				System.out.println(aList);
				
			}
			
			
			
		}
		
		
	}

	private void iniData() {
		for(int i=0;i<20;i++){
			Students ss = new Students();
			ss.setId(i);
			ss.setName("我是"+i+"pppppp");
			System.out.println(ss.getName());
			ss.setNotes("哦哦哦"+i);
			System.out.println(ss.getNotes());
			aList.add(ss);
		}
		System.out.println("kkkkkkkkkkk"+aList.size());
		System.out.println("kllllllllllllllllll"+aList);
		
	}

	private void initView() {
		
		lv = (ListView) findViewById(R.id.lv);
		
	}


}

 
</pre><pre name="code" class="html">适配器
<pre name="code" class="java">package us.mifeng.jihe.activity.adapter;

import java.util.ArrayList;

import us.mifeng.jihe.R;
import us.mifeng.jihe.bean.Students;

import android.R.integer;
import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter{
	 ArrayList<Students> maList;
	 Context mContext;
	 LayoutInflater mInflater; 


	public MyAdapter(ArrayList<Students> aList,Context context){
		maList=aList;
		mContext=context;
		mInflater=LayoutInflater.from(context);

	}


	@Override
	public int getCount() {
		return maList.size();
	}

	@Override
	public Object getItem(int position) {
		return maList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh;
		if(convertView==null){
			convertView=mInflater.inflate(R.layout.item_lv, parent, false);
			vh = new ViewHolder(convertView, R.id.tv1, R.id.tv2);
			convertView.setTag(vh);
		}

		vh=(ViewHolder) convertView.getTag();
		Students item = (Students) getItem(position);
		vh.tv1.setText(item.getName()+item.getId());
		vh.tv2.setText(item.getNotes());

		return convertView;
	}

	public class ViewHolder{
		TextView tv1,tv2;
		public ViewHolder(View v,int id1 ,int id2){
			tv1=(TextView) v.findViewById(id1);
			tv2=(TextView) v.findViewById(id2);
		}
	}

}

 
</pre><pre name="code" class="html">bean类
<pre name="code" class="java">package us.mifeng.jihe.bean;

public class Students {
	
	String name,notes;
	int id;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getNotes() {
		return notes;
	}
	public void setNotes(String notes) {
		this.notes = notes;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	@Override
	public String toString() {
		return "Student[name="+name+",notes="+notes+",id"+id+"]";
	}

}

 
</pre><pre name="code" class="html">第二个页面
<pre name="code" class="java">package us.mifeng.jihe.activity;

import us.mifeng.jihe.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SecondActivity extends Activity{
	
	private TextView tv;
	private EditText et;
	private Button bt;
	private int id;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		initView();
		
		Intent intent = getIntent();
		String name = intent.getStringExtra("name");
		String notes = intent.getStringExtra("notes");
		id = intent.getIntExtra("id",0);
		
//		Bundle bundle = intent.getExtras();
//		String name = bundle.getString("name");
//		String id = bundle.getString("id");
//		String notes = bundle.getString("notes");
		System.out.println(name+notes);
		tv.setText(name+notes);
		initEvent();
		
	}

	private void initEvent() {
		
		bt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String trim = et.getText().toString().trim();
				if(!TextUtils.isEmpty(trim)){
					Intent intent = new Intent();
					intent.putExtra("notes", trim);
					intent.putExtra("id", id);
					System.out.println(id+"rrrrrrrrrrrr"+ trim);
					setResult(5, intent);
					finish();
				}
				
				
				
			}
		});
		
	}

	private void initView() {
		tv = (TextView) findViewById(R.id.tv);
		et = (EditText) findViewById(R.id.et);
		bt = (Button) findViewById(R.id.bt);
	}
	
	

}


 

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值