2015-06-27 237 views
2

當屏幕變滿時,即通過使用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" /> 

+1

你有'fill_parent'列表視圖。即將覆蓋整個屏幕 – Raghunandan

+0

,但我已經使用列表視圖正確的 –

+0

填充父,所以它會採取全屏幕的權利。其他元素的空間在哪裏。同樣使用'match_parent' – Raghunandan

回答

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

所以,我們需要再次設置細節再循環的視圖。

+0

它爲我工作,謝謝你 –

+0

我的解決方案有幫助,或者你還有其他一些問題嗎? – Dinash

+0

不用謝謝你 –