为什么listView中的名称不断变化?

时间:2015-11-18 08:29:42

标签: java android listview android-listview

我在活动A中有一个listView,如下所示。实际上,所有值和名称都从活动C返回到B,然后只返回A.

enter image description here

点击第一个列表时,它应在Project B上显示文字3和值editText。但它显示的是Medical,它实际上来自上一个列表。

enter image description here

将值从3更改为43并返回到A后,名称已更改。我应该怎么做才能使名字保持不变?

enter image description here

活动A

      ArrayAdapter<String> adapter;
      ArrayList<String> m_listItems = new ArrayList<String>();
      int mClickedPosition;
      adapter=new ArrayAdapter<String (getActivity(),R.layout.claims,R.id.textView1,m_listItems);
     listV = (ListView) claims.findViewById(R.id.listView1);

               listV.setOnItemClickListener(new AdapterView.OnItemClickListener() {  // if list clicked
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                mClickedPosition = position;


                if (name.equals("Project")) {
                    String temp[] = m_listItems.get(position).split("\\s\\s+");
                    result = temp[temp.length - 1].trim();
                    result = result.replace("RM","");
                    Intent intent = new Intent(Claims1.this.getActivity(), Project1.class);
                    intent.putExtra("bitmap", true); // image
                    intent.putExtra("name", name);
                    intent.putExtra("result", result);
                    startActivityForResult(intent, 0);
                    Log.e("RESULT", "Result= " + result);
                }
                else if(name.equals("Medical"))
                {
                    String temp[] = m_listItems.get(position).split("\\s\\s+");
                    result = temp[temp.length - 1].trim();
                    result = result.replace("RM","");
                    Intent intent = new Intent(Claims1.this.getActivity(), Medical.class);
                    intent.putExtra("bitmap", true);
                    intent.putExtra("name", name);
                    intent.putExtra("result", result);
                    startActivityForResult(intent, 1);
                }
            }
        });
        return claims;
       }


   @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 0:  // for Project
                result = data.getStringExtra("text"); //get from B
                name = data.getStringExtra("a");
                description = data.getStringExtra("c");
                as = Long.parseLong(result);
                Log.d("FIRST", "result:" + result);
                Text = "  " + name + "                                  " + "RM" + result + "";
                if (mClickedPosition == -1) { // if is icon button clicked
                    m_listItems.add(Text);

                } else {
                    m_listItems.set(mClickedPosition, Text);

                }
                adapter.notifyDataSetChanged();
                listV.setAdapter(adapter);
                break;

            case 1:  // for Medical
                result = data.getStringExtra("text");
                name = data.getStringExtra("a");
                description = data.getStringExtra("c");
                as = Long.parseLong(result);
                Log.d("FIRST", "result:" + result);
                Text = "  " + name + "                                  " + "RM" + result + "";
                // m_listItems.clear();
                if (mClickedPosition==-1)
                {
                    m_listItems.add(Text);

                }
                else
                {
                    m_listItems.set(mClickedPosition, Text);

                }
                adapter.notifyDataSetChanged();
                listV.setAdapter(adapter);
                break;

活动B (Project1)

   if(getIntent().getExtras()!=null) { //if  has value pass from A
            final String Amount = getIntent().getExtras().getString("result");
            final String description1 = getIntent().getExtras().getString("description");
            txt1.setText(description1);
            txt.setText(Amount);
        }

                b.setOnClickListener(new View.OnClickListener() {  // return to A
                    public void onClick(View arg0) {
                        Intent returnIntent = new Intent();
                        a = "Project";
                        text = txt.getText().toString(); // amount
                        returnIntent.putExtra("text", text);
                        returnIntent.putExtra("a", a);
                        returnIntent.putExtra("c", c); // receive from Activity C
                        setResult(Activity.RESULT_OK, returnIntent);
                       finish();
                    }
                });
                       viewImage.setImageBitmap(Global.img); // receive from C
                  }

注意到活动A中的result代表值3,5,而name代表projectMedical 。我怎样才能解决这个问题 ?请帮忙。

1 个答案:

答案 0 :(得分:0)

根据我的理解,活动A中的变量name包含以前产生的值(来自活动B),而不是来自您点击列表视图的行,即您之前从活动B获得的“医疗”值变量name以及您的listview点击事件,您检查了name是“医疗”还是“项目”(在这种情况下,它是“医疗”,因此开始医疗活动)。

一种解决方案是从listview获取值(“Project / Medical”)。

试试这段代码。 活动A

  mClickedPosition = position;
  String temp[] = m_listItems.get(position).split("\\s\\s+");
  result = temp[temp.length - 1].trim();
  result = result.replace("RM","");
  name = temp[1].trim();
  if (name.equals("Project")) {


                Intent intent = new Intent(Claims1.this.getActivity(), Project1.class);
                intent.putExtra("bitmap", true);
                intent.putExtra("name", name);
                intent.putExtra("result", result);
                startActivityForResult(intent, 0);
                Log.e("RESULT", "Result= " + result);
            }
            else if(name.equals("Medical"))
            {
                Intent intent = new Intent(Claims1.this.getActivity(), Medical.class);
                intent.putExtra("bitmap", true);
                intent.putExtra("name", name);
                intent.putExtra("result", result);
                startActivityForResult(intent, 1);
            }