android开发分享安卓: 适配器(Apdater)与Intent(页面跳转)实现简单的投票案例

案例运行结果:代码部分:xml:activity_tou_piao.xml<?xml version=”1.0″ encoding=”utf-8″?><LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent”


案例运行结果:

安卓: 适配器(Apdater)与Intent(页面跳转)实现简单的投票案例
安卓: 适配器(Apdater)与Intent(页面跳转)实现简单的投票案例

代码部分:

xml:
activity_tou_piao.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"     xmlns:tools="https://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"    >     <ListView         android:id="@+id/listTouPiao"         android:layout_width="match_parent"         android:layout_height="wrap_content"></ListView> </LinearLayout>  

activity_toupiao_item.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"     android:descendantFocusability="blocksDescendants"     android:orientation="horizontal"     android:layout_width="match_parent"     android:layout_height="wrap_content">     <!-- android:descendantFocusability="blocksDescendants"     beforeDescendants:viewgroup会优先其子类控件而获取到焦点     afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点     blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点     -->     <LinearLayout         android:orientation="horizontal"         android:layout_width="match_parent"         android:layout_height="wrap_content">     <!-- 定义一个TextView,用于作为列表项的一部分  -->         <TextView             android:id="@+id/studentName"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:textSize="20dp"             android:textColor="#f0f"             android:paddingLeft="10dp"/>         <ProgressBar style="@style/Widget.AppCompat.ProgressBar.Horizontal"             android:id="@+id/progressCount"             android:layout_weight="1"             android:layout_width="0dp"             android:layout_height="wrap_content"             android:max="100"/>         <!-- 定义一个TextView,用于作为列表项的一部分  -->         <Button             android:id="@+id/btnTouPiao"             android:text="投票"             android:focusable="true"             android:clickable="true"             android:layout_gravity="right"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:textSize="14dp"             android:paddingLeft="10dp"/>     </LinearLayout> </LinearLayout>  

activity_single_student_info.xml

<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="https://schemas.android.com/apk/res/android"     xmlns:tools="https://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     >     <TableRow>         <TextView             android:text="学生姓名:"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:textSize="30dp"/>         <TextView             android:id="@+id/txtStudentName"             android:text=""             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:textSize="30dp"/>     </TableRow>     <TableRow>         <TextView             android:text="票数:"             android:textSize="30dp"/>         <TextView             android:id="@+id/txtCount"             android:textSize="30dp"/>     </TableRow> </TableLayout>  

java
ToupiaoActivity.java

package com.example.firstapplication.toupiaoTest;  import android.content.Context; import android.content.Intent; import android.os.Bundle; 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.ListView; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast;   import androidx.appcompat.app.AppCompatActivity;  import com.example.firstapplication.R;  import java.util.LinkedList;    public class TouPiaoActivity extends AppCompatActivity {     //列表中要显示的数据集合listStudent,其中每一项是一个Student对象,包括姓名和得票数两个属性     LinkedList<Student> listStudent=new LinkedList<Student>();     //界面要显示的ListView对象listTouPiao     ListView listTouPiao;     @Override     protected void onCreate(final Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_tou_piao);         //获取布局文件中定的ListView对象listTouPiao         listTouPiao=(ListView)this.findViewById(R.id.listTouPiao);         //给数据集合listStudent添加数据,将来可以从数据库中取的这个集合的数据         listStudent.add(new Student("张三",0));         listStudent.add(new Student("李四",0));         listStudent.add(new Student("王五",0));         //创建StudentAdapter对象adapter,adapter对象里面包含了集合的数据,         //以及集合的姓名和得票数与列表项中的studentName和progressCount的对应关系,以及投票按钮的事件处理         //其中集合的姓名赋给在studentName这个TextView的text属性,得票数赋给progressCount这个进度条的progress属性中         StudentApdater adapter=new StudentApdater(listStudent,this);         listTouPiao.setAdapter(adapter);          listTouPiao.setOnItemClickListener(new AdapterView.OnItemClickListener() {             @Override             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                 TextView studentName=(TextView)view.findViewById(R.id.studentName);                 ProgressBar progressBar=(ProgressBar)view.findViewById(R.id.progressCount);                 Bundle bundle=new Bundle();                  bundle.putString("studentName",studentName.getText().toString());                 bundle.putInt("count",progressBar.getProgress());                  Intent intent=new Intent(TouPiaoActivity.this,SingleStudentInfoActivity.class);                 intent.putExtras(bundle);                 startActivity(intent);                  Toast.makeText(TouPiaoActivity.this,"测试"+progressBar.getProgress(),Toast.LENGTH_LONG).show();             }         });     }      /**StudentApdater类 实现了ListView中的列表项的完全控制。      * listStudent集合保存了要在ListView中显示的所有数据      * context是上下文对象,在这里是指当前Activity对象,调用时用TouPiaoActivity.this即可      */     class StudentApdater extends BaseAdapter{         private LinkedList<Student> listStudent;//要显示在该adpter里的数据集合         private Context context;         public StudentApdater(LinkedList<Student> listStudent, Context context) {             this.listStudent = listStudent;             this.context = context;         }          @Override         public int getCount() {             //返回列表的项数             return listStudent.size();         }          @Override         public Object getItem(int position) {             //返回第position个列表项,可以用null代替             return null;         }          @Override         public long getItemId(int position) {             //返回第position项的id,一般用position代替             return position;         }          //getView方法是核心,在该方法中完全控制每一项的详细显示结果。         //本方法的最终结果是要生成一个View对象convertView,这个View对象就代表ListView的一个列表项,即第position行。         //convertView对象里面包含一个TextView对象studentName,一个ProgressBar对象progressCount,一个Button对象btnTouPiao         @Override         public View getView(final int position, View convertView, ViewGroup parent) {             //student_toupiao_item.xml是一个布局文件,用它来生成一个View对象convertView,             // 里面定义了一个TextView对象studentName,一个ProgressBar对象progressCount,一个Button对象btnTouPiao             //LayoutInflater是一个用于加载布局的系统服务,就是用来把student_toupiao_item.xml布局文件实例化为一个View对象,             // 即将Layout xml文件创建成一个view对象。对于一个没有被载入或者想要动态载入的布局界面,都需要使用LayoutInflater.inflate()来载入;             //LayoutInflater不能直接使用,需要先获得他的实例,有三种方法:             // LayoutInflater inflater1 = LayoutInflater.from(this);             // LayoutInflater inflater2 = getLayoutInflater();             // LayoutInflater inflater3 = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);             convertView= LayoutInflater.from(context).inflate(R.layout.student_toupiao_item,parent,false);             TextView studentName=(TextView)convertView.findViewById(R.id.studentName);             //progressCount必须定义成final,因为在btnTouPiao.setOnClickListener方法中要用到他,否则语法通不过。             final ProgressBar progressCount=(ProgressBar)convertView.findViewById(R.id.progressCount);             Button btnTouPiao=(Button)convertView.findViewById(R.id.btnTouPiao);             //listStudent集合中第position项的学生姓名设置为TextView对象studentName的text值             studentName.setText(listStudent.get(position).getName());             //listStudent集合中第position项的学生得票数设置为ProgressBar对象progressCount的progress值             progressCount.setProgress(listStudent.get(position).getCount());             //给投票按钮btnTouPiao添加OnClickListener监听器             btnTouPiao.setOnClickListener(new View.OnClickListener() {                 @Override                 public void onClick(View v) {                     //先将集合listStudent第position项的学生得票数+1                     listStudent.get(position).setCount(listStudent.get(position).getCount()+1);                     //再将集合listStudent第position项的学生得票数(加1后的)赋给progressCount的progress属性,让他显示出来                     progressCount.setProgress(listStudent.get(position).getCount());                 }             });             //返回最新变化后的convertView(即将姓名和得票数都显示到相应控件后的convertView)             return convertView;         }     }      /**Student实体类      * 包含姓名name和得票数count属性      * 一个Student对象保存一个学生的姓名和得票数,      * 将来将这些对象放到listStudent集合中,用于显示到ListView控件中。      */     class Student{         private String name;         private int count;//该学生获得的票数         public Student(String name,int count){             this.name=name;             this.count=count;         }         public Student(){         }         public String getName() {             return name;         }          public void setName(String name) {             this.name = name;         }          public int getCount() {             return count;         }          public void setCount(int count) {             this.count = count;         }     } }  

SingleStudentInfoActivity.java

package com.example.firstapplication.toupiaoTest;  import android.content.Intent; import android.os.Bundle; import android.widget.TextView;  import androidx.appcompat.app.AppCompatActivity;  import com.example.firstapplication.R;   public class SingleStudentInfoActivity extends AppCompatActivity {     TextView txtStudentName,txtCount;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_single_student_info);         Intent intent=this.getIntent();         Bundle bundle=intent.getExtras();         String studentName=bundle.getString("studentName");         System.out.println("studentName="+studentName);         int count=bundle.getInt("count");         txtStudentName=(TextView)findViewById(R.id.txtStudentName);         txtCount=(TextView)findViewById(R.id.txtCount);         txtStudentName.setText(studentName);         txtCount.setText(" "+count);     } }  

www.ctvol.com true Article android开发分享安卓: 适配器(Apdater)与Intent(页面跳转)实现简单的投票案例

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/addevelopment/890909.html

(0)
上一篇 2021年10月19日 下午5:24
下一篇 2021年10月19日 下午5:26

精彩推荐