hi guys,
hope you are doing well in android. Today my code is about binding multiple dependent spinner controls. These spinner controls are binded with the sql lite database which i would be dealing in my next post. The following code would give you an idea about how to bind multiple spinner controls.
Step1 Bind the spinners
Step2 based on the selection of search create an array
Step3 check the selection made by the user and remove the dependents in case the user selects the default value from the spinner control.
Step4 bind the data and pass it on to the result page.
Hope you guys get some idea about binding the spinner control. Here is the code
package ritesh.tandon.advancsearch
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class GuestProgramSearch extends Activity
{
DBHelper MyDB=new DBHelper(this);
SQLiteDatabase db;
TextView tv;
String textMake="Select",textModel="Select",textEditionType="Select",textColour="Select";
Button btnSearch,btnReset;
Spinner spinnerMake,spinnerModel,spinnerEdition,spinnerColour;
Context mctx;
String Make="",Model="",Editiontype="",Colour="",searchtype="",searchvalue="";
List<String> dropdownsarray = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.guestprogramsearch);
tv=(TextView)findViewById(R.id.editTextSearchProgram);
mctx=this;
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED || conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) {
CarSearch srch=new CarSearch();
srch.createandUpdate( mctx);
}
else if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED || conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
Toast.makeText(mctx, "You are Not Connected with internet", Toast.LENGTH_LONG).show();
}
btnSearch=(Button)findViewById(R.id.btnSearch);
btnReset=(Button)findViewById(R.id.btnReset);
final String cols[]={"Make"};
final String cols1[]={"Model"};
final String cols2[]={"EditionType"};
final String cols3[]={"Colour"};
spinnerMake =(Spinner)findViewById(R.id.spinnerMake);
spinnerMake.setAdapter(BindSpinner("Cars",cols,null,true,""));
spinnerMake.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
String selected = parentView.getItemAtPosition(position).toString();
if(textMake.equals("Select") && !selected.equals("Select"))
{
dropdownsarray.add("Make");
}
else if(!textMake.equals("Select") && !selected.equals("Select"))
{
int index=dropdownsarray.indexOf("Make");
for(int i=index+1;i<dropdownsarray.size();i++)
{
if(dropdownsarray.get(i).equals("Make"))
textMake="Select";
else if(dropdownsarray.get(i).equals("Model"))
textModel="Select";
else if(dropdownsarray.get(i).equals("EditionType"))
textEditionType="Select";
else if(dropdownsarray.get(i).equals("Colour"))
textColour="Select";
dropdownsarray.remove(i);
}
removedependents("Make");
}
else if(!textMake.equals("Select") && selected.equals("Select"))
{
removedependents("Make");
dropdownsarray.remove("Make");
}
textMake=selected;
SearchUpdated();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
spinnerModel =(Spinner)findViewById(R.id.spinnerModel);
spinnerModel.setAdapter(BindSpinner("Cars",cols1,null,true,""));
spinnerModel.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
String selected = parentView.getItemAtPosition(position).toString();
if(textModel.equals("Select") && !selected.equals("Select"))
{
dropdownsarray.add("Model");
}
else if(!textModel.equals("Select") && !selected.equals("Select"))
{
int index=dropdownsarray.indexOf("Model");
for(int i=index+1;i<dropdownsarray.size();i++)
{
if(dropdownsarray.get(i).equals("Make"))
textMake="Select";
else if(dropdownsarray.get(i).equals("Model"))
textModel="Select";
else if(dropdownsarray.get(i).equals("EditionType"))
textEditionType="Select";
else if(dropdownsarray.get(i).equals("Colour"))
textColour="Select";
dropdownsarray.remove(i);
}
removedependents("Model");
}
else if(!textModel.equals("Select") && selected.equals("Select"))
{
removedependents("Model");
dropdownsarray.remove("Model");
}
textModel=selected;
SearchUpdated();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
spinnerEdition =(Spinner)findViewById(R.id.spinnerEdition);
spinnerEdition.setAdapter(BindSpinner("Cars",cols2,null,true,""));
spinnerEdition.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
String selected = parentView.getItemAtPosition(position).toString();
if(textEditionType.equals("Select") && !selected.equals("Select"))
{
dropdownsarray.add("EditionType");
}
else if(!textEditionType.equals("Select") && !selected.equals("Select"))
{
//again selection
int index=dropdownsarray.indexOf("EditionType");
for(int i=index+1;i<dropdownsarray.size();i++)
{
if(dropdownsarray.get(i).equals("Make"))
textMake="Select";
else if(dropdownsarray.get(i).equals("Model"))
textModel="Select";
else if(dropdownsarray.get(i).equals("EditionType"))
textEditionType="Select";
else if(dropdownsarray.get(i).equals("Colour"))
textColour="Select";
dropdownsarray.remove(i);
}
removedependents("EditionType");
}
else if(!textEditionType.equals("Select") && selected.equals("Select"))
{
//Select selected again
removedependents("EditionType");
dropdownsarray.remove("EditionType");
}
textEditionType=selected;
SearchUpdated();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
spinnerColour =(Spinner)findViewById(R.id.spinnerColour);
spinnerColour.setAdapter(BindSpinner("Cars",cols3,null,true,""));
spinnerColour.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
String selected = parentView.getItemAtPosition(position).toString();
if(textColour.equals("Select") && !selected.equals("Select"))
{
dropdownsarray.add("Colour");
}
else if(!textColour.equals("Select") && !selected.equals("Select"))
{
//again selection
int index=dropdownsarray.indexOf("Colour");
for(int i=index+1;i<dropdownsarray.size();i++)
{
if(dropdownsarray.get(i).equals("Make"))
textMake="Select";
else if(dropdownsarray.get(i).equals("Model"))
textModel="Select";
else if(dropdownsarray.get(i).equals("EditionType"))
textEditionType="Select";
else if(dropdownsarray.get(i).equals("Colour"))
textColour="Select";
dropdownsarray.remove(i);
}
removedependents("Colour");
}
else if(!textColour.equals("Select") && selected.equals("Select"))
{
//Select selected again
removedependents("Colour");
dropdownsarray.remove("Colour");
}
textColour=selected;
SearchUpdated();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
btnSearch.setOnClickListener(new OnClickListener()
{
boolean flg=false;
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
searchvalue=tv.getText().toString();
Intent intent=new Intent(mctx,GuestProgramSearchResults.class);
intent.putExtra("Make", textMake);
intent.putExtra("Model", textModel);
intent.putExtra("Colour", textColour);
intent.putExtra("EditionType", textEditionType);
intent.putExtra("SearchText",searchvalue);
if(!searchvalue.equals("") || (!textMake.equals("Select")) || (!textModel.equals("Select")) || (!textColour.equals("Select")) || (!textEditionType.equals("Select")))
{
flg=true;
}
else
{
Toast.makeText(mctx, "Please Select At Least One Option To Search.", Toast.LENGTH_SHORT).show();
flg=false;
}
if(flg==true)
startActivity(intent);
}
});
btnReset.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
textMake="Select";
textModel="Select";
textEditionType="Select";
textColour="Select";
spinnerMake.setAdapter(BindSpinner("Cars",cols,null,true,""));
spinnerModel.setAdapter(BindSpinner("Cars",cols1,null,true,""));
spinnerEdition.setAdapter(BindSpinner("Cars",cols2,null,true,""));
spinnerColour.setAdapter(BindSpinner("Cars",cols3,null,true,""));
tv.setText("");
dropdownsarray.clear();
textMake="Select";
textModel="Select";
textEditionType="Select";
textColour="Select";
}
});
}
public void removedependents(String dropdownsarrayvalue)
{
int index=dropdownsarray.indexOf(dropdownsarrayvalue);
if(index!=-1)
{
for(int i=index+1;i<dropdownsarray.size();i++)
{
String[] c={dropdownsarray.get(i)};
//tv.setText(tv.getText()+", "+c[0]);
if(c[0].equals("Make"))
spinnerMake.setAdapter(BindSpinner("Cars",c,null,true,""));
else if(c[0].equals("Model"))
spinnerModel.setAdapter(BindSpinner("Cars",c,null,true,""));
else if(c[0].equals("EditionType"))
spinnerEdition.setAdapter(BindSpinner("Cars",c,null,true,""));
else if(c[0].equals("Colour"))
spinnerColour.setAdapter(BindSpinner("Cars",c,null,true,""));
}
}
}
public void SearchUpdated()
{
try
{
if(dropdownsarray.size()>0)
{
String[] cols=new String[dropdownsarray.size()];
dropdownsarray.toArray(cols);
String[] vals=new String[dropdownsarray.size()];
for(int i=0;i<dropdownsarray.size();i++)
{
if(dropdownsarray.get(i).equals("Make"))
vals[i]=textMake;
else if(dropdownsarray.get(i).equals("Model"))
vals[i]=textModel;
else if(dropdownsarray.get(i).equals("EditionType"))
vals[i]=textEditionType;
else if(dropdownsarray.get(i).equals("Colour"))
vals[i]=textColour;
}
if(textMake.equals("Select"))
spinnerMake.setAdapter(BindSpinner("Cars",cols,vals,false,"Make"));
if(textModel.equals("Select"))
spinnerModel.setAdapter(BindSpinner("Cars",cols,vals,false,"Model"));
if(textEditionType.equals("Select"))
spinnerEdition.setAdapter(BindSpinner("Cars",cols,vals,false,"EditionType"));
if(textColour.equals("Select"))
spinnerColour.setAdapter(BindSpinner("Cars",cols,vals,false,"Colour"));
}
}
catch(Exception ex)
{
tv.setText(ex.toString());
}
}
public void checkNetworkStatus(){
final ConnectivityManager connMgr = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if( wifi.isAvailable() ){
Toast.makeText(this, "Wifi" , Toast.LENGTH_LONG).show();
}
else if( mobile.isAvailable() ){
Toast.makeText(this, "Mobile 3G " , Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "No Network " , Toast.LENGTH_LONG).show();
}
}
protected void alertbox(String title, String mymessage)
{
new AlertDialog.Builder(this)
.setMessage(mymessage)
.setTitle(title)
.setCancelable(true)
.setNeutralButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){}
})
.show();
}
protected ArrayAdapter<String> BindSpinner(String tablename,String[] ColumnNames,String[] ColumnValues,boolean isload,String FieldName)
{
try
{
List<String> x = new ArrayList<String>();
x.add("Select");
MyDB.createDataBase(mctx);
db=MyDB.openDataBaseFromother();
if(isload)
{
Cursor c1=db.query(true,tablename,ColumnNames, null, null, null, null, null, null);
for(c1.moveToFirst(); !c1.isAfterLast(); c1.moveToNext())
{
x.add(c1.getString(0));
}
c1.close();
}
else
{
String[] Cols={FieldName};
String query="",query1="";
for(int j=0;j<ColumnNames.length;j++)
{
if(query=="")
{
query=ColumnNames[j]+" = ?";
query1=ColumnNames[j]+" = "+ColumnValues[j];
}
else
{
if (!query.toString().contains(ColumnNames[j]))
{
query+=" And "+ColumnNames[j]+" = ?";
query1+=" And "+ColumnNames[j]+" = "+ColumnValues[j];
}
}
}
if(ColumnNames.length>0)
{
Cursor c1=db.query(true,tablename,Cols,query, ColumnValues, null, null, null, null);
for(c1.moveToFirst(); !c1.isAfterLast(); c1.moveToNext())
{
x.add(c1.getString(0));
}
c1.close();
}
}
db.close();
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, x);
return spinnerArrayAdapter;
}
catch(Exception ex)
{
if(db.isOpen())
db.close();
Log.w("Error", ex.toString());
return null;
}
}
}
hope you are doing well in android. Today my code is about binding multiple dependent spinner controls. These spinner controls are binded with the sql lite database which i would be dealing in my next post. The following code would give you an idea about how to bind multiple spinner controls.
Step1 Bind the spinners
Step2 based on the selection of search create an array
Step3 check the selection made by the user and remove the dependents in case the user selects the default value from the spinner control.
Step4 bind the data and pass it on to the result page.
Hope you guys get some idea about binding the spinner control. Here is the code
package ritesh.tandon.advancsearch
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class GuestProgramSearch extends Activity
{
DBHelper MyDB=new DBHelper(this);
SQLiteDatabase db;
TextView tv;
String textMake="Select",textModel="Select",textEditionType="Select",textColour="Select";
Button btnSearch,btnReset;
Spinner spinnerMake,spinnerModel,spinnerEdition,spinnerColour;
Context mctx;
String Make="",Model="",Editiontype="",Colour="",searchtype="",searchvalue="";
List<String> dropdownsarray = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.guestprogramsearch);
tv=(TextView)findViewById(R.id.editTextSearchProgram);
mctx=this;
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED || conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) {
CarSearch srch=new CarSearch();
srch.createandUpdate( mctx);
}
else if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED || conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
Toast.makeText(mctx, "You are Not Connected with internet", Toast.LENGTH_LONG).show();
}
btnSearch=(Button)findViewById(R.id.btnSearch);
btnReset=(Button)findViewById(R.id.btnReset);
final String cols[]={"Make"};
final String cols1[]={"Model"};
final String cols2[]={"EditionType"};
final String cols3[]={"Colour"};
spinnerMake =(Spinner)findViewById(R.id.spinnerMake);
spinnerMake.setAdapter(BindSpinner("Cars",cols,null,true,""));
spinnerMake.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
String selected = parentView.getItemAtPosition(position).toString();
if(textMake.equals("Select") && !selected.equals("Select"))
{
dropdownsarray.add("Make");
}
else if(!textMake.equals("Select") && !selected.equals("Select"))
{
int index=dropdownsarray.indexOf("Make");
for(int i=index+1;i<dropdownsarray.size();i++)
{
if(dropdownsarray.get(i).equals("Make"))
textMake="Select";
else if(dropdownsarray.get(i).equals("Model"))
textModel="Select";
else if(dropdownsarray.get(i).equals("EditionType"))
textEditionType="Select";
else if(dropdownsarray.get(i).equals("Colour"))
textColour="Select";
dropdownsarray.remove(i);
}
removedependents("Make");
}
else if(!textMake.equals("Select") && selected.equals("Select"))
{
removedependents("Make");
dropdownsarray.remove("Make");
}
textMake=selected;
SearchUpdated();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
spinnerModel =(Spinner)findViewById(R.id.spinnerModel);
spinnerModel.setAdapter(BindSpinner("Cars",cols1,null,true,""));
spinnerModel.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
String selected = parentView.getItemAtPosition(position).toString();
if(textModel.equals("Select") && !selected.equals("Select"))
{
dropdownsarray.add("Model");
}
else if(!textModel.equals("Select") && !selected.equals("Select"))
{
int index=dropdownsarray.indexOf("Model");
for(int i=index+1;i<dropdownsarray.size();i++)
{
if(dropdownsarray.get(i).equals("Make"))
textMake="Select";
else if(dropdownsarray.get(i).equals("Model"))
textModel="Select";
else if(dropdownsarray.get(i).equals("EditionType"))
textEditionType="Select";
else if(dropdownsarray.get(i).equals("Colour"))
textColour="Select";
dropdownsarray.remove(i);
}
removedependents("Model");
}
else if(!textModel.equals("Select") && selected.equals("Select"))
{
removedependents("Model");
dropdownsarray.remove("Model");
}
textModel=selected;
SearchUpdated();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
spinnerEdition =(Spinner)findViewById(R.id.spinnerEdition);
spinnerEdition.setAdapter(BindSpinner("Cars",cols2,null,true,""));
spinnerEdition.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
String selected = parentView.getItemAtPosition(position).toString();
if(textEditionType.equals("Select") && !selected.equals("Select"))
{
dropdownsarray.add("EditionType");
}
else if(!textEditionType.equals("Select") && !selected.equals("Select"))
{
//again selection
int index=dropdownsarray.indexOf("EditionType");
for(int i=index+1;i<dropdownsarray.size();i++)
{
if(dropdownsarray.get(i).equals("Make"))
textMake="Select";
else if(dropdownsarray.get(i).equals("Model"))
textModel="Select";
else if(dropdownsarray.get(i).equals("EditionType"))
textEditionType="Select";
else if(dropdownsarray.get(i).equals("Colour"))
textColour="Select";
dropdownsarray.remove(i);
}
removedependents("EditionType");
}
else if(!textEditionType.equals("Select") && selected.equals("Select"))
{
//Select selected again
removedependents("EditionType");
dropdownsarray.remove("EditionType");
}
textEditionType=selected;
SearchUpdated();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
spinnerColour =(Spinner)findViewById(R.id.spinnerColour);
spinnerColour.setAdapter(BindSpinner("Cars",cols3,null,true,""));
spinnerColour.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
String selected = parentView.getItemAtPosition(position).toString();
if(textColour.equals("Select") && !selected.equals("Select"))
{
dropdownsarray.add("Colour");
}
else if(!textColour.equals("Select") && !selected.equals("Select"))
{
//again selection
int index=dropdownsarray.indexOf("Colour");
for(int i=index+1;i<dropdownsarray.size();i++)
{
if(dropdownsarray.get(i).equals("Make"))
textMake="Select";
else if(dropdownsarray.get(i).equals("Model"))
textModel="Select";
else if(dropdownsarray.get(i).equals("EditionType"))
textEditionType="Select";
else if(dropdownsarray.get(i).equals("Colour"))
textColour="Select";
dropdownsarray.remove(i);
}
removedependents("Colour");
}
else if(!textColour.equals("Select") && selected.equals("Select"))
{
//Select selected again
removedependents("Colour");
dropdownsarray.remove("Colour");
}
textColour=selected;
SearchUpdated();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
btnSearch.setOnClickListener(new OnClickListener()
{
boolean flg=false;
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
searchvalue=tv.getText().toString();
Intent intent=new Intent(mctx,GuestProgramSearchResults.class);
intent.putExtra("Make", textMake);
intent.putExtra("Model", textModel);
intent.putExtra("Colour", textColour);
intent.putExtra("EditionType", textEditionType);
intent.putExtra("SearchText",searchvalue);
if(!searchvalue.equals("") || (!textMake.equals("Select")) || (!textModel.equals("Select")) || (!textColour.equals("Select")) || (!textEditionType.equals("Select")))
{
flg=true;
}
else
{
Toast.makeText(mctx, "Please Select At Least One Option To Search.", Toast.LENGTH_SHORT).show();
flg=false;
}
if(flg==true)
startActivity(intent);
}
});
btnReset.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
textMake="Select";
textModel="Select";
textEditionType="Select";
textColour="Select";
spinnerMake.setAdapter(BindSpinner("Cars",cols,null,true,""));
spinnerModel.setAdapter(BindSpinner("Cars",cols1,null,true,""));
spinnerEdition.setAdapter(BindSpinner("Cars",cols2,null,true,""));
spinnerColour.setAdapter(BindSpinner("Cars",cols3,null,true,""));
tv.setText("");
dropdownsarray.clear();
textMake="Select";
textModel="Select";
textEditionType="Select";
textColour="Select";
}
});
}
public void removedependents(String dropdownsarrayvalue)
{
int index=dropdownsarray.indexOf(dropdownsarrayvalue);
if(index!=-1)
{
for(int i=index+1;i<dropdownsarray.size();i++)
{
String[] c={dropdownsarray.get(i)};
//tv.setText(tv.getText()+", "+c[0]);
if(c[0].equals("Make"))
spinnerMake.setAdapter(BindSpinner("Cars",c,null,true,""));
else if(c[0].equals("Model"))
spinnerModel.setAdapter(BindSpinner("Cars",c,null,true,""));
else if(c[0].equals("EditionType"))
spinnerEdition.setAdapter(BindSpinner("Cars",c,null,true,""));
else if(c[0].equals("Colour"))
spinnerColour.setAdapter(BindSpinner("Cars",c,null,true,""));
}
}
}
public void SearchUpdated()
{
try
{
if(dropdownsarray.size()>0)
{
String[] cols=new String[dropdownsarray.size()];
dropdownsarray.toArray(cols);
String[] vals=new String[dropdownsarray.size()];
for(int i=0;i<dropdownsarray.size();i++)
{
if(dropdownsarray.get(i).equals("Make"))
vals[i]=textMake;
else if(dropdownsarray.get(i).equals("Model"))
vals[i]=textModel;
else if(dropdownsarray.get(i).equals("EditionType"))
vals[i]=textEditionType;
else if(dropdownsarray.get(i).equals("Colour"))
vals[i]=textColour;
}
if(textMake.equals("Select"))
spinnerMake.setAdapter(BindSpinner("Cars",cols,vals,false,"Make"));
if(textModel.equals("Select"))
spinnerModel.setAdapter(BindSpinner("Cars",cols,vals,false,"Model"));
if(textEditionType.equals("Select"))
spinnerEdition.setAdapter(BindSpinner("Cars",cols,vals,false,"EditionType"));
if(textColour.equals("Select"))
spinnerColour.setAdapter(BindSpinner("Cars",cols,vals,false,"Colour"));
}
}
catch(Exception ex)
{
tv.setText(ex.toString());
}
}
public void checkNetworkStatus(){
final ConnectivityManager connMgr = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if( wifi.isAvailable() ){
Toast.makeText(this, "Wifi" , Toast.LENGTH_LONG).show();
}
else if( mobile.isAvailable() ){
Toast.makeText(this, "Mobile 3G " , Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "No Network " , Toast.LENGTH_LONG).show();
}
}
protected void alertbox(String title, String mymessage)
{
new AlertDialog.Builder(this)
.setMessage(mymessage)
.setTitle(title)
.setCancelable(true)
.setNeutralButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){}
})
.show();
}
protected ArrayAdapter<String> BindSpinner(String tablename,String[] ColumnNames,String[] ColumnValues,boolean isload,String FieldName)
{
try
{
List<String> x = new ArrayList<String>();
x.add("Select");
MyDB.createDataBase(mctx);
db=MyDB.openDataBaseFromother();
if(isload)
{
Cursor c1=db.query(true,tablename,ColumnNames, null, null, null, null, null, null);
for(c1.moveToFirst(); !c1.isAfterLast(); c1.moveToNext())
{
x.add(c1.getString(0));
}
c1.close();
}
else
{
String[] Cols={FieldName};
String query="",query1="";
for(int j=0;j<ColumnNames.length;j++)
{
if(query=="")
{
query=ColumnNames[j]+" = ?";
query1=ColumnNames[j]+" = "+ColumnValues[j];
}
else
{
if (!query.toString().contains(ColumnNames[j]))
{
query+=" And "+ColumnNames[j]+" = ?";
query1+=" And "+ColumnNames[j]+" = "+ColumnValues[j];
}
}
}
if(ColumnNames.length>0)
{
Cursor c1=db.query(true,tablename,Cols,query, ColumnValues, null, null, null, null);
for(c1.moveToFirst(); !c1.isAfterLast(); c1.moveToNext())
{
x.add(c1.getString(0));
}
c1.close();
}
}
db.close();
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, x);
return spinnerArrayAdapter;
}
catch(Exception ex)
{
if(db.isOpen())
db.close();
Log.w("Error", ex.toString());
return null;
}
}
}
No comments:
Post a Comment
Comments are welcome, Please join me on my Linked In account
http://in.linkedin.com/pub/ritesh-tandon/21/644/33b