1.ListViewUpdate.java文件:
2. ViewAdapter.java适配器文件
3. ItemListener.java监听器:
4.最后是3个xml布局文件:
在附上几个图片文件,供测试使用:
package com.example;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
public class ListViewUpdate extends Activity implements OnClickListener {
private ListView lvDynamic;
private ViewAdapter viewAdapter;
private ItemListener listener;
private static String[] data = new String[] { "机器化身", "第九区", "火星任务",
"人工智能", "钢铁侠", "铁臂阿童木 ", "未来战士", "星际传奇", };
private int selectedIndex = -1;
public int getSelectedIndex() {
return selectedIndex;
}
public void setSelectedIndex(int index){
this.selectedIndex = index;
}
private int getImageResourceId() {
int[] resourceIds = new int[] { R.drawable.item1, R.drawable.item2,
R.drawable.item3, R.drawable.item4, R.drawable.item5 };
return resourceIds[new Random().nextInt(resourceIds.length)];
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lvDynamic = (ListView) findViewById(R.id.lvDynamic);
Button btnAddText = (Button) findViewById(R.id.btnAddText);
Button btnAddImage = (Button) findViewById(R.id.btnAddImage);
Button btnRemove = (Button) findViewById(R.id.btnRemove);
Button btnRemoveAll = (Button) findViewById(R.id.btnRemoveAll);
btnAddText.setOnClickListener(this);
btnAddImage.setOnClickListener(this);
btnRemove.setOnClickListener(this);
btnRemoveAll.setOnClickListener(this);
listener = new ItemListener(this);
viewAdapter = new ViewAdapter(this);
lvDynamic.setAdapter(viewAdapter);
lvDynamic.setOnItemSelectedListener(listener);
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch (view.getId()) {
case R.id.btnAddText:
int randomNum = new Random().nextInt(data.length);
viewAdapter.addText(data[randomNum]);
break;
case R.id.btnAddImage:
viewAdapter.addImage(getImageResourceId());
break;
case R.id.btnRemove:
viewAdapter.remove(selectedIndex);
selectedIndex = -1;
break;
case R.id.btnRemoveAll:
viewAdapter.removeAll();
break;
}
}
}
2. ViewAdapter.java适配器文件
package com.example;
import java.util.ArrayList;
import java.util.List;
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.LinearLayout;
import android.widget.TextView;
public class ViewAdapter extends BaseAdapter {
private Context context;
private List textIdList = new ArrayList();
public ViewAdapter(Context context) {
this.context = context;
}
public int getCount() {
return textIdList.size();
}
public Object getItem(int position) {
return textIdList.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("position2222----------------------->"+position);
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(inflater);
LinearLayout linearLayout = null;
if (textIdList.get(position) instanceof String) {
linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.text,
null);
TextView textView = ((TextView) linearLayout
.findViewById(R.id.textview));
textView.setText(String.valueOf(textIdList.get(position)));
} else if (textIdList.get(position) instanceof Integer) {
linearLayout = (LinearLayout) layoutInflater.inflate(
R.layout.image, null);
ImageView imageView = (ImageView) linearLayout
.findViewById(R.id.imageview);
imageView.setImageResource(Integer.parseInt(String
.valueOf(textIdList.get(position))));
}
return linearLayout;
}
public void addText(String text) {
textIdList.add(text);
notifyDataSetChanged();
}
public void addImage(int resId) {
textIdList.add(resId);
notifyDataSetChanged();
}
public void remove(int index) {
if (index < 0)
return;
textIdList.remove(index);
notifyDataSetChanged();
}
public void removeAll() {
textIdList.clear();
notifyDataSetChanged();
}
}
3. ItemListener.java监听器:
package com.example;
import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
public class ItemListener implements OnItemSelectedListener {
private Context con;
public ItemListener(Context con){
this.con = con;
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
((ListViewUpdate)con).setSelectedIndex(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
4.最后是3个xml布局文件:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:id="@+id/btnAddText" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="添加文本" />
<Button android:id="@+id/btnAddImage" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="添加图像" />
<Button android:id="@+id/btnRemove" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="删除当前项" />
<Button android:id="@+id/btnRemoveAll" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="清空" />
</LinearLayout>
<ListView android:id="@+id/lvDynamic" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:cacheColorHint="#00000000" />
</LinearLayout>
image.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView android:id="@+id/imageview" android:layout_width="100dp"
android:layout_height="100dp" android:padding="10dp"/>
</LinearLayout>
text.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/textview" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:padding="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
在附上几个图片文件,供测试使用: