An analysis of the problem of Android ListView point Praise _android

Source: Internet
Author: User
Tags static class first row visibility

I've been looking at Android for the last time, using ListView to do some of the awesome functions

Basic ideas:

Access to the interface – "Getting Data" –
Show in ListView-"
Save each row by clicking on the Map Collection (Position,boolean)-
Use entity classes to save objects-"
The Get/set method is worth changing accordingly-"
Click once, the corresponding quantity plus 1

Only achieved a point of Praise function , tread and praise basic similar.

Specifically implemented as follows:

Inherit from Baseadapter

Package Com.gz.test_listview;
Import Android.content.Context;
Import Android.content.DialogInterface;
Import android.content.Intent;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.util.Log;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.AdapterView;
Import Android.widget.BaseAdapter;
Import Android.widget.Button;
Import Android.widget.ImageView;
Import Android.widget.ListView;
Import Android.widget.TextView;

Import Android.widget.Toast;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;

Import Java.util.Map;
 /** * Created by GZ on 2016/11/9.
  * * public class Mainactivityadapter extends baseadapter{//used to get elements of the interface private Viewholder holder;
  private context; The Bean here is a test of the internal class, the point of praise and the number of steps to save, you can define a class,//direct access to data is good, need to improve the private list<bean> Praise_step_num = new arraylist<

  Bean> ();
  Private Layoutinflater Inflater; Save the current point of praise List<map<striNg,object>> ListItems; /* According to position, save the clicked which row of data, the default is False, click to True/private Map<integer, boolean> isexist = new Hashmap<integer

  , boolean> ();
    Public Mainactivityadapter (Context context,list<map<string,object>> listitems) {this.context = context;
    This.listitems = ListItems;
    Inflater = Layoutinflater.from (context);
    LOG.I ("ListItem", listitems.tostring ());
  Init (); private void Init () {/* is assigned a value for the corresponding data, all data is stored in listitems position one by one corresponds to each row of data * * for (int i = 0;i<li
      Stitems.size (); i++) {isexist.put (i,false);
      Bean B = new Bean ();
      Integer praise = Integer.parseint (Listitems.get (i). Get ("Praise"). ToString ());
      B.setpraise (Praise);
      Integer step = Integer.parseint (Listitems.get (i). Get ("step"). ToString ());
      B.setstep (step);
      Praise_step_num.add (I,B);
    LOG.I ("Praise_step", Praise_step_num.get (i). Getpraise () + ""); @Override public int GetCount () {return LIstitems.size ();
  @Override public Object getitem (int position) {return position;
  @Override public long getitemid (int position) {return position; @Override Public View getview (final int position, View Convertview, ViewGroup parent) {//Get the praise, step value final Bea

    n Bean = praise_step_num.get (position);
      if (Convertview = = null) {holder = new Viewholder ();
      Convertview = Inflater.inflate (r.layout.item_praise,null);
      Holder.img_praise = (ImageView) Convertview.findviewbyid (r.id.tv_praises_img);
      Holder.img_step = (ImageView) Convertview.findviewbyid (r.id.tv_step_img);
      Holder.praise = (TextView) Convertview.findviewbyid (r.id.tv_praises);
      Holder.step = (TextView) Convertview.findviewbyid (r.id.tv_step);


      Holder.title = (TextView) Convertview.findviewbyid (r.id.tv_name);



      Holder.img_step.setImageResource (R.drawable.bad);
    Convertview.settag (holder); }else{holder = (viewholder) convertview.geTtag ();
    } holder.praise.setText (Bean.getpraise () + "");
    Holder.step.setText (Bean.getstep () + "");
     Holder.title.setText (Listitems.get (position). Get ("title") + ""); /* Every time the Convertview, Convertview is not assigned value, will be used after the assignment of data, such as: Click on the first row of data Praise button, the corresponding first line of praise picture into red when sliding down the page, the bottom of the There will be a change in the data, reused here for each reuse assignment/if (Bean.getpraise ()!=0) {Holder.img_praise.setImageResource (r.drawable).
    Good);
    }else{Holder.img_praise.setImageResource (R.DRAWABLE.GOOD_NO);
    } if (Bean.getstep ()!=0) {Holder.img_step.setImageResource (R.drawable.bad);
    }else{Holder.img_step.setImageResource (R.DRAWABLE.BAD_NO);

    } holder.img_praise.setOnClickListener (New Imgclick (Position,bean));
  return convertview;
    Class Imgclick implements View.onclicklistener {private int position;

    Private Bean bean;
      Public Imgclick (int position,bean Bean) {this.position = position;
    This.bean = Bean; } @Override PubLIC void OnClick (final View v) {log.i ("position", position+ "");
            if (Bean.getpraise () ==0) {if isexist.get (position) = = False) {Final Handler Handler = new Handler () {
              @Override public void Handlemessage (msg) {super.handlemessage);
                  Switch (msg.what) {case 1:imageview btn = (imageview) v;
                    if (v.getid () = = Btn.getid ()) {Isexist.put (position, true);
                    Bean.setpraise (Bean.getpraise () +1);
                    Btn.setimageresource (R.drawable.good);
                    Animationtools.scale (BTN);
                    Notifydatasetchanged ();
                  Break
                  Case 2:toast.maketext (context, "failure", Toast.length_long). Show ();
              Break
          }
            }
          }; New Thread () {@Override public VOID run () {msg = new message ();
              Msg.what=1;
            Handler.sendmessage (msg);
        }}.start ();
      }else{Toast.maketext (context, "already clicked", Toast.length_long). Show ();

    }} private static class viewholder{public ImageView img_praise;
    Public ImageView Getimg_praise () {return img_praise;
    Public ImageView Getimg_step () {return img_step;
    Public TextView getpraise () {return praise;
    Public TextView Getstep () {return step;
    Public TextView GetTitle () {return title;
    Private ImageView Img_step;
    Public TextView Praise;
    Private TextView step;


  Private TextView title;
    Class bean{public int getpraise () {return praise;
    The public void setpraise (int praise) {this.praise = praise;
    public int Getstep () {return step; } public void SetStep (inT step} {this.step = step;
    } private int praise;

  private int step;

 }
}

Mainactivity

Assign a value to a ListView value, and then bind to and adapter

Package Com.gz.test_listview;
Import android.app.Activity;
Import android.content.Intent;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.AdapterView;

Import Android.widget.ListView;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;

Import java.util.Objects;
  public class Mainactivity extends activity {private ListView ListView;
  Private string[] title = {"Commercial", "household", "test", "multi-Union", "Air energy", "commercial", "Home", "Test", "multi-Union", "Air energy"};
  Private string[] Praise = {"3", "1", "0", "0", "0", "3", "1", "0", "0", "0"};

  Private string[] Step = {"3", "0", "0", "0", "0", "3", "0", "0", "0", "0"};

  Private arraylist<map<string,object>> Map = new arraylist<map<string, object>> ();
  Private ListView ListView;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main); ListView = (ListView)Findviewbyid (R.id.listview);
      for (int i=0;i<title.length;i++) {map<string,object> item = new hashmap<string,object> ();
      Item.put ("title", Title[i]);
      Item.put ("Praise", Praise[i]);
      Item.put ("Step", Step[i]);
    Map.add (item);

    } mainactivityadapter adapter = new Mainactivityadapter (THIS,MAP);
    Listview.setadapter (adapter); Listview.setonitemclicklistener (New Adapterview.onitemclicklistener () {@Override public void Onitemclick (ADAP Terview<?> Parent, view view, int position, long id) {Intent Intent = new Intent (Mainactivity.this,detailac
        Tivity.class);
      StartActivity (Intent);

  }
    });

 }
}

Jump Interface , test with

Package Com.gz.test_listview;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.ListView;

Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Map;

public class Detailactivity extends activity {


  @Override
  protected void onCreate (Bundle savedinstancestate) { C11/>super.oncreate (savedinstancestate);
    Setcontentview (R.layout.detail_info);
  }


The realization of simple animation effect

Just use it right now.

Package Com.gz.test_listview;

Import Android.view.View;
Import android.view.animation.Animation;
Import android.view.animation.ScaleAnimation;

public class Animationtools {public
  static void scale (View v) {
    scaleanimation anim = new Scaleanimation (1.0f, 1. 5f, 1.0f, 1.5f,
        animation.relative_to_self, 0.5f, animation.relative_to_self,
        0.5f);
    Anim.setduration ();
    V.startanimation (ANIM);

  }

Interface

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
  "http://" Schemas.android.com/apk/res/android "
  xmlns:tools=" Http://schemas.android.com/tools "
  android:layout_ Width= "Match_parent"
  android:layout_height= "match_parent"
  android:paddingleft= "@dimen/activity_ Horizontal_margin "
  android:paddingright=" @dimen/activity_horizontal_margin "
  android:paddingtop=" @dimen /activity_vertical_margin "
  android:paddingbottom=" @dimen/activity_vertical_margin "
  tools:context=" Com.gz.test_listview. Mainactivity ">

  <listview
    android:id=" @+id/listview "android:layout_width=" Match_parent "
    android:layout_height= "Wrap_content" >


  </ListView>
</RelativeLayout>


ListView Child Items

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:id=" @+id/lyt_root "android:layout_width=" match_parent "android:layout_height=" Match_parent "Andro" Id:background= "#ccc" android:orientation= "vertical" android:descendantfocusability= "blocksdescendants" > <F
      Ramelayout android:layout_width= "fill_parent" android:layout_height= "wrap_content" > <ImageView Android:id= "@+id/has_exame" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" and roid:layout_gravity= "Top|left"/> <linearlayout android:layout_margin= "10dip" Android:layout_ Width= "Fill_parent" android:layout_height= "wrap_content" android:layout_gravity= "center_vertical" Androi

      d:background= "@drawable/corners" android:layout_weight= "1.0" android:orientation= "vertical" > <textview android:id= "@+id/tv_name "android:layout_width=" match_parent "android:layout_height=" Wrap_content "Android:tex" Tsize= "14SP" android:text= "commercial multiple online" android:textcolor= "#000"/> <linearlayout A Ndroid:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" &G

        T <textview android:id= "@+id/tv_date" android:layout_margintop= "10dip" android:layout_width = "Match_parent" android:layout_height= "wrap_content" android:textsize= "12SP" android:text= " 2016-10-10 "android:textcolor=" #000 "android:visibility=" visible "/> <tex Tview android:id= "@+id/tv_downloads" android:layout_width= "Wrap_content" Android:layout_heig ht= "Wrap_content" android:layout_gravity= "center_vertical" android:layout_weight= "1" Android : textcolor= "#000 "android:textsize=" 14sp "android:visibility=" Gone "/> </LinearLayout> ; LinearLayout android:layout_width= "match_parent" android:layout_height= "Match_parent" Android:orie ntation= "Horizontal" android:gravity= "Center|right" > <imageview android:id= "@+id" /tv_praises_img "android:layout_width=" 30dip "android:layout_height=" 30dip "Android:backgrou nd= "@drawable/good_no" android:layout_marginright= "5dip"/> <textview android: Id= "@+id/tv_praises" android:layout_width= "50dip" android:layout_height= "Wrap_content" Andro Id:textcolor= "#000" android:text= "android:textsize=" 14dip "android:layout_gravity=" Cente
          R "/> <imageview android:id=" @+id/tv_step_img "android:layout_width=" 30dip " Android:layout_height= "30dip" android:background= "@drawable/bad_no" android:layout_marginright= "5dip"/&G
        T <textview android:id= "@+id/tv_step" android:layout_width= "50dip" android:layout_height= "W Rap_content "android:textcolor=" #000 "android:text=" android:textsize= "14dip" a ndroid:layout_gravity= "center"/> </LinearLayout> </LinearLayout> </framelayou

 T> </LinearLayout>

Enter the interface after clicking Item

<?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=" match_parent "android:paddingleft=" @dimen Activity_horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen
  Activity_vertical_margin "android:paddingbottom=" @dimen/activity_vertical_margin "android:orientation=" vertical " > <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "11 1 "/> <textview android:layout_width=" wrap_content android:layout_height= "Wrap_content" android:t ext= "/>" <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" an 
    droid:text= "/>" <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "/>" <textviEW android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "/>"


 ;/linearlayout>

In the code is my understanding of the program, some places may not be very clear, but also perfect
This article gave me a lot of help, animation effect is moved over, haha, very good, learning.

Reference article: http://www.jb51.net/article/97335.htm

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.