ListView
1.简单列表--ArrayAdapter
package com.example.listviewtest;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_array_list = findViewById(R.id.btn_array_list);
btn_array_list.setOnClickListener(this);
Button btn_array_simple = findViewById(R.id.btn_array_simple);
btn_array_simple.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_array_list){
Intent intent = new Intent(this, ArrayListActivity.class);
startActivity(intent);
} else if (view.getId() == R.id.btn_array_simple) {
Intent intent = new Intent(this, SimpleListActivity.class);
startActivity(intent);
}
}
}
package com.example.listviewtest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class ArrayListActivity extends AppCompatActivity {
private ListView mListView;
private List mStringList;
private ArrayAdapter mArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_array_list);
mListView = findViewById(R.id.lv);
mStringList = new ArrayList<>();
for (int i = 0; i < 50; i++) {
mStringList.add("这是条目"+i);
}
mArrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,mStringList);
mListView.setAdapter(mArrayAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
Toast.makeText(ArrayListActivity.this,"你点击了"+i,Toast.LENGTH_LONG).show();
}
});
}
}
2.图文列表--SimpleAdapter
package com.example.listviewtest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SimpleListActivity extends AppCompatActivity {
private ListView mListView;
private SimpleAdapter mSimpleAdapter;
private List
3.图文复杂列表--BaseAdapter
package com.example.listviewtest;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_array_list = findViewById(R.id.btn_array_list);
btn_array_list.setOnClickListener(this);
Button btn_array_simple = findViewById(R.id.btn_array_simple);
btn_array_simple.setOnClickListener(this);
Button btn_array_base = findViewById(R.id.btn_array_base);
btn_array_base.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_array_list){
Intent intent = new Intent(this, ArrayListActivity.class);
startActivity(intent);
} else if (view.getId() == R.id.btn_array_simple) {
Intent intent = new Intent(this, SimpleListActivity.class);
startActivity(intent);
} else if (view.getId() == R.id.btn_array_base) {
Intent intent = new Intent(this, BaseAdapterActivity.class);
startActivity(intent);
}
}
}
package com.example.listviewtest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.example.listviewtest.adapter.MyAdapter;
import com.example.listviewtest.bean.ItemBean;
import java.util.ArrayList;
import java.util.List;
public class BaseAdapterActivity extends AppCompatActivity {
private ListView mListView;
private List mBeanList;
private MyAdapter mMyAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base_adapter);
initView();
initData();
initEvent();
}
private void initEvent() {
mMyAdapter = new MyAdapter(this,mBeanList);
mListView.setAdapter(mMyAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
ItemBean itemBean = mBeanList.get(i);
String title = itemBean.getTitle();
Toast.makeText(BaseAdapterActivity.this,"您点击了"+i+title,Toast.LENGTH_LONG).show();
}
});
mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView> adapterView, View view, int i, long l) {
return false;
}
});
}
private void initData() {
mBeanList = new ArrayList<>();
ItemBean itemBean1 = new ItemBean();
itemBean1.setTitle("我的小黑狗");
itemBean1.setContent("我的小黑狗,明天就到了,到底是死是活,没人知道,希望你不会挂");
itemBean1.setImgResId(R.drawable.test1);
ItemBean itemBean2 = new ItemBean();
itemBean2.setTitle("我的小白狗");
itemBean2.setContent("我的小白狗,以前跑丢了,希望你不会被狗贩子抓去杀了,卖狗肉");
itemBean2.setImgResId(R.drawable.test2);
ItemBean itemBean3 = new ItemBean();
itemBean3.setTitle("我的小黑狗");
itemBean3.setContent("我的小黑狗,明天就到了,到底是死是活,没人知道,希望你不会挂");
itemBean3.setImgResId(R.drawable.test3);
ItemBean itemBean4 = new ItemBean();
itemBean4.setTitle("我的小白狗");
itemBean4.setContent("我的小白狗,以前跑丢了,希望你不会被狗贩子抓去杀了,卖狗肉");
itemBean4.setImgResId(R.drawable.test4);
ItemBean itemBean5 = new ItemBean();
itemBean5.setTitle("我的小黑狗");
itemBean5.setContent("我的小黑狗,明天就到了,到底是死是活,没人知道,希望你不会挂");
itemBean5.setImgResId(R.drawable.test5);
ItemBean itemBean6 = new ItemBean();
itemBean6.setTitle("我的小白狗");
itemBean6.setContent("我的小白狗,以前跑丢了,希望你不会被狗贩子抓去杀了,卖狗肉");
itemBean6.setImgResId(R.drawable.test6);
ItemBean itemBean7 = new ItemBean();
itemBean7.setTitle("我的小黑狗");
itemBean7.setContent("我的小黑狗,明天就到了,到底是死是活,没人知道,希望你不会挂");
itemBean7.setImgResId(R.drawable.test7);
ItemBean itemBean8 = new ItemBean();
itemBean8.setTitle("我的小白狗");
itemBean8.setContent("我的小白狗,以前跑丢了,希望你不会被狗贩子抓去杀了,卖狗肉");
itemBean8.setImgResId(R.drawable.test8);
ItemBean itemBean9 = new ItemBean();
itemBean9.setTitle("我的小黑狗");
itemBean9.setContent("我的小黑狗,明天就到了,到底是死是活,没人知道,希望你不会挂");
itemBean9.setImgResId(R.drawable.test9);
ItemBean itemBean10 = new ItemBean();
itemBean10.setTitle("我的小白狗");
itemBean10.setContent("我的小白狗,以前跑丢了,希望你不会被狗贩子抓去杀了,卖狗肉");
itemBean10.setImgResId(R.drawable.test10);
mBeanList.add(itemBean1);
mBeanList.add(itemBean2);
mBeanList.add(itemBean3);
mBeanList.add(itemBean4);
mBeanList.add(itemBean5);
mBeanList.add(itemBean6);
mBeanList.add(itemBean7);
mBeanList.add(itemBean8);
mBeanList.add(itemBean9);
mBeanList.add(itemBean10);
}
private void initView() {
mListView = findViewById(R.id.lv);
}
}
package com.example.listviewtest.adapter;
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 com.example.listviewtest.R;
import com.example.listviewtest.bean.ItemBean;
import java.util.List;
public class MyAdapter extends BaseAdapter {
private List mBeanList;
private LayoutInflater mLayoutInflater;
private Context mContext;
public MyAdapter(Context context, List beanList){
this.mContext = context;
this.mBeanList = beanList;
mLayoutInflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
return mBeanList.size();
}
@Override
public Object getItem(int i) {
return mBeanList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = mLayoutInflater.inflate(R.layout.list_item_layout,viewGroup,false);
ImageView imageView = view.findViewById(R.id.iv_img);
TextView tvTitle= view.findViewById(R.id.tv_title);
TextView tvContent = view.findViewById(R.id.tv_content);
ItemBean itemBean = mBeanList.get(i);
imageView.setImageResource(itemBean.getImgResId());
tvTitle.setText(itemBean.getTitle());
tvContent.setText(itemBean.getContent());
return view;
}
}
package com.example.listviewtest.bean;
public class ItemBean {
private String title;
private String content;
private int imgResId;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getImgResId() {
return imgResId;
}
public void setImgResId(int imgResId) {
this.imgResId = imgResId;
}
@Override
public String toString() {
return "itemBean{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
", imgResId=" + imgResId +
'}';
}
}