不要在listView

时间:2015-11-17 18:14:48

标签: 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);
                    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代表项目和医疗

1 个答案:

答案 0 :(得分:0)

以下是您要在listview

中填充的自定义对象
MyItem.java

public class MyItem {

    private String name;

    private String something;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSomething() {
        return something;
    }

    public void setSomething(String something) {
        this.something = something;
    }
}

单个项目的布局如何在ListView中显示这是文件名my_single_item.xml 下面是布局的内容

<?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="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/text_first"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0.60"
        android:gravity="center"
        android:padding="4dp"
        android:textColor="#000"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/text_second"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0.60"
        android:gravity="center"
        android:padding="4dp"
        android:textColor="#000"
        android:textSize="18sp" />

</LinearLayout>

这是一个适配器,它将通过制作单个项目来填充MyItems listview内的基础列表

MyAdapter.java

import java.util.ArrayList;

/**
 * Created by Pankaj Nimgade on 18-11-2015.
 */
public class MyAdapter extends ArrayAdapter {

    private LayoutInflater layoutInflater;
    private ArrayList<MyItem> myItems;

    public MyAdapter(Context context, int resource, ArrayList<MyItem> myItems) {
        super(context, resource, myItems);
        layoutInflater = LayoutInflater.from(context);
        this.myItems = myItems;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.my_single_item, parent, false);
        TextView name = (TextView) view.findViewById(R.id.text_first);
        TextView something = (TextView) view.findViewById(R.id.text_second);
        MyItem myItem = myItems.get(position);
        name.setText("" + myItem.getName());
        something.setText("" + myItem.getSomething());
        return view;
    }
}

在您的活动中写下此代码

listView = (ListView) findViewById(R.id.myListView);
ArrayList<MyItem> myItems = new ArrayList<>();

for (int i = 0; i < 5; i++) {
    MyItem myItem = new MyItem();
    myItem.setName("Name_" + i);
    myItem.setSomething("RM_" + i);
    myItems.add(myItem);
}

MyAdapter myAdapter = new MyAdapter(this, android.R.layout.simple_list_item_1, myItems);
listView.setAdapter(myAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        MyItem myItem = (MyItem)listView.getItemAtPosition(position);
        Toast.makeText(getApplicationContext(), ""+myItem.getName(), Toast.LENGTH_SHORT).show();
    }
});

确保在此活动布局中添加列表视图

 <ListView
        android:id="@+id/myListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>

你会看到吐司会匹配你现在点击的项目使用这个对象并将其传递给你想要的任何活动,它将具有你点击的值