<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加一条记录" />
<Button
android:id="@+id/btn_add2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="往第五列插入一条数据" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="根据对象删除数据" />
<Button
android:id="@+id/btn_remove2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="根据游标删除数据" />
LinearLayout>
<ListView
android:id="@+id/list_one"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txt_empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#000000"
android:textSize="15pt" />
LinearLayout>
package run.yang.com.listviewactivedemo;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.LinkedList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ListView list_one;
private TextView txt_empty;
private Button btn_add;
private Button btn_add2;
private Button btn_remove;
private Button btn_remove2;
private MyAdapter mAdapter = null;
private List mData = null;
private Context mContext = null;
private int flag = 1;
private Data mData_5 = null; //用来临时放对象的
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
mData = new LinkedList();
mAdapter = new MyAdapter((LinkedList) mData,mContext);
bindViews();
}
private void bindViews(){
list_one = (ListView) findViewById(R.id.list_one);
txt_empty = (TextView) findViewById(R.id.txt_empty);
btn_add = (Button) findViewById(R.id.btn_add);
btn_add2 = (Button) findViewById(R.id.btn_add2);
btn_remove = (Button) findViewById(R.id.btn_remove);
btn_remove2 = (Button) findViewById(R.id.btn_remove2);
txt_empty.setText("暂无数据~");
list_one.setAdapter(mAdapter);
list_one.setEmptyView(txt_empty);
btn_add.setOnClickListener(this);
btn_add2.setOnClickListener(this);
btn_remove.setOnClickListener(this);
btn_remove2.setOnClickListener(this);
}
private void updateListItem(int postion,Data mData){
int visiblePosition = list_one.getFirstVisiblePosition();
View v = list_one.getChildAt(postion - visiblePosition);
ImageView img = (ImageView) v.findViewById(R.id.img_icon);
TextView tv = (TextView) v.findViewById(R.id.txt_content);
img.setImageResource(mData.getImgId());
tv.setText(mData.getContent());
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_add:
if(flag == 5){
mData_5 = new Data(R.mipmap.ic_icon_qitao,"给政哥跪了~~~ x " + flag);
mAdapter.add(mData_5);
}else{
mAdapter.add(new Data(R.mipmap.ic_icon_qitao,"给政哥跪了~~~ x " + flag));
}
flag++;
break;
case R.id.btn_add2:
//position从0开始算的
mAdapter.add(4,new Data(R.mipmap.ic_icon_qitao,"给政哥跪了~~~ x " + flag));
break;
case R.id.btn_remove:
mAdapter.remove(mData_5);
break;
case R.id.btn_remove2:
mAdapter.remove(2);
break;
}
}
}
package run.yang.com.listviewactivedemo;
/**
* Created by yang_zzheng on 2016/7/14
* [email protected]
*/
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;
}
}
package run.yang.com.listviewactivedemo;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.LinkedList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ListView list_one;
private TextView txt_empty;
private Button btn_add;
private Button btn_add2;
private Button btn_remove;
private Button btn_remove2;
private MyAdapter mAdapter = null;
private List mData = null;
private Context mContext = null;
private int flag = 1;
private Data mData_5 = null; //用来临时放对象的
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
mData = new LinkedList();
mAdapter = new MyAdapter((LinkedList) mData,mContext);
bindViews();
}
private void bindViews(){
list_one = (ListView) findViewById(R.id.list_one);
txt_empty = (TextView) findViewById(R.id.txt_empty);
btn_add = (Button) findViewById(R.id.btn_add);
btn_add2 = (Button) findViewById(R.id.btn_add2);
btn_remove = (Button) findViewById(R.id.btn_remove);
btn_remove2 = (Button) findViewById(R.id.btn_remove2);
txt_empty.setText("暂无数据~");
list_one.setAdapter(mAdapter);
list_one.setEmptyView(txt_empty);
btn_add.setOnClickListener(this);
btn_add2.setOnClickListener(this);
btn_remove.setOnClickListener(this);
btn_remove2.setOnClickListener(this);
}
private void updateListItem(int postion,Data mData){
int visiblePosition = list_one.getFirstVisiblePosition();
View v = list_one.getChildAt(postion - visiblePosition);
ImageView img = (ImageView) v.findViewById(R.id.img_icon);
TextView tv = (TextView) v.findViewById(R.id.txt_content);
img.setImageResource(mData.getImgId());
tv.setText(mData.getContent());
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_add:
if(flag == 5){
mData_5 = new Data(R.mipmap.ic_icon_qitao,"给政哥跪了~~~ x " + flag);
mAdapter.add(mData_5);
}else{
mAdapter.add(new Data(R.mipmap.ic_icon_qitao,"给政哥跪了~~~ x " + flag));
}
flag++;
break;
case R.id.btn_add2:
//position从0开始算的
mAdapter.add(4,new Data(R.mipmap.ic_icon_qitao,"给政哥跪了~~~ x " + flag));
break;
case R.id.btn_remove:
mAdapter.remove(mData_5);
break;
case R.id.btn_remove2:
mAdapter.remove(2);
break;
}
}
}
<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>
http://blog.csdn.net/coder_pig/article/details/48631595