【问题标题】:Android ListView with onClick items带有 onClick 项目的 Android ListView
【发布时间】:2014-02-13 05:49:41
【问题描述】:

我是一名新程序员,也是 Android 新手。我正在使用这个示例http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/,效果很好。

现在我想让项目(Dell、Samsung Galaxy S3 等)调用一个函数来打开一个具有不同信息的新活动。

例如:

如果我触摸 Dell,则必须显示一个新活动,向我显示有关 Dell 的信息。如果我碰三星,也是一样。

我在 Google 上搜索过,但找不到任何有用的信息,有什么提示吗?我认为这是基本的,但我是新手,所以我真的不知道从哪里开始

【问题讨论】:

    标签: android listview methods android-activity elements


    【解决方案1】:

    在您定义列表视图的活动中

    你写

    listview.setOnItemClickListener(new OnItemClickListener(){   
        @Override
        public void onItemClick(AdapterView<?>adapter,View v, int position){
            ItemClicked item = adapter.getItemAtPosition(position);
    
            Intent intent = new Intent(Activity.this,destinationActivity.class);
            //based on item add info to intent
            startActivity(intent);
        }
    });
    

    在您编写的适配器的 getItem 中

    public ItemClicked getItem(int position){
        return items.get(position);
    }
    

    【讨论】:

      【解决方案2】:
      lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      
              Intent i = new Intent(getActivity(), DiscussAddValu.class);
              startActivity(i);
          }
      });
      

      【讨论】:

      • 这个答案出现在低质量审查队列中,大概是因为您没有提供任何代码解释。如果此代码回答了问题,请考虑在答案中添加一些解释代码的文本。这样一来,您就更有可能获得更多支持,并帮助提问者学习新知识。
      【解决方案3】:

      你带着意图开始新的活动。向意图发送数据的一种方法是传递一个在意图中实现 parcelable 的类。请注意,您正在传递课程的副本。

      http://developer.android.com/reference/android/os/Parcelable.html

      这里我有一个 onItemClick。我创建意图并将整个类放入意图中。我发送的课程已经实现了 parcelable。提示:您只需要在重新创建类所需的最低限度上实现可解析。即,可能是一个文件名或一些简单的东西,比如构造函数可以用来创建类的字符串。新 Activity 稍后可以 getExtras,它本质上是使用其构造方法创建类的副本。

      在这里,当我在列表视图中收到 onclick 时,我会启动 my app 的 kmlreader 类。

      注意:下面的摘要是我正在传递的类的列表,因此 get(position) 实际上返回该类,它与填充列表视图的列表相同

      List<KmlSummary> summary = null;
      ...
      
      public final static String EXTRA_KMLSUMMARY = "com.gosylvester.bestrides.util.KmlSummary";
      
      ...
      
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position,
              long id) {
          lastshownitem = position;
          Intent intent = new Intent(context, KmlReader.class);
          intent.putExtra(ImageTextListViewActivity.EXTRA_KMLSUMMARY,
                  summary.get(position));
          startActivity(intent);
      }
      

      稍后在新活动中,我使用

      拉出可解析类
      kmlSummary = intent.getExtras().getParcelable(
                      ImageTextListViewActivity.EXTRA_KMLSUMMARY);
      
      //note:
      //KmlSummary implements parcelable.
      //there is a constructor method for parcel in
      // and a overridden writetoparcel method
      // these are really easy to setup.
      
      public KmlSummary(Parcel in) {
          this._id = in.readInt();
          this._description = in.readString();
          this._name = in.readString();
          this.set_bounds(in.readDouble(), in.readDouble(), in.readDouble(),
          in.readDouble());
          this._resrawid = in.readInt();
          this._resdrawableid = in.readInt();
           this._pathstring = in.readString();
          String s = in.readString();
          this.set_isThumbCreated(Boolean.parseBoolean(s));
      }
      
      @Override
      public void writeToParcel(Parcel arg0, int arg1) {
          arg0.writeInt(this._id);
          arg0.writeString(this._description);
          arg0.writeString(this._name);
          arg0.writeDouble(this.get_bounds().southwest.latitude);
          arg0.writeDouble(this.get_bounds().southwest.longitude);
          arg0.writeDouble(this.get_bounds().northeast.latitude);
          arg0.writeDouble(this.get_bounds().northeast.longitude);
          arg0.writeInt(this._resrawid);
          arg0.writeInt(this._resdrawableid);
          arg0.writeString(this.get_pathstring());
          String s = Boolean.toString(this.isThumbCreated());
          arg0.writeString(s);
      }
      

      祝你好运 丹尼117

      【讨论】:

        【解决方案4】:

        您绝对应该扩展您的 ArrayListAdapter 并在您的 getView() 方法中实现它。如果第二个参数(View)的值为null,则应该对其进行膨胀,利用它并​​在膨胀后将其设置为onClickListener()

        假设它被称为你的第二个getView()的参数被称为convertView

        convertView.setOnClickListener(new View.OnClickListener() {
          public void onClick(final View v) {
            if (isSamsung) {
              final Intent intent = new Intent(this, SamsungInfo.class);
              startActivity(intent);
            }
            else if (...) {
              ...
            }
          }
        }
        

        如果你想了解如何扩展ArrayListAdapter,我推荐这个link

        【讨论】:

          【解决方案5】:

          在您的 onitemClick 中,您将发送选定的值,如 deal ,并在打开新活动时以您的意图发送,并在您的新活动中获取发送的数据,并且与所选项目相关的将显示您的数据

          list获取名称

          String item = yourData.get(position).getName(); 
          

          在意图中设置数据

          intent.putExtra("Key", item);
          

          在第二个活动中获取数据

          getIntent().getExtras().getString("Key")
          

          【讨论】:

            【解决方案6】:

            什么样的地狱实现 Parcelable ?

            他正在传递给适配器 String[] 所以

            • 在位置获取项目(字符串)
            • 创建意图
            • 把它当作额外的
            • 开始活动
            • 在活动中获得额外奖励

            要存储产品列表,您可以在此处使用 HashMap (例如作为 STATIC 对象)

            描述产品的示例类:

            public class Product {
                private String _name;
                private String _description;
                private int _id
            
                public Product(String name, String description,int id) {
                    _name = name;
                    _desctription = description;
                    _id = id;
                }
            
                public String getName() {
                    return _name;
                }
            
                public String getDescription() {
                    return _description;
                }
            }
            
            Product dell = new Product("dell","this is dell",1);
            
            HashMap<String,Product> _hashMap = new HashMap<>();
            _hashMap.put(dell.getName(),dell);
            

            然后你传递给适配器键集:

            String[] productNames =  _hashMap.keySet().toArray(new String[_hashMap.size()]);
            

            当您在适配器中返回视图时,您可以像这样设置侦听器:

             @Override
             public View getView(int position, View convertView, ViewGroup parent) {
            
                 Context context = parent.getContext(); 
            
                 String itemName = getItem(position)
            
                 someView.setOnClikListener(new MyOnClickListener(context, itemName));
            
             }
            
            
             private class MyOnClickListener implements View.OnClickListener { 
            
                 private  String _itemName; 
                 private  Context _context
            
                 public MyOnClickListener(Context context, String itemName) {
                     _context = context;
                     _itemName = itemName; 
                 }
            
                 @Override 
                 public void onClick(View view) {
                     //------listener onClick example method body ------
                     Intent intent = new Intent(_context, SomeClassToHandleData.class);
                     intent.putExtra(key_to_product_name,_itemName);
                     _context.startActivity(intent);
                 }
             }
            

            然后在其他活动中:

            @Override 
            public void onCreate(Bundle) {
            
                String productName = getIntent().getExtra(key_to_product_name);
                Product product = _hashMap.get(productName);
            
            }
            

            *key_to_product_name 是一个公共静态字符串,用作额外的密钥

            ps。抱歉打错了,我很着急:) ps2。这应该让你知道怎么做 ps3。当我有更多时间时,我会添加详细说明

            我的评论:

            • 请勿使用任何开关声明
            • 不要为每个产品创建单独的活动(你只需要一个)

            【讨论】:

              【解决方案7】:
              listview.setOnItemClickListener(new OnItemClickListener(){
              
                  @Override
                  public void onItemClick(AdapterView<?>adapter,View v, int position){
                  Intent intent;
                  switch(position){
                    case 0:
                      intent = new Intent(Activity.this,firstActivity.class);
                      break;
                    case 1:
                      intent = new Intent(Activity.this,secondActivity.class);
                      break;
                   case 2:
                      intent = new Intent(Activity.this,thirdActivity.class);
                      break;
                  //add more if you have more items in listview
                 //0 is the first item 1 second and so on...
                  }
                  startActivity(intent);
                }
              
              });
              

              【讨论】:

                【解决方案8】:

                我可以通过将上下文引用从 thisContext.this 替换为 getapplicationcontext 来解决整个问题。

                【讨论】:

                  【解决方案9】:
                  listview.setOnItemClickListener(new OnItemClickListener(){
                  
                  //setting onclick to items in the listview.
                  
                  @Override
                  public void onItemClick(AdapterView<?>adapter,View v, int position){
                  Intent intent;
                  switch(position){
                  
                  // case 0 is the first item in the listView.
                  
                    case 0:
                      intent = new Intent(Activity.this,firstActivity.class);
                      break;
                  //case 1 is the second item in the listView.
                  
                    case 1:
                      intent = new Intent(Activity.this,secondActivity.class);
                      break;
                   case 2:
                      intent = new Intent(Activity.this,thirdActivity.class);
                      break;
                  //add more if you have more items in listView
                  startActivity(intent);
                  }
                  
                  });
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2015-02-05
                    • 2018-03-23
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2015-01-19
                    相关资源
                    最近更新 更多