Android ListView with onClick items

前端 未结 9 1550
梦如初夏
梦如初夏 2020-11-27 03:33

I\'m a new programmer and new in Android. I\'m using this example http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/ and it works great.

相关标签:
9条回答
  • 2020-11-27 03:51
    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);
    }
    
    });
    
    0 讨论(0)
  • 2020-11-27 03:56

    for what kind of Hell implementing Parcelable ?

    he is passing to adapter String[] so

    • get item(String) at position
    • create intent
    • put it as extra
    • start activity
    • in activity get extra

    to store product list you can use here HashMap (for example as STATIC object)

    example class describing product:

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

    then u pass to adapter set of keys as:

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

    when in adapter u return view u set listener like this for example:

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

    then in other activity:

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

    *key_to_product_name is a public static String to serve as key for extra

    ps. sorry for typo i was in hurry :) ps2. this shoud give you a idea how to do it ps3. when i will have more time i I'll add a detailed description

    MY COMMENT:

    • DO NOT USE ANY SWITCH STATEMENT
    • DO NOT CREATE SEPARATE ACTIVITIES FOR EACH PRODUCT ( U NEED ONLY ONE)
    0 讨论(0)
  • 2020-11-27 03:57

    You should definitely extend you ArrayListAdapter and implement this in your getView() method. The second parameter (a View) should be inflated if it's value is null, take advantage of it and set it an onClickListener() just after inflating.

    Suposing it's called your second getView()'s parameter is called 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 (...) {
          ...
        }
      }
    }
    

    If you want some info on how to extend ArrayListAdapter, I recommend this link.

    0 讨论(0)
  • 2020-11-27 03:58

    I was able to go around the whole thing by replacing the context reference from this or Context.this to getapplicationcontext.

    0 讨论(0)
  • 2020-11-27 03:59

    You start new activities with intents. One method to send data to an intent is to pass a class that implements parcelable in the intent. Take note you are passing a copy of the class.

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

    Here I have an onItemClick. I create intent and putExtra an entire class into the intent. The class I'm sending has implemented parcelable. Tip: You only need implement the parseable over what is minimally needed to re-create the class. Ie maybe a filename or something simple like a string something that a constructor can use to create the class. The new activity can later getExtras and it is essentially creating a copy of the class with its constructor method.

    Here I launch the kmlreader class of my app when I recieve an onclick in the listview.

    Note: below summary is a list of the class that I am passing so get(position) returns the class infact it is the same list that populates the listview

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

    later in the new activity I pull out the parseable class with

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

    Good Luck Danny117

    0 讨论(0)
  • 2020-11-27 04:05
    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);
        }
    });
    
    0 讨论(0)
提交回复
热议问题