从 View Holder 传递值

Pass value from View Holder

我有 Activity DishList,我使用 ListView 列出项目和按钮转到下一个 activity。我使用适配器 class DishListAdapter 那里我有 ViewHolder class 保存 TextView、ImageView 等所以当我单击 DishList 中的按钮时它应该从 ViewHolder 获取值 并将其传递给下一个 activity.

菜品清单Activity

    public class DishesList extends ApplicationMenu implements TextWatcher {

public String cat_id,name;
EditText searchDish;
Button checkout;
ListView dishesList;
DishListBaseAdapter adapter;
DishListBaseAdapter.ViewHolder holder= new DishListBaseAdapter.ViewHolder();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dishes_main);

checkout=(Button)findViewByID(R.iD.checkout);
checkout.setOnclickListener(new OnClickListener){
public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
            Intent ourIntent = new Intent(DishesList.this, OrderActivity.class);
            DishesList.this.startActivity(ourIntent);
        }
    });

    //  top order bar
    RelativeLayout top_bar = (RelativeLayout) findViewById(R.id.top_order_bar);
    TextView top_order_txt = (TextView) findViewById(R.id.text_order);

    top_bar.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
            Intent ourIntent = new Intent(DishesList.this, Order.class);
            DishesList.this.startActivity(ourIntent);
        }
    });

    if(OrderStore.getCart().isEmpty())
    {

    }
    else
    {
        top_order_txt.setText(OrderStore.getCart().size()+" item(s) added.");
        top_bar.setVisibility(0);
    }



    ArrayList<DishDetails> dish_details = GetSearchResults();

    final Bundle gotBasket = getIntent().getExtras();
    name = gotBasket.getString("name");

    RelativeLayout backBtn = (RelativeLayout) findViewById(R.id.backBtn);
    ImageView homeBtn = (ImageView) findViewById(R.id.homeBtn);
    TextView headerTxt = (TextView) findViewById(R.id.headertext);
    ImageView seperator=(ImageView) findViewById(R.id.sperator);
    seperator.setVisibility(View.GONE);

    dishesList = (ListView) findViewById(R.id.dishesList);
    adapter = new DishListBaseAdapter(this, dish_details,"normal",gotBasket.getString("name"),gotBasket.getString("id")); 

    headerTxt.setText(name);

    backBtn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
            Intent ourIntent = new Intent(DishesList.this, CategoriesList.class);
            DishesList.this.startActivity(ourIntent);
        }
    });
    homeBtn.setImageResource(R.drawable.place_order);
    homeBtn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
        System.out.println(gotBasket.getString("name"));
            finish();
            Intent i = new Intent(DishesList.this, OrderActivity.class);
            i.putExtra("name", gotBasket.getString("name"));
            DishesList.this.startActivity(i);
        }
    });

    if(dish_details.size() > 0)
    {

          dishesList.setAdapter(adapter);
    }
    else
    {
        Toast.makeText(getApplicationContext(), "No Dishes Found!", Toast.LENGTH_LONG).show();
    }


    searchDish = (EditText) findViewById(R.id.search_dish);
    searchDish.addTextChangedListener(this);
}

public void onDestroy()
{
    dishesList.setAdapter(null);
    super.onDestroy();
}

public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
    // TODO Auto-generated method stub

}

public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub

}

public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
    String text = searchDish.getText().toString().toLowerCase();
    adapter.filter(text);
}

public ArrayList<DishDetails> GetSearchResults(){
    ArrayList<DishDetails> results = new ArrayList<DishDetails>();
    Bundle gotBasket = getIntent().getExtras();
    cat_id = gotBasket.getString("id");


    }

DishListBase 适配器

      ublic class DishListBaseAdapter extends BaseAdapter {
private ArrayList<DishDetails> itemDetailsArrayList = null;;
private ArrayList<DishDetails> itemDetailsArrayOriginal = null;

 public ImageLoader imageLoader; 
private LayoutInflater l_Inflater;
private String activityType;
private String catName;
private String catID;
int count=0;





public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;


    if (convertView == null) {
        convertView = l_Inflater.inflate(R.layout.dish, null);
        holder = new ViewHolder();
        holder.check_out=(Button) convertView.findViewById(R.id.checkout);
        holder.txt_itemName = (TextView) convertView.findViewById(R.id.name);
        holder.txt_itemPrice = (TextView) convertView.findViewById(R.id.price);
        holder.itemImage = (ImageView) convertView.findViewById(R.id.photo);
        holder.quickAdd = (ImageView) convertView.findViewById(R.id.quickAdd);
        holder.quickRemove=(ImageView) convertView.findViewById(R.id.quickRemove);
        holder.txt_item=(TextView) convertView.findViewById(R.id.item);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }


    final Integer dish_id = itemDetailsArrayList.get(position).getDishID();
    final DishDetails obj = itemDetailsArrayList.get(position);


    holder.quickAdd.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            ViewGroup parent = (ViewGroup) v.getParent();
                TextView tv = (TextView) parent.findViewById(R.id.item);

                int count = Integer.parseInt((String) holder.txt_item.getText());
                count++;
                holder.txt_item.setText(String.valueOf(count));


        }
    });
    holder.quickRemove.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
             ViewGroup parent = (ViewGroup) v.getParent();
                TextView tv = (TextView) parent.findViewById(R.id.item);

                int count = Integer.parseInt((String) holder.txt_item.getText());

                if (count > 0) {
                    count--;
                    holder.txt_item.setText(String.valueOf(count));

                } 

        }
    });



    holder.txt_itemName.setText(itemDetailsArrayList.get(position).getDishName());
    holder.txt_itemPrice.setText(itemDetailsArrayList.get(position).getDishPrice() + " PKR");
    String thumburl = URLConnectionReader.getMediaIP()+"uploads/dish/" + itemDetailsArrayList.get(position).getDishImage();

            return convertView;
}

static class ViewHolder {
    TextView txt_itemName;
    TextView txt_itemPrice;
    ImageView itemImage;
    ImageView quickAdd;
    ImageView quickRemove;
    TextView txt_item;
    Button check_out;

}

然后创建一个 hashmap,每当发生更新时,将数据保存在 hashmap 中。

  • 继承适配器中的 getItem(int index) 方法,HashMap as return 类型 & 和 return 该方法中对应索引的数据。

    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    
    @Override
    public HashMap getItem(int i) {
    return hashMap.get(i);
    }
    
  • 就像如果用户在 edittext 中键入内容,然后为该按钮使用 textChangeListener & onTextChange 将数据保存在 hashmap 中以用于相应的索引。

    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
        }
    
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    hashMap.put(position, editText.getText().toString());
        }
    
        @Override
        public void afterTextChanged(Editable editable) {
    
        }
    });
    

然后在activity按钮的onClick中写下面的代码。

for (int i = 0; i < listView.getAdapter().getCount(); i++) {
        HashMap hashMap = (HashMap) listView.getAdapter().getItem(i);
        }