将ListView值传递给EditText

时间:2015-11-17 04:39:10

标签: android listview android-listview

我在活动A中有一个listView,如下所示。

enter image description here

点击第一个列表后,它应在3上显示editText。但它显示5实际上从最后一个列表中获取。

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() { // when list is pressed, intent to B
            public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
                            mClickedPosition = position;
                            if (name.equals("Project")) {
                                Intent intent = new Intent(Claims1.this.getActivity(), B.class);
                                intent.putExtra("bitmap", true);
                                intent.putExtra("name", name);
                                intent.putExtra("result", result);
                                startActivityForResult(intent, 0);
                            }


            @Override
            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                switch (requestCode) { // receive from B
                    case 0:
                        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

   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
                  }

1 个答案:

答案 0 :(得分:2)

使用ArrayList提取ListView位置的字符串,如..

listV.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
        public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
           mClickedPosition = position;
           String result = m_listItems.get(position);        //add this
           result = result.substring(2);
           if (name.equals("Project")) {
               Intent intent = new Intent(Claims1.this.getActivity(), B.class);
               intent.putExtra("result", result);     // your intent stuff
           }
         }
});