Friday, October 11, 2013

Filter in Android for Search details

// in adapter class
public Filter getFilter()
    {
        if(vlfilter == null)
        {
            vlfilter = new ValueFilter();
        }
        // TODO Auto-generated method stub
        return vlfilter;
    }
  
    private class ValueFilter extends Filter
    {
        ArrayList<Restaurants> filterList;

        @Override
        protected FilterResults performFiltering(CharSequence constraint)
        {
            // TODO Auto-generated method stub
            FilterResults results = new FilterResults();

            if(constraint!=null && constraint.length()>0)
            {
                filterList=new ArrayList<Restaurants>();
                for(int i=0;i<fltrlst.size();i++)
                {
                    if(fltrlst.get(i).getTitle().toLowerCase().contains(constraint))
                    {
                        filterList.add(fltrlst.get(i));
                    }
                }
                results.count=filterList.size();
                results.values=filterList;
            }
            else
            {
                results.count=fltrlst.size();
                results.values=fltrlst;
            }
            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results)
        {
            // TODO Auto-generated method stub
            lst =(ArrayList<Restaurants>) results.values;            
            notifyDataSetInvalidated();
        }
      
    }

Thursday, June 20, 2013

adapetr class for listview

public class RecentContestAdapter extends BaseAdapter
{
    ArrayList<Object> itemList;
    public Activity context;
    public LayoutInflater inflater;
    public URL htlUrl;
    Bitmap htlUrlBitmap;
    public String image;

    public RecentContestAdapter(Activity context,ArrayList<Object> itemList)
    {
        super();
        this.context = context;
        this.itemList = itemList;       
        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    public int getCount()
    {
        // TODO Auto-generated method stub
        return itemList.size();
    }

 
    public Object getItem(int position)
    {
        // TODO Auto-generated method stub
        return itemList.get(position);
    }


    public long getItemId(int position)
    {
        // TODO Auto-generated method stub
        return 0;
    }

    public static class ViewHolder
    {       
        TextView txtCntstTitle;
        TextView txtCntstDescription;       
        TextView txtCntstStatus;
        ImageView BannerImage;
    }

  
    public View getView(int position, View convertView, ViewGroup parent)
    {
        // TODO Auto-generated method stub
        ViewHolder holder;
        if(convertView==null)
        {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.child_row, null);         
            holder.txtCntstTitle = (TextView) convertView.findViewById(R.id.txtContestTitle);
            holder.txtCntstDescription = (TextView) convertView.findViewById(R.id.txtContestDesc);
            holder.txtCntstStatus = (TextView) convertView.findViewById(R.id.txtContestEndDate);
           
            convertView.setTag(holder);
        }
        else
            holder=(ViewHolder)convertView.getTag();
       
        RecentContest cntst = (RecentContest) itemList.get(position);       


        holder.txtCntstTitle.setText(cntst.getTitle().trim());
        holder.txtCntstDescription.setText(cntst.getDescription().trim()); 
        holder.txtCntstStatus.setText("Submission "+cntst.getStatus().trim());

       
            
      
        return convertView;
    }
}

send json object using httpclient

   
                 public void getData()
     {
         dialog = new ProgressDialog(RecentContestActivity.this);
            dialog.setIcon(R.drawable.appicon);
            dialog.setTitle("ForTalent");       
            dialog.setMessage("Loading ...");
            dialog.setIndeterminate(false); 
            dialog.setCancelable(false);
            dialog.show();                                      

         new Thread()
            {
              public void run()
              {
                 try
                       {
                      JSONObject json = new JSONObject();                                                       
                           json.put("call", "contest");
                           json.put("status", "recent");
                           response =  WS.readstream(json);
                  }
                catch (Exception e)
                {
                   
                }
                
                 handler.sendEmptyMessage(0);
               
             }
            }.start();
           
     }
   
     private Handler handler = new Handler() {

         @Override
         public void handleMessage(Message msg)
         {
           
             itmlist = new ArrayList<Object>();
             JSONObject json;
             JSONArray jArray = null;
           
             try
             {               
                 json = new JSONObject(response);
                 jArray = json.getJSONArray("Recent");
                                             
                 if(jArray.length() <= 0)
                 {                                     
                     txterrmsg.setVisibility(View.VISIBLE);
                     txterrmsg.setText("No Recent Contest are available");
                 }
                 else
                 {
                     txterrmsg.setVisibility(View.GONE);
                 }
             }
             catch(Exception e)
             {
               
             }
           
             try
                 {
                 json = new JSONObject(response);
                 jArray = json.getJSONArray("Recent");
                         
                 Recentcontests=new RecentContest[jArray.length()];
            
                 for (int i = 0; i < jArray.length(); i++)
                 {
                     Recentcontests[i]=new RecentContest();
                
                     JSONObject contestobj = jArray.getJSONObject(i);
                
                     Recentcontests[i].contest_Id=contestobj.getString("Contest_id").toString();
                     Recentcontests[i].contest_title=contestobj.getString("Contest_title").toString();
                     Recentcontests[i].contest_description=contestobj.getString("Contest_Desc").toString();
                     Recentcontests[i].contest_bannerurl=contestobj.getString("Contest_bannerURL").toString();
                     Recentcontests[i].contest_starts=contestobj.getString("Contest_starts").toString();
                     Recentcontests[i].contest_ends=contestobj.getString("Contest_ends").toString();
                     Recentcontests[i].contest_status=contestobj.getString("Contest_Status").toString();
                     Recentcontests[i].contest_type=contestobj.getString("Contest_Type").toString();
                    
                 }
            
             }
             catch(Exception e)
             {
                
             }
             //Recentcontests=WS.converttoobjects(response);
           
             for(int i=0;i<Recentcontests.length;i++)
             {
                   itmlist.add(Recentcontests[i]);                     
             }
                 
                  cntstadp = new RecentContestAdapter(RecentContestActivity.this,itmlist);
                  contestlistView.setAdapter(cntstadp);
                  contestlistView.setOnItemClickListener(RecentContestActivity.this);
                  dialog.dismiss();                
         }
     };
public class WSReceiver
{
    public static String mainurl = "http://www.example.com";
    public String readstream(JSONObject json) throws JSONException
    {
        String result=null;
        try
        {
            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
            HttpConnectionParams.setSoTimeout(httpParams, 10000);
            HttpClient client = new DefaultHttpClient(httpParams);
          
            String url = mainurl ;

            HttpPost request = new HttpPost(url);
            request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
            request.setHeader("json", json.toString());           
            HttpResponse response = client.execute(request);
           
            HttpEntity entity = response.getEntity();
           
            // If the response does not enclose an entity, there is no need
            if (entity != null)
            {
                InputStream instream = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
                StringBuilder sb = new StringBuilder();

                String line = null;
                try
                {
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                    }
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                finally
                {
                    try
                    {
                        instream.close();
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }           
                result = sb.toString();           
            }
        }
        catch (Throwable t)
        {
            result = t.toString();
        }
        return result;
    }
   
   

Thursday, August 16, 2012

Creating dynamic Rows, Coloumns and Textbox in Table : ASP.NET using C#

Select Table tool from toolbar and name it as TBL
and declare the Variables
int I;
//ICOL denotes the number of Coloumn 
//IROW denotes the number of Rows

string[] HSTR = { "ROLLNO""NAME""MARKS""""" };
TableHeaderRow HR = new TableHeaderRow();
TBL.Controls.Add(HR);
for (I = 0; I <= 4; I++)
{
TableHeaderCell HCELL = new TableHeaderCell();
HR.Cells.Add(HCELL);
HCELL.Text = HSTR[I];
}
for (I = 1; I <= IROW; I++)
{
TableRow TROW = new TableRow();
TBL.Controls.Add(TROW);
for (j = 0; j < ICOL - 2; j++)
{
TableCell TCELL = new TableCell();
TROW.Cells.Add(TCELL);
}
TableCell TCELL1 = new TableCell();
TROW.Cells.Add(TCELL1);
TextBox TXT = new TextBox();
TB[I] = TXT;
TCELL1.Controls.Add(TB[I]);
TableCell TCELL2 = new TableCell();
TROW.Cells.Add(TCELL2);