Senthilvel S
Senthilvel S

Reputation: 430

how to Pass id of the selected row in a listview and get the values of row in another acticity,values are stored in sqlite database

I have a listview which is populated using the SQlite database. If I click the row of the listview I want to pass the values of the corresponding row to second activity and I want to display it in the edit text in the second activity. I used Listadapter, hashmap to populate list from the database. I am using a bundle to pass the values from one activity to another.

The main problem I am having is how to retrieve it in the second class. I have tried various methods. I already got the answer using the simple vursor adapter ,as it is deprecated in android so I want to find an alternate solution to pass the listview data in another activity.

My model class :

public class Customer1 {
     String name,id,price,type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getNo() {
        return name;
    }

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

myadapter class:

public class FragmentMod extends Fragment {
Fragment fe= new FragmentOne();
Customer1 c= new Customer1();
HashMap<String, String> map = new HashMap<String, String>();

    public FragmentMod()
    {

    }

    @Override
       public View onCreateView(LayoutInflater inflater,
                  ViewGroup container, Bundle savedInstanceState)
                {
                   //Inflate the layout for this fragment
               View view =inflater.inflate(R.layout.fragment_mod, container, false);
               setHasOptionsMenu(true);
               ListView list =(ListView)view.findViewById(R.id.lvv2);
               ArrayList<HashMap<String, String>> Items = new ArrayList<HashMap<String, String>>();
                 DatabaseHandler1 db= new DatabaseHandler1(getActivity().getBaseContext());
                List<Customer1> labels = db.getAllDatas();       

                for (Customer1
                        val : labels) {

                        // Writing values to map
                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put("name", val.getNo());
                    map.put("price",val.getPrice());
                    map.put("type", val.getType());

                    Items.add(map);        
                }

             // Adding Items to ListView
             final ListAdapter adapter = new SimpleAdapter(getActivity(), Items,
                        R.layout.single_nodif,new  String[]{ "name","price","type" },
                        new int[] {R.id.txt_1,R.id.txt_2,R.id.txt_3});
                   list.setAdapter(adapter);}}

Upvotes: 1

Views: 1344

Answers (1)

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

Try this way,hope this will help you to solve your problem.

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
     Intent intent = new Intent(getActivity(),YourSecond.class);
     intent.putExtra("ID",labels.get(position).getId());
     startActivity(intent);
}
});

How to get intent value in second class

String id = getIntent().getStringExtra("ID");

Note : please declare this "labels" variable as out side OnCreateView() then you can access in list on item click listener.

Upvotes: 1

Related Questions