當屏幕變滿時,即通過使用Onclick監聽器功能插入文本和圖像後,列表變爲可滾動,當我嘗試添加新項目以及當我向下滾動列表,我看到列表的第一項已添加到列表中,而不是必須添加到列表中的當前項目。滾動列表視圖後無法將項目添加到列表視圖
CustomListView
public class CustomListView extends ListActivity {
ArrayList<Item> imageArry = new ArrayList<Item>();
//CustomImageAdapter adapter;
private MyCustomAdapter mAdapter;
Button a, b;
EditText abc;
String result;
ListView dataList;
Item abcd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
abc = (EditText) findViewById(R.id.one);
a = (Button) findViewById(R.id.button);
b = (Button) findViewById(R.id.button2);
mAdapter = new MyCustomAdapter(this);
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
result = abc.getText().toString();
mAdapter.addItem(new Item(result));
}
});
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mAdapter.addSeparatorItem(new Item(R.drawable.google));
}
});
setListAdapter(mAdapter);
}
}
MyCustomAdapter
public class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
Context context;
private ArrayList<Item> mData = new ArrayList<Item>();
private LayoutInflater mInflater;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
public MyCustomAdapter(Context context) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
}
public void addItem(final Item item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparatorItem(final Item item) {
mData.add(item);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Item getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Item myImage = mData.get(position);
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.textlayout, null);
holder.textView = (TextView) convertView.findViewById(R.id.resulttext);
holder.textView.setText(myImage.getName());
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.resultimage, null);
holder.img = (ImageView) convertView.findViewById(R.id.resultimage);
holder.img.setImageResource(myImage.getImage());
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
public static class ViewHolder {
public TextView textView;
public ImageView img;
}
}
Item.java
public class Item {
int image;
String name;
public Item(int image, String name) {
super();
this.image = image;
this.name = name;
}
public Item(String name) {
super();
this.name = name;
}
public Item() {
}
public Item(int image) {
super();
this.image=image;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.55"
android:cacheColorHint="#00000000"></ListView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/one"
android:layout_width="230dp"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="a"
android:id="@+id/button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="b"
android:id="@+id/button2"/>
</LinearLayout>
textlayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/resulttext"/>
resultimage.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/resultimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
你有'fill_parent'列表視圖。即將覆蓋整個屏幕 – Raghunandan
,但我已經使用列表視圖正確的 –
填充父,所以它會採取全屏幕的權利。其他元素的空間在哪裏。同樣使用'match_parent' – Raghunandan