从View Holder传递值

时间:2016-02-03 07:08:03

标签: android listview android-viewholder

我有Activity DishList,我使用ListView列出项目和按钮转到下一个活动。我使用适配器类DishListAdapter,我有ViewHolder类,它包含TextView,ImageView等,所以当我点击DishList中的按钮时,它应该从ViewHolder中获取值 并将其传递给下一个活动。

DishList活动

    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;

}

1 个答案:

答案 0 :(得分:0)

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

  • 在适配器中使用HashMap继承getItem(int index)方法作为返回类型&amp;并返回该方法中的数据以获得相应的索引。

    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    
    @Override
    public HashMap getItem(int i) {
    return hashMap.get(i);
    }
    
  • 如果用户在edittext中输入内容,则使用textChangeListener作为该按钮&amp; 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) {
    
        }
    });
    

然后在代码下面的活动中点击onClick按钮。

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