繁体   English   中英

如何在列表视图中传递所选行的ID并以另一种方式获得行的值,值存储在sqlite数据库中

[英]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

我有一个使用SQlite数据库填充的列表视图。 如果单击列表视图的行,我想将相应行的值传递给第二个活动,并在第二个活动的编辑文本中显示它。 我使用Listadapter,hashmap从数据库填充列表。 我正在使用捆绑包将值从一个活动传递到另一个活动。

我遇到的主要问题是如何在第二堂课中检索它。 我尝试了各种方法。 我已经使用简单的vursor适配器得到了答案,因为它在android中已弃用,因此我想找到一个替代解决方案,以在另一个活动中传递listview数据。

我的模特班:

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

尝试这种方式,希望这将帮助您解决问题。

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

如何在第二节课中获得意图价值

字符串ID = getIntent()。getStringExtra(“ ID”);

注意:请在OnCreateView()外部声明此“标签”变量,然后可以在项目单击侦听器的列表中访问。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM