当我的TextView
事件触发时,我正在尝试更新我的onItenClick
,我有几个TextView,它们在我的行模板中创建了我想要的数量,但这里出现问题,当我尝试设置这个TextView
值,它实际上是设置的,但另一个textview,也设置了相同的值,我知道这个行为的原因,但不知道如何解决它。
我的想法:
getRowView
方法(当我滚动列表时),我看到了
不寻常的行为,一旦我为我的其中一个设定了一些价值,它
不断更改获得此值的文本视图。所以我想这是
问题的核心。PS:我正在使用组件HeaderListView,如果你得到另一个框架建议来做同样的事情,我真的很感激。
PS2:这是我的代码:
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import br.com.soutsapp.souts.R;
import br.com.soutsapp.souts.model.Menu;
import br.com.soutsapp.souts.model.Product;
import br.com.soutsapp.souts.model.modelview.OrderItem;
import br.com.soutsapp.souts.userInterface.activity.SectionAdapter;
import br.com.soutsapp.souts.userInterface.adapter.HeaderListView;
public class MenuFragment extends Fragment {
private Context mContext;
private List<OrderItem> itens;
private Menu menu;
public MenuFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_menu, container, false);
mContext = getContext();
itens = new ArrayList<>();
menu = new Menu();
final String[] sec1 = {"aaaaaaaa","aaaaaaaa","aaaaaaaa","aaaaaaaa","aaaaaaaa","aaaaaaaa","aaaaaaaa"};
final String[] sec2 = {"bbbbbbbb","bbbbbbbb","bbbbbbbb","bbbbbbbb","bbbbbbbb","bbbbbbbb","bbbbbbbb"};
final String[] sec3 = {"cccccccc","cccccccc","cccccccc","cccccccc","cccccccc","cccccccc","cccccccc"};
final String[] sec4 = {"dddddddd","dddddddd","dddddddd","dddddddd","dddddddd","dddddddd","dddddddd"};
HeaderListView lv = (HeaderListView) v.findViewById(R.id.lv_menu_items);
lv.setAdapter(new SectionAdapter() {
@Override
public int numberOfSections() {
return menu.getMenuSessions().size();
}
@Override
public int numberOfRows(int section) {
return menu.getMenuSessions().get(section).size();
}
@Override
public Object getRowItem(int section, int row) {
return menu.getMenuSessions().get(section).get(row);
}
@Override
public boolean hasSectionHeaderView(int section) {
return true;
}
@Override
public View getRowView(int section, int row, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(getResources().getLayout(R.layout.menu_item_row), null);
}
//Product product = (Product)getRowItem(section, row);
//convertView.setId(product.getId());
TextView tvMenuItemName = (TextView) convertView.findViewById(R.id.tv_menu_item_name);
TextView tvMenuItemPrice = (TextView) convertView.findViewById(R.id.tv_price);
String itemName = ((Product)getRowItem(section,row)).getName();
tvMenuItemName.setText(itemName);
String itemPrice = String.valueOf(((Product) getRowItem(section, row)).getPrice());
tvMenuItemPrice.setText(itemPrice);
final int mySection = section;
final int myRow = row;
final TextView tvQuantity = (TextView) convertView.findViewById(R.id.tv_quantity);
tvQuantity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Product product = (Product) getRowItem(mySection, myRow);
if (tvQuantity.getText().equals("1") && !tvQuantity.getText().equals("")){
tvQuantity.setText("");
OrderItem itenToRemove = null;
for(OrderItem item: itens){
if(item.getProductId() == product.getId()){
itenToRemove = item;
break;
}
}
itens.remove(itenToRemove);
}
else{
for(OrderItem item: itens){
if(item.getProductId() == product.getId()){
int quantity = item.getQuantity();
item.setQuantity(--quantity);
tvQuantity.setText(String.valueOf(item.getQuantity()));
break;
}
}
}
}
});
return convertView;
}
@Override
public int getSectionHeaderViewTypeCount() {
return 1;
}
@Override
public int getSectionHeaderItemViewType(int section) {
return section % 1;
}
@Override
public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = (TextView) getActivity().getLayoutInflater().inflate(getResources().getLayout(android.R.layout.simple_list_item_1), null);
}
if (getSectionHeaderItemViewType(section) == 0) {
((TextView) convertView).setText("Header for section " + section);
} else {
((TextView) convertView.findViewById(android.R.id.text1)).setText("Header for section " + section);
}
switch (section) {
case 0:
convertView.setBackgroundColor(getResources().getColor(R.color.Blue_Jay));
break;
case 1:
convertView.setBackgroundColor(getResources().getColor(R.color.Yellow));
break;
case 2:
convertView.setBackgroundColor(getResources().getColor(R.color.Red_Wine));
break;
}
return convertView;
}
@Override
public void onRowItemClick(AdapterView<?> parent, View view, int section, int row, long id) {
super.onRowItemClick(parent, view, section, row, id);
//Product p = (Product)view.getTag();
TextView tvQuantity = (TextView) view.findViewById(R.id.tv_quantity);
Product p = menu.getMenuSessions().get(section).get(row);
//Product p = (Product) getRowItem(section, row);
boolean exist = false;
for(OrderItem item : itens){
if(item.getProductId() == p.getId()){
int actualQuantity = item.getQuantity();
item.setQuantity(++actualQuantity);
tvQuantity.setText(String.valueOf(item.getQuantity()));
exist = true;
}
}
if(!exist){
itens.add(new OrderItem(p.getId(), 1));
tvQuantity.setText("1");
}
}
});
return v;
}
}
在屏幕截图中,我触摸了“Smirnoff剂量”项目,“Smirnoff剂量”和“Água”的文本视图值都发生了变化。