停止列表视图以更新以前的项目并仅更新当前项目

时间:2014-04-22 10:50:12

标签: android listview refresh

我是新来的。就在几个月前我开始学习android,我想创建listview,就像父母&子关系,当点击子值中的父值时,我应该修改。我做到了。但问题是,当我点击第一个父母时,它正常工作,而点击另一个,所有值都会刷新。

我的代码

//int slno = 1;
int rs;
int Pcount =1;
int finalrs;
String  IP_Address, url, TableNo, itmeName;

private String ITEM_NAME = "ItemName";
private String ITEM_PRICE = "price";

int in,  totalqut ,  totalv;

 String strvalp;


//public Activity activity;

//Initialize variables
private static final String STR_CHECKED = " has Checked!";
private static final String STR_UNCHECKED = " has unChecked!";
private int ParentClickStatus=-1;
private int ChildClickStatus=-1;
private ArrayList<Parent> parents;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    Resources res = this.getResources();
    Drawable devider = res.getDrawable(R.drawable.line);    



    Intent intent = getIntent();         
    IP_Address  = intent.getStringExtra("IP_Address");
    TableNo  = intent.getStringExtra("TableNo");
    itmeName =  intent.getStringExtra("button_text");


    Log.e("log_tag"," I am in Ite List Pages ");  
    Log.e("log_tag"," IP Address : " +IP_Address);         
    Log.e("log_tag", " Table Number : " +TableNo);
    Log.e("log_tag", " Manu lict clicked : " +itmeName);

    url= IP_Address;

    // Set ExpandableListView values

    getExpandableListView().setDivider(devider);
    getExpandableListView().setChildDivider(devider);
    getExpandableListView().setDividerHeight(1);
    getExpandableListView().setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
    registerForContextMenu(getExpandableListView());



    //Creating static data in arraylist
    final ArrayList<Parent> dummyList = buildDummyData();

    // Adding ArrayList data to ExpandableListView values
    loadHosts(dummyList);
}



private ArrayList<Parent> buildDummyData()
{
    Log.e("log_tag"," 1 "); 
    // TODO Auto-generated method stub
    final ArrayList<Parent> list = new ArrayList<Parent>();
    // Creating ArrayList of type parent class to store parent class objects

    String result1= main_menu_list();   
    try
    {               

        JSONArray jArray1 = new JSONArray(result1);             

        Log.e("log_tag", "JSON Array length  "+jArray1.length());           
        for (int i = 1; i < jArray1.length(); i++)
        {
            //Create parent class object
            final Parent parent = new Parent();

            // JSON object parsing  

            JSONObject json_data = jArray1.getJSONObject(i);


            String itemnamej = json_data.getString("product_name");
            String itempricej = json_data.getString("price");

            // Set values in parent class object
            parent.setSlno(i+".");                                         
            parent.setItemname(itemnamej);
            parent.setPrice(itempricej );   


            parent.setChildren(new ArrayList<Child>());
            // Create Child class object 
            final Child child = new Child();
            child.setPrice(itempricej);
            child.setQut("1");

            //Add Child class object to parent class object
            parent.getChildren().add(child);



            //Adding Parent class object to ArrayList         
            list.add(parent);
        }

    }
    catch(JSONException e)
    {
        Log.e("log_tag", "Error parsing data "+e.toString());
    }        
    return list;
}
private String main_menu_list() 
{
    Log.e("log_tag"," 2  ");
    // TODO Auto-generated method stub
    String result1 = null;

    url= url + "RMS/Sub_Manu.php";
    ArrayList<NameValuePair> postParameters= new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("itmeName",itmeName));

    try 
    { 
        String  response1 = CustomHttpClient.executeHttpPost(url,postParameters);
        result1 = response1.toString();              

    }
    catch (Exception e) 
    {
        Log.e("log_tag","Error in http connection!!" + e.toString());     
    }
    return result1;
}





private void loadHosts(ArrayList<Parent> newParents)
{
    // TODO Auto-generated method stub
    Log.e("log_tag"," 3 ");

    if (newParents == null)
        return;

    parents = newParents;

    // Check for ExpandableListAdapter object
    if (this.getExpandableListAdapter() == null)
    {
        //Create ExpandableListAdapter Object  
        final MyExpandableListAdapter mAdapter = new MyExpandableListAdapter();

        // Set Adapter to ExpandableList Adapter  
        this.setListAdapter(mAdapter);
    }
    else
    {
        // Refresh ExpandableListView data 
        ((MyExpandableListAdapter)getExpandableListAdapter()).notifyDataSetChanged();
    }   

}

static class ViewHolder {
    TextView slno ;
    TextView itemName ;
    TextView price;
    ImageView rupees;
    ImageView plu ;
}

/**
 * A Custom adapter to create Parent view (Used grouprow.xml) and Child View((Used childrow.xml).
 */
private class MyExpandableListAdapter extends BaseExpandableListAdapter
{
    private HashMap<Integer, View> childMap = null;
    private int count = 1;
    private Parent parent = null;
    private LayoutInflater inflater;            
    private ViewHolder holder = null;
    private int mPreviousSelectedBtnId = -1;
    private int mSelectedIndex = -1;

    public MyExpandableListAdapter()
    {
        // Create Layout Inflator
        inflater = LayoutInflater.from(ItemList.this);
        childMap = new HashMap<Integer, View>();
    }


    // This Function used to inflate parent rows view

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, 
            View convertView, ViewGroup parentView)
    {
        //int i=0;
        parent = parents.get(groupPosition);

        if(convertView == null)
        {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.grouprow, parentView,false);
            holder.itemName = (TextView)convertView.findViewById(R.id.ItemName); // title
            holder.price = (TextView)convertView.findViewById(R.id.price); // description
            holder.slno = (TextView)convertView.findViewById(R.id.slno); // description 
            holder.rupees=(ImageView)convertView.findViewById(R.id.rupees); // thumb image
            holder.plu = (ImageView) convertView.findViewById(R.id.plu);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder)convertView.getTag();
        }

        convertView.setId(groupPosition);
        holder.plu.setId(groupPosition);
        holder.itemName.setTag(ITEM_NAME);
        holder.price.setTag(ITEM_PRICE);

        if(parent != null)
        {
            holder.itemName.setText(parent.getItemname());
            holder.price.setText(parent.getPrice());                
            holder.slno.setText(groupPosition+".");
        }

        holder.plu.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) 
            {                   
                Toast.makeText(ItemList.this,"Plus Button is clicked!", Toast.LENGTH_SHORT).show();


                mSelectedIndex = arg0.getId();

                if(mPreviousSelectedBtnId == arg0.getId())
                {
                    count++;
                }
                else
                {
                    count = 2;
                    mPreviousSelectedBtnId = arg0.getId();
                }

                Log.d("Elist", arg0.getId()+"  >>>. Button Clicked  "+count);

                Log.d("Elist", arg0.getId()+"  >>>. Button Parent is  "+(View)arg0.getParent());

                getExpandableListView().expandGroup(arg0.getId());

                View childView = getExpandableListAdapter().getChildView(arg0.getId(), 0, true, getExpandableListView().getChildAt(arg0.getId()), getExpandableListView());


                if(childView != null) 
                {
                    Log.d("HASH code", "In Button Click "+childView.hashCode()+"");
                    TextView qty = (TextView) childView.findViewById(R.id.qut);


                    //************************************************
                    // Here i am try to get parent price value 

                    //Remark :- for the 1st prent it's show current value when i click anothere 

                    TextView topic = (TextView) findViewById(R.id.price);                       

                    String totalprice = topic.getText().toString();

                    //******************************************************
                    Log.e("log_tag","Total Price : " +totalprice);
                    Log.e("log_tag","Total Quaintity : " +qty.getText().toString());
                    Log.e("log_tag","Total Price Value : " +topic.getText().toString());

                    String val = qty.getText().toString();

                    if(qty != null)
                    {

                        //*****************************************************
                        //Here Calculating the price with quantity 
                        qty.setText(count+" ");

                         in = Integer.valueOf(topic.getText().toString());

                        totalqut = Integer.valueOf(val);

                        System.out.println("Total Price after conv int :" + totalqut);

                        totalv= in*totalqut;    

                        System.out.println("After  value : "+ totalv);                          

                        strvalp = String.valueOf(totalv);


                        //***********************************************
                        Log.d("Elist", "Text View changes called "+ qty.getTag(R.id.qut));
                        qty.invalidate(); 
                    }               


                    else
                    {
                        Log.d("Elist", "Text View Null");
                    }

                    ((MyExpandableListAdapter)getExpandableListAdapter()).notifyDataSetChanged();
                }
                else
                {
                    Log.d("Elist", "chilGdView View Null");
                }

            }});

        return convertView;
    }


    // This Function used to inflate child rows view
    @Override 
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
            View convertView, ViewGroup parentView)
    {
        final Parent parent = parents.get(groupPosition);
        final Child child = parent.getChildren().get(childPosition);

        // Inflate childrow.xml file for child rows
        convertView = inflater.inflate(R.layout.childrow, parentView, false);

        // Get childrow.xml file elements and set values
        ((TextView) convertView.findViewById(R.id.TotalPrice)).setText(child.getPrice());
        TextView mQtyText = (TextView) convertView.findViewById(R.id.qut);

        TextView s2 = (TextView) convertView.findViewById(R.id.TotalPrice);

        if(mQtyText!= null)
        {
            if(mSelectedIndex == groupPosition)
            {
                mQtyText.setText(count+""); 
                //*******************************
                //setting the total price values  
                s2.setText(strvalp);    
                //*******************************
            }

            else
            {
                mQtyText.setText("1");  
            }
            mQtyText.setTag(R.id.qut,"TextView "+groupPosition);
        }

        final TextView sl = (TextView) convertView.findViewById(R.id.TotalPrice);
        final TextView qut = (TextView) convertView.findViewById(R.id.qut);
        ImageView image=(ImageView)convertView.findViewById(R.id.mins);         
        Log.d("HASH code", convertView.hashCode()+"");

        final TextView sll = (TextView) findViewById(R.id.price);

        int sllint = Integer.valueOf(qut.getText().toString());

        image.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) 
            {                   
                Toast.makeText(ItemList.this," Mins Button is clicked!", Toast.LENGTH_SHORT).show();

                String quantityval = qut.getText().toString();
                String quantityval2 = sll.getText().toString();
                String totalprice = sl.getText().toString();

                Log.i("Log_tag  value of quantity ",quantityval);
                Log.i("Log_tag  value of totalprice ",totalprice);
                Log.i("Log_tag  value of quantity ",quantityval2);

                int in = Integer.valueOf(sll.getText().toString());
                int in2 = Integer.valueOf(sl.getText().toString());

                if ((in2 != 0) && (count != 0))
                {


                    // ***************************************
                    // Here modify value when clicked minus button 
                    System.out.println("Befor value : "+in); 
                   //in -=in;   

                    int val = in2 - in; 
                    System.out.println("After  value : "+in);

                    sl.setText(String.valueOf(val)); 
                     count--;

                    //****************************************** 
                    //String str = (String.valueOf(in));
                    qut.setText(count+"");
                    if(in == 0)
                    {
                        Log.i("Log_ tag ","If quantit queal to 0");
                        sl.setText("0");
                        qut.setText("0");
                    }


                }

            }
        }); 


        return convertView;
    }


    @Override
    public Object getChild(int groupPosition, int childPosition)
    {
        return getExpandableListView().getChildAt(groupPosition);
    }

    //Call when child row clicked
    @Override
    public long getChildId(int groupPosition, int childPosition)
    {
        /****** When Child row clicked then this function call *******/

        if( ChildClickStatus!=childPosition)
        {
            ChildClickStatus = childPosition;
            Toast.makeText(getApplicationContext(), "$$$Parent :"+groupPosition + "**** Child :"+childPosition , Toast.LENGTH_LONG).show();
        }  

        return childPosition;
    }

    @Override  
    public int getChildrenCount(int groupPosition)
    {
        int size=0;
        Log.e("log_tag"," 9  ");
        if(parents.get(groupPosition).getChildren()!=null)
            size = parents.get(groupPosition).getChildren().size();
        return size;
    }

    @Override
    public Object getGroup(int groupPosition)
    {
        Log.i("Parent", groupPosition+"=  getGroup ");
        return parents.get(groupPosition);
    }

    @Override
    public int getGroupCount()
    {
        return parents.size();
    }

    //Call when parent row clicked
    @Override
    public long getGroupId(int groupPosition)
    {

        Log.i("Parent", groupPosition+"=  getGroupId "+ParentClickStatus);



        if(groupPosition==2 && ParentClickStatus!=groupPosition){

            //Alert to user
            Toast.makeText(getApplicationContext(), "Parent :"+groupPosition , 
                    Toast.LENGTH_LONG).show();
        }

        ParentClickStatus=groupPosition;
        if(ParentClickStatus==0)
            ParentClickStatus=-1;

        return groupPosition;
    }


    @Override
    public void notifyDataSetChanged()
    {
        Log.e("log_tag"," 13 ");
        // Refresh List rows
        super.notifyDataSetChanged();
    }

    @Override
    public boolean isEmpty()
    {
        Log.e("log_tag"," 14 ");
        return ((parents == null) || parents.isEmpty());
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition)
    {
        Log.e("log_tag"," 15 ");
        return true;
    }

    @Override
    public boolean hasStableIds()
    {
        Log.e("log_tag"," 16 ");
        return true;
    }

    @Override
    public boolean areAllItemsEnabled()
    {
        Log.e("log_tag"," 17 ");
        return true;
    }
}

}

0 个答案:

没有答案