滚动列表视图后,无法将项目添加到列表视图中

时间:2015-06-27 05:51:37

标签: android android-layout listview

当屏幕变满时,使用Onclick Listener功能插入文本和图像,当列表变为可滚动时,当我尝试添加新项目时,当我向下滚动列表时,我看到第一个项目列表已添加到列表中,而不是必须添加到列表中的当前项目。

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" />

1 个答案:

答案 0 :(得分:1)

问题在于适配器的getView方法,在列表视图中,将重新使用屏幕上的视图而不是创建新视图,因此视图将具有第一个视图的详细信息。

尝试用下面的方法替换你的getView方法,它应该解决你的第一个项目再次出现的问题。

 @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;
        }

    } else {
        holder = (ViewHolder) convertView.getTag();
    }
switch (type) {
case TYPE_ITEM:
                holder.textView.setText(myImage.getName());
                break;
 case TYPE_SEPARATOR:           
                holder.img.setImageResource(myImage.getImage());
                break;
}
convertView.setTag(holder);
    return convertView;
}

因此,我们需要再次为再循环的视图设置细节。