Don't change the name on the listView

拟墨画扇 提交于 2019-12-13 18:43:43

问题


I have a listView in Activity A as shown below. All the values and name were actually returned from Activity C to B then only A.

When the first list is clicked, it should display text Project and value 3 on editText B. But it displays Medical which was actually getting from the last list.

After I change the value from 3 to 43 and return to A, the name changed. What should I do so that the name will remain the same ?

Activity 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;

Activity 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
                  }

Noted that the result in A represents value 3,5 while name represents project and Medical


回答1:


following is your custom object which you want to populate in 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;
    }
}

layout for individual item how it would appear in ListView this is the file name my_single_item.xml below is the content for the layout

<?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>

This is the adapter which will populate the underlying list of MyItems inside listview by making individual item

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;
    }
}

write this code in your activity

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();
    }
});

make sure you do add a listview in this activities layout

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

you would see the toast will match the item you would click now use this object and pass it to any activity you want and it will have the values you clicked on



来源:https://stackoverflow.com/questions/33764162/dont-change-the-name-on-the-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!