2

ArrayList から取得した最初に設定されたカスタム ListView を表示するアクティビティがあり、ユーザーはこのリストに追加できます。これを問題なく保存できます。追加したアイテムの表示に問題があります。これまでのところ、オンラインで見たコードは ArrayAdapter を使用しており、カスタムのリストビューではなく単純なリストビューのみを使用しています。

関連ファイルは次のとおりです。

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 の部分は次のとおりです。

<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 に追加されます。

2 番目の条件は、アクティビティが初めて開かれたときに実行され、そのリストには自分で入力したもの以外はまだ何もないはずです。

4

1 に答える 1

5

アダプターは、独自のデータ セットで初期化しているため、新しいデータを取得しません。

1 つの可能性は、新しいアダプターをインスタンス化し、それを 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());

または、カスタム アダプタを変更して setListData メソッドを含めます。

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();
于 2013-07-30T07:49:08.983 に答える