需求
每点击一次按钮就添加一条数据(图片+文字)
项目目录
.java
.domain
Data
package top.gaojc.calculator.domain; public class Data { private int imgId; private String content; public Data() {} public Data(int imgId, String content) { this.imgId = imgId; this.content = content; } public int getImgId() { return imgId; } public String getContent() { return content; } public void setImgId(int imgId) { this.imgId = imgId; } public void setContent(String content) { this.content = content; } }
MainActivity.java
package top.gaojc.calculator; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.os.Bundle; import android.provider.ContactsContract; import android.view.View; import android.widget.Button; import android.widget.ListView; import java.util.LinkedList; import java.util.List; import top.gaojc.calculator.domain.Data; public class MainActivity extends AppCompatActivity implements View.OnClickListener { // 初始化 Button btn_add; int flag = 1; private ListView list_one; private MyAdapter mAdapter = null; private List<Data> mData = null; private Context mContext = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = MainActivity.this; bindViews(); mData = new LinkedList<Data>(); mAdapter = new MyAdapter((LinkedList<Data>) mData,mContext); list_one.setAdapter(mAdapter); btn_add = findViewById(R.id.btn_add); btn_add.setOnClickListener(this); } private void bindViews(){ list_one = (ListView) findViewById(R.id.list_one); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btn_add: mAdapter.add(new Data(R.drawable.girl,"想你想你想我!!! × " + flag)); flag++; break; } } }
MyAdapter.java
package top.gaojc.calculator; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.LinkedList; import top.gaojc.calculator.domain.Data; public class MyAdapter extends BaseAdapter { private Context mContext; private LinkedList<Data> mData; public MyAdapter() {} public MyAdapter(LinkedList<Data> mData, Context mContext) { this.mData = mData; this.mContext = mContext; } @Override public int getCount() { return mData.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if(convertView == null){ convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list,parent,false); holder = new ViewHolder(); holder.img_icon = (ImageView) convertView.findViewById(R.id.img_icon); holder.txt_content = (TextView) convertView.findViewById(R.id.txt_content); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } holder.img_icon.setImageResource(mData.get(position).getImgId()); holder.txt_content.setText(mData.get(position).getContent()); return convertView; } private class ViewHolder{ ImageView img_icon; TextView txt_content; } public void add(Data data) { if (mData == null) { mData = new LinkedList<>(); } mData.add(data); notifyDataSetChanged(); } }
.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <Button android:id="@+id/btn_add" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加数据"/> <ListView android:id="@+id/list_one" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
item_list.xml
<?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:orientation="horizontal"> <ImageView android:id="@+id/img_icon" android:layout_width="56dp" android:layout_height="56dp"/> <TextView android:id="@+id/txt_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginLeft="10dp" android:textSize="18sp" /> </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="top.gaojc.calculator"> <application android:allowBackup="true" android:icon="@drawable/girl" android:label="@string/app_name" android:roundIcon="@drawable/girl" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
页面效果