Chrisddddddd
2019-04-15 15:14
采纳率: 0%
浏览 284
已采纳

请问要怎么样通过新activity的返回值修改gridview中各个item的内容呢?

package com.example.hasee.myapplication;

import android.arch.core.internal.SafeIterableMap;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
GridView gridView;
private int clickTemp = -1;
//标识选择的Item
SimpleAdapter simpleAdapter;
List>list;
String add_Title="",add_Context="";
Mapmap;
Button button;
int count;
final String[] name=new String[]{"语文","语文","语文","语文","语文","语文","语文","语文","语文","语文","语文","语文","语文","语文","语文","语文","语文"};
final String[] location=new String[]{"六教","六教","六教","六教","六教","六教","六教","六教","六教","六教","六教","六教","六教","六教","六教","六教","六教"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    gridView=findViewById(R.id.My_gridview);
    list=new ArrayList<>();
    for(int i=0;i<name.length;i++)
    {
        map=new HashMap<>();
        map.put("001",name[i]);
        map.put("002",location[i]);
        list.add(map);
    }
    simpleAdapter=new SimpleAdapter(MainActivity.this,list,R.layout.gridview_item,new String[]{"001","002"},new int[]{R.id.textView,R.id.textView2});
    gridView.setAdapter(simpleAdapter);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            startActivityForResult(new Intent(MainActivity.this,Main2Activity.class),0x01);
            count=position;
        }

    });
}



@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 0x01 && resultCode == 0x07) {
        add_Title = data.getStringExtra("003");
        add_Context = data.getStringExtra("004");
        name[count]=add_Title;
        location[count]=add_Context;
        Toast.makeText(MainActivity.this,name[count],Toast.LENGTH_SHORT).show();
        simpleAdapter.notifyDataSetChanged();
    }
}

}

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题