0

我有一個使用自定義適配器和行佈局填充對象數組的列表視圖。該行顯示對象的名稱,價格,包含用於更改數量的按鈕,並根據所選數量顯示總計。如何訪問ListView中的特定行並對該行的內容進行操作?

Example of list populated with 3 items

這裏的問題:我想告訴列表視圖,例如,當用戶點擊第二行中的數量按鈕,我想訪問同一行中的價格和數量值,&將結果顯示在特定行末尾的總單元格中。我無法弄清楚如何做到這一點。我很困惑,因爲按鈕元素在XML佈局中有一個ID,所以我怎麼區分第二行中的按鈕與第一行或最後一行中的按鈕,因爲它們都是重複的?

我知道OnClickListener方法的語法,它將執行我需要的數學運算,我只是不知道應該在程序的哪個位置執行它。我嘗試過在適配器中,但只有列表中最後一行的按鈕正常工作,而不是全部。我一直在搜索和谷歌搜索幾天,我看到了不同的方法,但我正在努力實施它們。

我最好的領導是關於一個名爲ListView.getChildAt(index)的方法,它應該基於索引返回列表中的特定行,但是如何根據哪個按鈕獲得行的索引點擊?該適配器

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

代碼:This is the stackoverflow post where I read on the .getChildAt(index) method

我給你提供的代碼下面我的項目:

代碼爲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(假);'在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)

的數量quantity: Int

添加一個字段中onClick的加號按鈕,增加該項目的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來訪問數據集中的項目。

編輯:

在你getView()方法嘗試這樣的事:

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

首先讓我感謝你的反饋我的朋友。我按照你的建議做了,並在包含價格和名稱的對象類中添加了數量實例變量。問題是當我試圖修改適配器中的代碼並將onClickListener添加到plusButton時,我無法寫出你給我的語法。 – MotorByte

+0

我已經在Java中添加了一個適合更多當前代碼的示例,這可能會有所幫助。 – damian

+0

我試過你寫過的代碼,但它不接受「.quantity ++;」的語法。我認爲你有正確的想法,我在getView()中修改了上面的代碼,並且還爲我使用的ShoppingItem對象添加了代碼。不幸的是,這些更改仍然無效。當我點擊plusButton時,數量不會增加。你有什麼想法,爲什麼它不會? – MotorByte

相關問題