2013-07-30 32 views
2

我有一个活动,显示从ArrayList中取得的最初填充的自定义ListView,然后用户可以添加到这个列表中,我没有问题存储。我在显示添加的项目时遇到问题。到目前为止,我在网上看到的代码使用ArrayAdapter,他们只使用简单的listView而不是自定义的代码。Android - 添加ArrayList中的项目后动态更新自定义ListView

下面是相关文件:

list_row_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
    android:id="@+id/variant" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:text="variant" /> 

    <TextView 
    android:id="@+id/quantity" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:text="quantity" /> 

    <TextView 
    android:id="@+id/unit" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_marginRight="221dp" 
    android:layout_toLeftOf="@+id/quantity" 
    android:text="unit" /> 

</RelativeLayout> 

这里是我的activity_order_form.xml有ListView控件元素的一部分。

<RelativeLayout 
    android:id="@+id/relativeLayout3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/relativeLayout2" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textViewVariantB" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="94dp" 
     android:text="Variant" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textViewUnit" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginRight="123dp" 
     android:text="Unit" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <ListView 
     android:id="@+id/listViewProductOrder" 
     android:layout_width="match_parent" 
     android:layout_height="350dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/textViewVariantB" > 

    </ListView> 
</RelativeLayout> 

这里是存储ArrayList的类。

public class CurrentOrderClass { 

    private String productName; 

    //ArrayLists 
    private ArrayList<String> variantArray = new ArrayList<String>(); 
    private ArrayList<String> unitArray = new ArrayList<String>(); 
    private ArrayList<Integer> quantityArray = new ArrayList<Integer>(); 


    //TODO ArrayList functions 
    public ArrayList<String> getUnitArray() { 
    return unitArray; 
    } 

    public void setUnitArray(ArrayList<String> unitArray) { 
    this.unitArray = unitArray; 
    } 

    public void addToUnitArray(String unit){ 
    this.unitArray.add(unit); 
    } 



    public ArrayList<Integer> getQuantityArray() { 
    return quantityArray; 
    } 

    public void setQuantityArray(ArrayList<Integer> quantityArray) { 
    this.quantityArray = quantityArray; 
    } 

    public void addToQuantityArray(int quantity){ 
    this.quantityArray.add(quantity); 
    } 



    public ArrayList<String> getVariantArray() { 
    return variantArray; 
    } 

    public void setVariantArray(ArrayList<String> variantArray) { 
    this.variantArray = variantArray; 
    } 

    public void addToVariantArray(String variantArray){ 
    this.variantArray.add(variantArray); 
    } 
} 

这里是CustomListAdapter.java文件

public class CustomListAdapter extends BaseAdapter { 

    private ArrayList<CurrentOrderClass> listData; 

    private LayoutInflater layoutInflater; 

    public CustomListAdapter(Context context, ArrayList<CurrentOrderClass> listData) { 
    this.listData = listData; 
    layoutInflater = LayoutInflater.from(context); 
    } 

    @Override 
    public int getCount() { 
    return listData.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
    return listData.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
    return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    if (convertView == null) { 
     convertView = layoutInflater.inflate(R.layout.list_row_layout, null); 
     holder = new ViewHolder(); 
     holder.variantView = (TextView) convertView.findViewById(R.id.variant); 
     holder.unitView = (TextView) convertView.findViewById(R.id.unit); 
     holder.quantityView = (TextView) convertView.findViewById(R.id.quantity); 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    holder.variantView.setText(listData.get(position).getVariantArray().get(position).toString()); 
    holder.unitView.setText(listData.get(position).getUnitArray().get(position).toString()); 
    holder.quantityView.setText(String.valueOf(listData.get(position).getQuantityRow())); 

    return convertView; 
} 

    static class ViewHolder { 
    TextView variantView; 
    TextView unitView; 
    TextView quantityView; 
    } 


} 

这是我OrderForm.java活动的一部分,这说明的onCreate和填充ListView控件的方法,还有部分用户在那里输入被采取。

public class OrderForm extends Activity { 

    public TextView tv; 
    private int variantPosition; 
    CustomListAdapter customListAdapter; 
    CurrentOrderClass currentOrder = new CurrentOrderClass(); 

    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_order_form); 
      tv = (TextView)findViewById(R.id.textViewProduct); 

      //set variants here 
      popolateItem(); 

      //set current order listview here 
      ArrayList image_details = getListData(); 
      final ListView lv1 = (ListView) findViewById(R.id.listViewProductOrder); 

      customListAdapter = new CustomListAdapter(this, image_details); 
      lv1.setAdapter(customListAdapter); 

    } 

    private ArrayList getListData() { 

      ArrayList results = new ArrayList(); 

      for(int i = 0; i < 10; i++){ 
     currentOrder.getQuantityArray().add(i); 
     currentOrder.getUnitArray().add("Sample text here." + i); 
     currentOrder.getVariantArray().add("Another sample text here" + i); 
     results.add(currentOrder); 
      } 
      return results; 

    } 

     //....snip snip excess code..... 
     //This is the part wherein I add new items to the ArrayList in the class 
     //After this, I'm not sure how to proceed 


    currentOrder.setProductName(product.getName()); 
    currentOrder.addToQuantityArray(newQuantity); 
    currentOrder.addToUnitArray(product.getUnit()[position]); 
    currentOrder.addToVariantArray(product.getVariant()[variantPosition]); 

    Log.d("Angelo", currentOrder.getProductName() + " " 
      + currentOrder.getQuantityArray() + " " 
      + currentOrder.getUnitArray() + " " 
      + currentOrder.getVariantArray()); 


} 

我也尝试在日志条目之后放置customListAdapter.notifyDataSetChanged();,但没有任何事情发生。任何想法的人?谢谢。

编辑后的答案和修改。

只需按照FD的答案,添加他在那里指定的函数和之后的2个函数调用。现在,您希望修改getListData()功能是这样的:

private ArrayList getListData() { 

    ArrayList results = new ArrayList(); 

    int loopUntil; 

    /* Two scenarios 
    * First ---> the user started the activity for the first time, which means that the pre-populated list is what we want 
    *   if this is the case, look at the else part of the boolean statement, it only loops until 10 
    *   and in addition, populates those elements 
    * Second ---> the user has set something already and we want to populate until that element. 
    * 
    */ 


    if(currentOrder.getQuantityArray().size() > 10){ 
     loopUntil = currentOrder.getQuantityArray().size(); 

     for(int i = 0; i < loopUntil; i++){ 
      currentOrder.getQuantityArray(); 
      currentOrder.getUnitArray(); 
      currentOrder.getVariantArray(); 
      results.add(currentOrder); 
     } 

    } 
    else{ 
     loopUntil = 10; 

     for(int i = 0; i < loopUntil; i++){ 
      currentOrder.getQuantityArray().add(i); 
      currentOrder.getUnitArray().add("Sample text here." + i); 
      currentOrder.getVariantArray().add("Another sample text here" + i); 
      results.add(currentOrder); 
     } 
    } 

    return results; 

} 

第一个条件,当没有超出预填充的一个项目,因为else语句循环只有等到预填充的项目(项目编号执行9,索引0)。此循环确保您添加到ArrayList的所有内容都被添加到ListView中。

当活动第一次打开时,第二个条件会执行,除了您自己填充的那些列表之外,该列表中没有任何内容。

+0

我没有看到你传递你的数据适配器,除非即时弄错 – kabuto178

+0

后CustomListAdapter – Blackbelt

+0

道歉是失误,编辑。 – Razgriz

回答

5

您的适配器无法获得新数据,因为您正在使用其自己的一组数据进行初始化。

一种可能性是实例化一个新的适配器并将其分配给ListView。

您的ListView添加您的活动现场:

public TextView tv; 
private int variantPosition; 
CustomListAdapter customListAdapter; 
CurrentOrderClass currentOrder = new CurrentOrderClass(); 
ListView myListView; //Listview here 

在的onCreate,设置myListView指向您的ListView:

final ListView lv1 = (ListView) findViewById(R.id.listViewProductOrder) 
myListView = lv1; 

最后,当你改变你的数据,创建一个新的适用于新数据的适配器:

myListView.setAdapter(new CustomListAdapter(this, getListData()); 

或者,修改您的自定义适配器以包含as etListData方法:

public class CustomListAdapter extends BaseAdapter { 

    private ArrayList<CurrentOrderClass> listData; 

    private LayoutInflater layoutInflater; 

    public CustomListAdapter(Context context, ArrayList<CurrentOrderClass> listData) { 
    this.listData = listData; 
    layoutInflater = LayoutInflater.from(context); 
    } 

    public void setListData(ArrayList<CurrentOrderClass> data){ 
    listData = data; 
    } 

    @Override 
    public int getCount() { 
    return listData.size(); 
    } 


    @Override 
    public Object getItem(int position) { 
    return listData.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
    return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    if (convertView == null) { 
     convertView = layoutInflater.inflate(R.layout.list_row_layout, null); 
     holder = new ViewHolder(); 
     holder.variantView = (TextView) convertView.findViewById(R.id.variant); 
     holder.unitView = (TextView) convertView.findViewById(R.id.unit); 
     holder.quantityView = (TextView) convertView.findViewById(R.id.quantity); 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    holder.variantView.setText(listData.get(position).getVariantArray().get(position).toString()); 
    holder.unitView.setText(listData.get(position).getUnitArray().get(position).toString()); 
    holder.quantityView.setText(String.valueOf(listData.get(position).getQuantityRow())); 

    return convertView; 
} 

    static class ViewHolder { 
    TextView variantView; 
    TextView unitView; 
    TextView quantityView; 
    } 


} 

然后,修改数据后,只要致电:

customListAdapter.setListData(getListData()); 
customListAdapter.notifyDataSetChanged(); 
+0

嗯。这似乎是我“刷新”listView。我想要做的就是直接将用户输入添加到listView中,并使用'notifyDataSeChanged'刷新listView。我也更新了我的问题,现在它包含我的'CustomListAdapter'类。 – Razgriz

+1

我相应地修改了我的代码。 –

+0

'getListData()'只循环到第10个项目,我将对其进行修改并发布我为使代码正常工作所做的修改。我会将此标记为正确的答案。 – Razgriz

相关问题