0

カスタムアダプターと行レイアウトを使用してオブジェクトの配列が移入されたリストビューがあります。行にはオブジェクトの名前、価格が表示され、数量を変更するためのボタンが含まれ、選択した数量に基づいて合計が表示されます。ListViewの特定の行にアクセスし、その行の内容を操作する方法はありますか?

Example of list populated with 3 items

ここで問題です:私は、ユーザーが2行目で数量]ボタンをクリックしたときに、たとえば、私はその同じ行に価格と数量の値にアクセスしたい、というリストビューを伝えたいです、&は、その特定の行の最後にあるセル全体に結果を表示します。私はそれを行う方法を理解することはできません。私は、ボタン要素がXMLレイアウト内に単一のIDを持つので混乱しています。たとえば、2行目のボタンと1行目または最後の行のボタンは、重複しているので区別できますか?

私は必要な数学演算を実行するOnClickListenerメソッドの構文を知っていますが、プログラムのどこに実装する必要があるのか​​分かりません。私はアダプターで試しましたが、リストの最後の行のボタンだけが正しく機能していますが、それらのすべてではありません。私は何日もの間、検索して、グーグルグーグルをしてきました。私はさまざまなアプローチを見てきましたが、私はそれらを実装するのに苦労しています。

ListView.getChildAt(index)メソッドは、インデックスに基づいてリスト内の特定の行を返す必要がありましたが、どのボタンを基準にして行のインデックスを取得するのですかクリックした? This is the stackoverflow post where I read on the .getChildAt(index) method

私は以下の私のプロジェクトのためのコードを提供します:アダプタの

public class ShopActivity extends AppCompatActivity { 

ListView listView; 
ItemListAdapter adapter; 
ArrayList<ShoppingItem> shoppingItems = new ArrayList<ShoppingItem>(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.shop_activity); 

    shoppingItems.add(new ShoppingItem("Drink", "Water Bottle 1.5 ltr", 11)); 
    shoppingItems.add(new ShoppingItem("Drink", "Pepsi 2 ltr", 10)); 
    shoppingItems.add(new ShoppingItem("Drink", "Orange Juice 1.5 ltr", 7)); 

    listView = (ListView) findViewById(R.id.itemListView); 
    adapter = new ItemListAdapter(this, R.layout.item_layout, shoppingItems); 
    listView.setAdapter(adapter); 
} 

コード:ShopActivity.javaため

コード

public class ItemListAdapter extends ArrayAdapter<ShoppingItem> { 

private ArrayList<ShoppingItem> items; 
private int layoutResourceId; 
private Context context; 
ItemHolder holder = new ItemHolder(); 

public ItemListAdapter(Context context, int layoutResourceId, ArrayList<ShoppingItem> items) { 
    super(context, layoutResourceId, items); 
    this.layoutResourceId = layoutResourceId; 
    this.context = context; 
    this.items = items; 
} 

public static class ItemHolder { 
    public ShoppingItem shoppingItem; 
    public TextView itemName; 
    public TextView itemPrice; 
    public TextView itemQuantity; 
    public TextView totalPriceNumber; 
    public ImageButton plusButton; 
    public ImageButton minusButton; 
} 

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

@Override 
public ShoppingItem getItem(int position) { 
    return items.get(position); 
} 

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

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 

    View rowView = convertView; 

    if (convertView == null) { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     rowView = inflater.inflate(layoutResourceId, parent, false); 

     TextView itemName = (TextView) rowView.findViewById(R.id.itemName); 
     TextView itemPrice = (TextView) rowView.findViewById(R.id.itemPrice); 
     TextView itemQuantity = (TextView) rowView.findViewById(R.id.itemQuantity); 
     TextView totalPriceNumber = (TextView) rowView.findViewById(R.id.totalPrice); 
     ImageButton plusButton = (ImageButton) rowView.findViewById(R.id.plusButton); 
     ImageButton minusButton = (ImageButton) rowView.findViewById(R.id.minusButton); 
     holder.itemName = itemName; 
     holder.itemPrice = itemPrice; 
     holder.itemQuantity = itemQuantity; 
     holder.totalPriceNumber = totalPriceNumber; 
     holder.plusButton = plusButton; 
     holder.minusButton = minusButton; 

     rowView.setTag(holder); 

    } else 
     holder = (ItemHolder) rowView.getTag(); 

    holder.shoppingItem = items.get(position); 

    holder.itemName.setText(holder.shoppingItem.getItemName()); 
    holder.itemPrice.setText(Double.toString(holder.shoppingItem.getItemPrice())); 
    holder.itemQuantity.setText(Integer.toString(holder.shoppingItem.getQuantity())); 
    holder.totalPriceNumber.setText(Double.toString(holder.shoppingItem.getItemPrice() * holder.shoppingItem.getQuantity())); 

    final int rowPosition = position; 
    holder.plusButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      holder.shoppingItem = items.get(rowPosition); 
      int newQuantity = holder.shoppingItem.getQuantity(); 
      newQuantity++; 
      holder.shoppingItem.setQuantity(newQuantity); 

      holder.itemQuantity.setText(Integer.toString(newQuantity)); 
      holder.totalPriceNumber.setText(Double.toString(holder.shoppingItem.getItemPrice() * holder.shoppingItem.getQuantity())); 
      notifyDataSetChanged(); 
     } 
    }); 

    return rowView; 
} 

コードの行についてXML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:padding="5dp"> 

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

    <TableRow> 

     <CheckBox 
      android:layout_width="25dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:gravity="center" /> 

     <ImageView 
      android:id="@+id/imageBar" 
      android:layout_width="50dp" 
      android:layout_height="75dp" 
      android:layout_gravity="center" 
      android:src="@drawable/drink" /> 

     <TextView 
      android:id="@+id/itemName" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="3" 
      android:width="0dp" /> 

     <TextView 
      android:id="@+id/itemPrice" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:width="0dp" 
      android:gravity="center" /> 

     <ImageButton 
      android:id="@+id/minusButton" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="0.3" /> 

     <TextView 
      android:id="@+id/itemQuantity" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="0.4" 
      android:width="0dp" 
      android:gravity="center" /> 

     <ImageButton 
      android:id="@+id/plusButton" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="0.3" /> 

     <TextView 
      android:id="@+id/totalPrice" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:width="0dp" 
      android:gravity="center" /> 
    </TableRow> 
</TableLayout> 

ShoppingItemオブジェクトのためのコード:

public class ShoppingItem { 
private String itemType; 
private String itemName; 
private double itemPrice; 
private int quantity = 1; 

public ShoppingItem(String itemType, String itemName, double itemPrice, int quantity) { 
    this.itemType = itemType; 
    this.itemName = itemName; 
    this.itemPrice = itemPrice; 
    this.quantity = quantity; 
} 

public String getItemType() { 
    return this.itemType; 
} 

public void setItemType(String itemType) { 
    this.itemType = itemType; 
} 

public String getItemName() { 
    return this.itemName; 
} 

public void setItemName(String itemName) { 
    this.itemName = itemName; 
} 

public double getItemPrice() { 
    return this.itemPrice; 
} 

public void setItemPrice(double itemPrice) { 
    this.itemPrice = itemPrice; 
} 

public int getQuantity() { 
    return this.quantity; 
} 

public void setQuantity(int quantity) { 
    this.quantity = quantity; 
} 

を私はAndroidの開発シーンに新たなんです。

+0

は 'holder.amountButton.setFocusable(false)を追加;'あなたのgetViewメソッドアダプタ方法で。 – grabarz121

+0

@ grabarz121私はそれを試みたが、残念ながらそれは動作しませんでした。それでも、最後の行だけが正しい合計を表示し、すべての行は変更を加えません。 if(convertView == null)セクションのコード行を追加すると、何かを明確にすることができます。私が間違っていることは何ですか?いずれにせよ私はあなたのフィードバックを感謝します。 – MotorByte

+0

あなたの 'com.cepheuen.elegantnumberbutton.view.ElegantNumberButton'は' android:clickable = "false" 'というプロパティを持っていますので、この行を削除すればうまくいきます。実際のプロジェクトでも同様のことを使っていますが、これはうまくいきます。 – grabarz121

答えて

1

各行はデータセットを表します。たとえば、アイテムのデータクラスがあるとします。

data class Item(name: String, price: Double)

、プラスボタンのonClickの数量quantity: Int

ためのフィールドを追加するアイテムのquantity変数をインクリメントします。このような例:

// data set is something like this... 
val dataSet = mutableListOf<Item>() 

// creating the view for row i... 
plusButton.onClick { 
    dataSet[i].quantity++ 
    notifyItemChanged(i) // dont think this exists for ListView, but you can also just use notifyDataSetChanged, which will invalidate all rows 
} 

量をインクリメントした後、あなたは変更行のnotifyItemChanged方法をトリガする、これは、あなたの現在のデータセットを表現する行のUI更新をトリガします。

あなたはgetView方法でonClickリスナーを追加し、代わりにあなたのデータセット内のアイテムにアクセスするためのi使用positionの必要があります。

EDIT:あなたのgetView()方法で

このような何かを試してみてください。

final int rowPosition = position; 
plusButton.setOnClickListener(new View.OnClickListener(View v) { 
    items.get(rowPosition).quantity++; 
    notifyDataSetChanged(); 
}); 
+0

まず、私の友人のフィードバックに感謝します。あなたがアドバイスしたときに、価格と名前を含むオブジェクトクラス内の量インスタンス変数を追加しました。問題は、アダプターのコードを変更して、plusClickにonClickListenerを追加しようとしたときに、私が与えた構文を書き込めなかったときです。 – MotorByte

+0

Javaで、現在のコードに合った例を追加しました。おそらくこれが役に立ちます。 – damian

+0

私が書いたコードを試しましたが、 ".quantity ++;"の構文は受け入れられません。私はあなたが正しいアイデアを念頭に置いていると思います。最後にgetView()のコードを修正しました。私が使用しているShoppingItemオブジェクトのコードも追加しました。残念ながら、これらの変更ではまだ動作しません。私がplusButtonを押すと数量は増えません。なぜそれができないのか分かりますか? – MotorByte

関連する問題