Struggling with getting data from another activity - java

I can transfer my array-data from an activity to the other, but not my string-data, and I don't know why.
This is my mainActivity:
protected void onClickCityBreak(View v) {
persons = (EditText) findViewById(R.id.txtPerson);
days = (EditText) findViewById(R.id.txtDays);
String p = persons.toString();
String d = days.toString();
String [] arrayCityBreak = getResources().getStringArray(R.array.citybreak);
Intent myintent = new Intent(MainActivity.this, TripActivity.class);
myintent.putExtra("PERSONS", p);
myintent.putExtra("DAYS", d);
myintent.putExtra("PLACES",arrayCityBreak);
startActivity(myintent);
}
This is my other activity I am sending to:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trip);
String person = getIntent().getStringExtra("PERSONS");
String day = getIntent().getStringExtra("DAYS");
TextView txtPerson = (TextView) findViewById(R.id.txtViewPersons);
txtPerson.setText("Persons travelling: " + person);
TextView txtDay = (TextView) findViewById(R.id.txtViewDays);
txtDay.setText("Days of traveling: " + day);
String[] arrayCityBreak = getIntent().getStringArrayExtra("PLACES");
ArrayAdapter<String> adapterCityBreak = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayCityBreak);
ListView myview = (ListView) findViewById(R.id.lstView);
myview.setAdapter(adapterCityBreak);
}
I get this on my TextView in the application:
android.support.v7.widget.AppCompactEditText{13e7726VFED...CL. ........563,56....#7f0c0059 app:id/txtDays\
The same for txtPerson.
I have also tried using Bundle

Getting the content of an EditText should be done in this way :
String p = persons.getText().toString();
String d = days.getText().toString();

Also if you have another problems with putExtra, you can do something like this:
MainActivity:
protected void onClickCityBreak(View v) {
persons = (EditText) findViewById(R.id.txtPerson);
days = (EditText) findViewById(R.id.txtDays);
String p = persons.getText().toString();
String d = days.getText().toString();
String [] arrayCityBreak = getResources().getStringArray(R.array.citybreak);
Intent myintent = new Intent(MainActivity.this, TripActivity.class);
AnotherActivityClass secondactivity = new AnotherActivityClass();
secondactivity.persons = p;
secondactivity.days = d;
secondactivity.places = arrayCityBreak;
startActivity(myintent);
}
Create public string persons and days, also create public String[] in SecondActivity:
public String days;
public String persons;
public String [] places;
And then in SecondActivity use this values:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trip);
TextView txtPerson = (TextView) findViewById(R.id.txtViewPersons);
txtPerson.setText("Persons travelling: " + person);
TextView txtDay = (TextView) findViewById(R.id.txtViewDays);
txtDay.setText("Days of traveling: " + day);
ArrayAdapter<String> adapterCityBreak = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayCityBreak);
ListView myview = (ListView) findViewById(R.id.lstView);
myview.setAdapter(adapterCityBreak);
}

Related

Save data from Main activity to a ListView activity

I have a MainAtivity which has some EditText and 2 button. Save button will save user input to ListView and List button to show ListView (which I display in second activity).
Is there anyway to collect data from multiple inputs then pass it to other activity. And after get that data how to combine it to a List item.
Please show me some code and explain cause I'm a beginner.
I have read some post and they suggest use startActivityForResult, intent, bundles but I still don't understand.
This is my Main class:
public class MainActivity extends AppCompatActivity {
String str, gender, vaccine, date;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button okay = (Button) findViewById(R.id.btnOk);
Button list = (Button) findViewById(R.id.btnList);
EditText name = (EditText) findViewById(R.id.inputName);
EditText address = (EditText) findViewById(R.id.inputAdd);
EditText phone = (EditText) findViewById(R.id.inputPhone);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
RadioButton female = (RadioButton) findViewById(R.id.inputFemale);
RadioButton male = (RadioButton) findViewById(R.id.inputMale);
CheckBox first = (CheckBox)findViewById(R.id.inputFirst);
CheckBox second = (CheckBox)findViewById(R.id.inputSecond);
CheckBox third = (CheckBox)findViewById(R.id.inputThird);
EditText datefirst = (EditText) findViewById(R.id.dateFirst);
EditText datesecond = (EditText) findViewById(R.id.dateSecond);
EditText datethird = (EditText) findViewById(R.id.dateThird);
TextView result = (TextView)findViewById(R.id.textResult);
okay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(female.isChecked()) gender = female.getText().toString();
if(male.isChecked()) gender = male.getText().toString();
if(first.isChecked()) {
vaccine = first.getText().toString();
date = datefirst.getText().toString();
}
if(second.isChecked()) {
vaccine = second.getText().toString();
date = datesecond.getText().toString();
}
if(third.isChecked()) {
vaccine = third.getText().toString();
date = datethird.getText().toString();
}
str = name.getText().toString() + "\n" + address.getText().toString() + "\n" + phone.getText().toString() + "\n" +
gender + "\n" + vaccine + "\n" + date;
result.setText(str);
Toast.makeText(getApplicationContext(),result.getText().toString(),Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, PersonView.class);
intent.putExtra("NAME",name.getText().toString());
startActivity(intent);
}
});
list.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, PersonView.class);
startActivity(intent);
}
});
}
}
This is my ListView class:
public class PersonView extends AppCompatActivity {
ArrayList<Person> listPerson;
PersonListViewAdapter personListViewAdapter;
ListView listViewPerson;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Intent intent = getIntent();
String message = intent.getStringExtra("NAME");
Button back = (Button) findViewById(R.id.btnBack);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(PersonView.this, MainActivity.class);
//startActivityForResult(intent,2);
}
});
listPerson = new ArrayList<>();
listPerson.add(new Person("Lieu Mai","25 Mac Dinh Chi", "0786867073", "female","3 injection", "24/07/2000"));
personListViewAdapter = new PersonListViewAdapter(listPerson);
listViewPerson = findViewById(R.id.listPerson);
listViewPerson.setAdapter(personListViewAdapter);
}
class Person {
String name, address, phone, gender, vaccine, date;
public Person( String name, String address, String phone, String gender, String vaccine, String date) {
this.name = name;
this.address = address;
this.phone = phone;
this.gender = gender;
this.vaccine = vaccine;
this.date = date;
}
}
class PersonListViewAdapter extends BaseAdapter {
final ArrayList<Person> listPerson;
PersonListViewAdapter(ArrayList<Person> listPerson) {
this.listPerson = listPerson;
}
#Override
public int getCount() {
return listPerson.size();
}
#Override
public Object getItem(int position) {
return listPerson.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View viewPerson;
if (convertView == null) {
viewPerson = View.inflate(parent.getContext(), R.layout.person_view, null);
} else viewPerson = convertView;
Person person = (Person) getItem(position);
((TextView) viewPerson.findViewById(R.id.txtName)).setText(String.format("Name: %s", person.name));
((TextView) viewPerson.findViewById(R.id.txtAddress)).setText(String.format("Address : %s", person.address));
((TextView) viewPerson.findViewById(R.id.txtPhone)).setText(String.format("Phone number: %s", person.phone));
((TextView) viewPerson.findViewById(R.id.txtGender)).setText(String.format("Gender: %s", person.gender));
((TextView) viewPerson.findViewById(R.id.txtVaccine)).setText(String.format("Vaccine: %s", person.vaccine));
((TextView) viewPerson.findViewById(R.id.txtDate)).setText(String.format("Date: %s", person.date));
return viewPerson;
}
}
}
You should look into the Singleton pattern. It is very simple as there is no external DB. What it essentially is a class that manages the data and lets other classes and activities use the data while not allowing duplication.
You have a model Person
public class Person {
private String name;
public String address;
...
constructors and getters and setters
Create a class PersonsSingelton something like this.
public class PersonManagerSingleton {
private PersonManagerSingleton() {
loadPersonsDataSet();
}
private static PersonManagerSingleton instance = null;
public static PersonManagerSingleton getInstance() {
// if there is a instance already created use that instance of create new instance
// instance created in MainActivity and you try to create a new instance in Details
// should not happen as that will cause data duplication.
if (instance == null) {
instance = new PersonManagerSingleton();
}
return instance;
}
private ArrayList<Person> personList = new ArrayList<Person>();
private void loadPersonsDataSet() {
this.personList.add(new Person(...));
this.personList.add(new Person(...));
this.personList.add(new Person(...));
this.personList.add(new Person(...));
}
public ArrayList<Person> getpersonList() {
return personList;
}
public Person getPersonByID(int PersonNumber) {
for (int i = 0; i < this.personList.size(); i++) {
Person curPerson = this.personList.get(i);
if (curPerson.getNumber() == PersonNumber) {
return curPerson;
}
}
return null;
}
// methods for adding a new person used in Activity with the form.
// other methods ...
}
This would be like a state in React. of the State Manager.
Your person adapter constructor has to accept an ArrayList<Person> listPerson. So modify the activity passing the data to pass only the position of the ListView clicked. You need to modify your Adapter for that.
Use the Singleton created to access the data.
PersonManagerSingleton personSingelton = PersonManagerSingleton.getInstance();
ArrayList<Person> listPerson = personSingelton.getPersonList();
PersonAdapter Person = new PersonAdapter(listPerson);
So now only things left is to modify the Persons adapter to pass position using Intent and nothing else. and then you can use the instance of Singleton in other files to access the data using the listPerson.get(position) and using getters and setters.
Link to a project like this.
https://github.com/smitgabani/anroid_apps_using_java/tree/main/pokemon_app

How to stop replacing previous entries in shared preferences?

My code used to replace the previous entries and I realized I needed to use different keys for storing in shared preferences. Now my code does not output anything in the listview. please help
Java code where I ask for information about the person (name, favcolor, favfood)
public class personInfo extends AppCompatActivity {
EditText editText_name;
EditText editText_favfood;
EditText editText_favcolor;
Button button_save;
static int count = 0;
SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_personinfo);
Log.d(MainActivity.class.getSimpleName(), "onCreate");
editText_name = (EditText) findViewById(R.id.editText_name);
editText_favcolor = (EditText) findViewById(R.id.editText_favcolor);
editText_favfood = (EditText) findViewById(R.id.editText_favfood);
button_save = (Button) findViewById(R.id.button_save);
button_save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count++;
SharedPreferences sharedPreferences = getSharedPreferences("ENTRIES", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("name" + count, editText_name.getText().toString());
editor.putString("favcolor" + count, editText_favcolor.getText().toString());
editor.putString("favfood" + count, editText_favfood.getText().toString());
editor.apply();
editor.putInt("numOfEntries", count);
Intent it = new Intent(personInfo.this, listOfPeople.class);
startActivity(it);
}
});
}
}
Java code, page that is supposed to display the entries
public class listOfPeople extends AppCompatActivity {
ListView listView;
ArrayList<listEntry> list = new ArrayList<>();
listEntry le;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
listView = (ListView) findViewById(R.id.listView_persons);
SharedPreferences sharedPreferences = getSharedPreferences("ENTRIES", MODE_PRIVATE);
int count = sharedPreferences.getInt("numOfEntries", 0);
for(int i = 1; i <= count; i++){
String nameValue = sharedPreferences.getString("name" + i, "");
String favcolorValue = sharedPreferences.getString("favcolor" + i, "");
String favfoodValue = sharedPreferences.getString("favfood" + i, "");
le = new listEntry(nameValue, favcolorValue, favfoodValue);
list.add(le);
}
personListAdapter adapter = new personListAdapter(this, R.layout.entryrow, list);
listView.setAdapter(adapter);
}
}
You are applying (saving) preferences before putting count
editor.apply();
editor.putInt("numOfEntries", count);
Just put before applying
editor.putInt("numOfEntries", count);
editor.apply();
call editor.apply() after editor.putInt("numOfEntries", count);
editor.putInt("numOfEntries", count);
editor.apply();

Sending radio button values in Android

Here is my AddLift.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_lift);
final EditText Origin = (EditText)findViewById(R.id.addOrigin);
final EditText Destination =
(EditText)findViewById(R.id.addDestination);
final EditText Time = (EditText)findViewById(R.id.setTime);
final EditText Seats = (EditText)findViewById(R.id.numOfSeats);
final RadioGroup DriverLifter = (RadioGroup)findViewById(driverLifter);
final RadioButton selectedButton =
(RadioButton)findViewById(selectedButton);
Button submit = (Button)findViewById(R.id.submitButton);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String addOrigin = Origin.getText().toString();
String addDestination = Destination.getText().toString();
String setTime = Time.getText().toString();
String numOfSeats = Seats.getText().toString();
int selectedId = DriverLifter.getCheckedRadioButtonId();
selectedButton = (RadioButton)findViewById(selectedId);
Intent intent = new Intent(AddLift.this, ViewLiftBoard.class);
intent.putExtra("ORIGIN", addOrigin);
intent.putExtra("DESTINATION", addDestination);
intent.putExtra("TIME", setTime);
intent.putExtra("SEATS", numOfSeats);
intent.putExtra("RADIO", driverLifter);
startActivity(intent);
}
});
And here is my ViewLiftBoard.java:
public class ViewLiftBoard extends AppCompatActivity {
String Origin;
String Destination;
String Time;
String Seats;
int DriverLifter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_lift_board);
Origin = getIntent().getExtras().getString("ORIGIN");
Destination = getIntent().getExtras().getString("DESTINATION");
Time = getIntent().getExtras().getString("TIME");
Seats = getIntent().getExtras().getString("SEATS");
DriverLifter = getIntent().getExtras().getInt("RADIO");
TextView textView = (TextView)findViewById(R.id.textView);
textView.setText("Origin:"+ "
"+Origin+'\n'+"Destination:"+""+Destination+'\n'+"Time:"+"
"+Time+'\n'+"Number of Seats:"+" "+Seats+'\n'+"Type:"+" "+DriverLifter);
}
}
But when I run the app the "Type" comes up as a number rather than the value of the selected radio button? Any help would be great
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_lift);
final EditText Origin = (EditText)findViewById(R.id.addOrigin);
final EditText Destination =
(EditText)findViewById(R.id.addDestination);
final EditText Time = (EditText)findViewById(R.id.setTime);
final EditText Seats = (EditText)findViewById(R.id.numOfSeats);
final RadioGroup DriverLifter = (RadioGroup)findViewById(driverLifter);
final RadioButton selectedButton =
(RadioButton)findViewById(selectedButton);
Button submit = (Button)findViewById(R.id.submitButton);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String addOrigin = Origin.getText().toString();
String addDestination = Destination.getText().toString();
String setTime = Time.getText().toString();
String numOfSeats = Seats.getText().toString();
int selectedId = DriverLifter.getCheckedRadioButtonId();
selectedButton = (RadioButton)findViewById(selectedId);
String selectedradio=selectedButton.gettex();
Intent intent = new Intent(AddLift.this, ViewLiftBoard.class);
intent.putExtra("ORIGIN", addOrigin);
intent.putExtra("DESTINATION", addDestination);
intent.putExtra("TIME", setTime);
intent.putExtra("SEATS", numOfSeats);
intent.putExtra("RADIO", selectedradio);
startActivity(intent);
}
});
ViewLiftBoard.java:
public class ViewLiftBoard extends AppCompatActivity {
String Origin;
String Destination;
String Time;
String Seats;
String DriverLifter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_lift_board);
Origin = getIntent().getExtras().getString("ORIGIN");
Destination = getIntent().getExtras().getString("DESTINATION");
Time = getIntent().getExtras().getString("TIME");
Seats = getIntent().getExtras().getString("SEATS");
DriverLifter = getIntent().getExtras().getString("RADIO");
TextView textView = (TextView)findViewById(R.id.textView);
textView.setText("Origin:"+ "
"+Origin+'\n'+"Destination:"+""+Destination+'\n'+"Time:"+"
"+Time+'\n'+"Number of Seats:"+" "+Seats+'\n'+"Type:"+" "+DriverLifter);
}
}
If you want to get text from selected radio button, you can use next combination:
RadioGroup driverLifter = (RadioGroup)findViewById(R.id.driverLifter);
String selectedButtonText = ((RadioButton)findViewById(driverLifter.getCheckedRadioButtonId())).getText().toString();
By the way, according to Java naming conventions, your variables should start from lower case.
That's because you are retrieving an Integer DataType from the intent extra
DriverLifter= getIntent().getExtras().getInt("RADIO");
To solve your problem you have to put this extra as a string in the intent, so instead of doing this :
intent.putExtra("RADIO", driverLifter);
Do this :
intent.putExtra("RADIO", (RadioButton) findValueById(DriverLifter .getCheckedRadioButton()). getText(). toString()) ;
And extract the Extra like this :
DriverLifter= getIntent().getExtras().getString("RADIO");

How to save string sets to Shared Preferences

I have been able to save one entry to shared preferences and for it to display in a list view on another view but I am wanting to add multiple entries and them to display in the listview too. I thought I had the correct code but it doesn't see mto have changed anything. My intent is a favourites list, I take the entry data from one view and display it in another view.
SingleView Activity:
SharedPreferences.Editor fd;
SharedPreferences FeedPref;
private ArrayList<String> addArray = new ArrayList<>();
txt = (TextView) findViewById(R.id.name);
add = (Button) findViewById(R.id.btnAdd);
FeedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
fd = FeedPref.edit();
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String message = txt.getText().toString();
if (addArray.contains(message)) {
Toast.makeText((getBaseContext()), "Plant Already Added", Toast.LENGTH_LONG).show();
} else {
addArray.add(message);
FeedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
fd = FeedPref.edit();
fd.putInt("array_size", addArray.size());
for (int i = 0; i < addArray.size(); i++) {
fd.putString("Status_" + i, addArray.get(i));
}
fd.commit();
Toast.makeText((getBaseContext()), "Plant Added", Toast.LENGTH_LONG).show();
}
}
});
}
mygarden activity:
public class mygardenMain extends Activity {
//String[] presidents;
ListView listView;
//ArrayAdapter<String> adapter;
SharedPreferences FeedPref;
SharedPreferences.Editor fd;
//private ArrayList<String> addArray;
//public static final String PREFS = "examplePrefs";
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mygarden_list);
listView = (ListView) findViewById(R.id.mygardenlist);
//addArray = new ArrayList<>();
FeedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
int size = FeedPref.getInt("array_size", 0);
for (int i = 0; i < size; i++) {
String mess = FeedPref.getString("Status_" + i, null);
String[] values = new String[]{mess};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
listView.setAdapter(adapter);
}
}
Set abc = new HashSet<>();
abc.add("john");
abc.add("test");
abc.add("again");
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putStringSet("key",abc);
editor.commit();
SingleView Activity:
SharedPreferences.Editor fd;
SharedPreferences FeedPref;
private ArrayList<String> addArray = new ArrayList<>();
txt = (TextView) findViewById(R.id.name);
add = (Button) findViewById(R.id.btnAdd);
FeedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
fd = FeedPref.edit();
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String message = txt.getText().toString();
if (addArray.contains(message)) {
Toast.makeText((getBaseContext()), "Plant Already Added", Toast.LENGTH_LONG).show();
} else {
FeedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
fd = FeedPref.edit();
Gson gson = new Gson();
String jsonText = Prefs.getString("key", "");
if(!jsonText.equals(""))
{
String[] text = gson.fromJson(jsonText, String[].class); //EDIT: gso to gson
if(text.length>0)
{
//addArray = Arrays.asList(text);
//addArray = new ArrayList(addArray);
List<String> addArrayNew = Arrays.asList(text);
addArray = new ArrayList(addArrayNew);
}
}
addArray.add(message);
gson = new Gson();
jsonText = gson.toJson(addArray );
prefsEditor.putString("key", jsonText);
prefsEditor.commit();
}
});
}
mygarden activity:
public class mygardenMain extends Activity {
//String[] presidents;
ListView listView;
//ArrayAdapter<String> adapter;
SharedPreferences FeedPref;
SharedPreferences.Editor fd;
//private ArrayList<String> addArray;
//public static final String PREFS = "examplePrefs";
String jsonText;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mygarden_list);
listView = (ListView) findViewById(R.id.mygardenlist);
//addArray = new ArrayList<>();
FeedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
int size = FeedPref.getInt("array_size", 0);
Gson gson = new Gson();
jsonText = FeedPref.getString("key", "");
if(!jsonText.equals(""))
{
String[] values= gson.fromJson(jsonText, String[].class);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
listView.setAdapter(adapter);
}
}

database query returning all values instead matching the entered column detail

I am developing a android application where user can search his details by filling any of the column in multiple field form, no need to fill all the field, just has to enter any one column. But the Problem is instead of matching the entered field column it is displaying whole database.i tried filling 1 field , 2 field whatever i fill or even just click submit without entering anything it return whole database! where am i wrong?
Search.java
public class Search extends Activity {
SqlHandler sqlHandler;
EditText txtname, txt_relative_type, txt_father, txt_id, txt_part, txt_sl,
txt_age, txt_house, txt_poling, txt_section, txt_ac_name,
txt_ac_no;
ImageButton reset_btn, submit_btn;
RadioButton RdioBtn_male,RdioBtn_female;
ListView lvCustomList;
String intentname,intentacno,intentpartno,intentpoll,intentsl,intenthouse,intentAc_name,intentRelative,intentfather,intentage,intentsection;
String name1,acno,part,poll,sl,house,Ac_name,Relative,father,age,id,section,intentid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
txtname = (EditText) findViewById(R.id.editText);
txt_relative_type = (EditText) findViewById(R.id.editText3);
txt_father = (EditText) findViewById(R.id.editText1);
txt_id = (EditText) findViewById(R.id.editText4);
txt_part = (EditText) findViewById(R.id.editText5);
txt_sl = (EditText) findViewById(R.id.editText6);
txt_age = (EditText) findViewById(R.id.editText7);
txt_house = (EditText) findViewById(R.id.editText8);
txt_poling = (EditText) findViewById(R.id.editText9);
txt_section = (EditText) findViewById(R.id.editText10);
txt_ac_name = (EditText) findViewById(R.id.editText11);
txt_ac_no = (EditText) findViewById(R.id.editText12);
reset_btn = (ImageButton) findViewById(R.id.imageButton1);
submit_btn = (ImageButton) findViewById(R.id.imageButton2);
submit_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
name1 = txtname.getText().toString();
acno = txt_ac_no.getText().toString();
part = txt_part.getText().toString();
poll = txt_poling.getText().toString();
sl = txt_sl.getText().toString();
house = txt_house.getText().toString();
Ac_name = txt_ac_name.getText().toString();
Relative= txt_relative_type.getText().toString();
father = txt_father.getText().toString();
age = txt_age.getText().toString();
id = txt_id.getText().toString();
section = txt_section.getText().toString();
Intent i = new Intent (Search.this,Listdisplay.class);
i.putExtra(intentname, name1);
i.putExtra(intentacno, acno);
i.putExtra(intentpartno, part);
i.putExtra(intentpoll, poll);
i.putExtra(intentsl, sl);
i.putExtra(intentAc_name, Ac_name);
i.putExtra(intentRelative, Relative);
i.putExtra(intentfather, father );
i.putExtra(intentage, age);
i.putExtra(intentid, id);
i.putExtra(intentsection, section);
i.putExtra(intenthouse, house);
startActivity(i);
}
});
reset_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txtname.setText("");
txt_relative_type.setText("");
txt_father.setText("");
txt_id.setText("");
txt_part.setText("");
txt_sl.setText("");
txt_age.setText("");
txt_house.setText("");
txt_poling.setText("");
txt_section.setText("");
txt_ac_name.setText("");
txt_ac_no.setText("");
}
});
}}
Listdisplay.java
public class Listdisplay extends Activity {
ListView lvCustomList;
String intentname,intentacno,intentpartno,intentpoll,intentsl,intentAc_name,intentRelative,intentfather,intentage,intentid,intentsection,intenthouse;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchlayout);
Intent myIntent = getIntent();
String name_search = myIntent.getStringExtra(intentname);
String acno_search = myIntent.getStringExtra(intentacno);
String part_search = myIntent.getStringExtra(intentpartno);
String address_search = myIntent.getStringExtra(intentpoll);
String sl_search = myIntent.getStringExtra(intentsl);
String area_search = myIntent.getStringExtra(intentAc_name);
String guardian_search = myIntent.getStringExtra(intentRelative);
String father_search = myIntent.getStringExtra(intentfather);
String age_search1 = myIntent.getStringExtra(intentage);
String idcard_search = myIntent.getStringExtra(intentid);
String section_search = myIntent.getStringExtra(intentsection);
String house_search = myIntent.getStringExtra(intenthouse);
lvCustomList = (ListView) findViewById(R.id.listView1);
showList(name_search,acno_search,part_search,address_search,sl_search,area_search,guardian_search,father_search,age_search1,idcard_search,section_search,house_search);
}
private void showList(String name_search,String acno_search,String part_search, String address_search,String sl_search,String area_search,String guardian_search,String father_search,String age_search1,String idcard_search,String section_search,String house_search ) {
SqlHandler sql = new SqlHandler(this);
ArrayList<contactlistitems> contactlist = new ArrayList<contactlistitems>();
contactlist.clear();
String query="SELECT * FROM VoterSearch WHERE name LIKE (case when '"+name_search+"'!='' then '%"+name_search+"%' else name end) and PollAddress LIKE (case when '"+address_search+"'!='' then '%"+address_search+"%' else PollAddress end) and Relation LIKE (case when '"+guardian_search+"'!='' then '%"+guardian_search+"%' else Relation end) and FatherName LIKE (case when '"+father_search+"'!='' then '%"+father_search+"%' else FatherName end) and AcName LIKE (case when '"+area_search+"'!='' then '%"+area_search+"%' else AcName end)"
+" and Section LIKE (case when '"+section_search+"'!='' then '%"+section_search+"%' else Section end)"+" and IdCard LIKE (case when '"+idcard_search+"'!='' then '%"+idcard_search+"%' else IdCard end)"+" and SlNo = (case when '"+sl_search+"'!='' then '"+sl_search+"' else SlNo end) and AcNo = (case when '"+acno_search+"'!='' then '"+acno_search+"' else AcNo end) and PartNo = (case when '"+part_search+"'!='' then '"+part_search+"' else PartNo end)"
+" and HouseNo LIKE (case when '"+house_search+"'!='' then '%"+name_search+"%' else HouseNo end)"+" and Age BETWEEN (case when '"+age_search1+"'!='' then '"+age_search1+"' else Age end)"+" AND (case when '"+age_search1+"'!='' then '"+age_search1+"' else Age end)";
Cursor c1 = sql.selectQuery(query);
Log.i("Error","error");
if(c1 != null && c1.getCount() != 0)
{
Log.i("Error","error");
if(c1.moveToFirst()) {
do {
contactlistitems contactlistitems = new contactlistitems();
contactlistitems.setname(c1.getString(c1.getColumnIndex("name")));
contactlistitems.setrelative_type(c1.getString(c1.getColumnIndex("Relation")));
contactlistitems.setfather(c1.getString(c1.getColumnIndex("FatherName")));
contactlistitems.setid(c1.getString(c1.getColumnIndex("IdCard")));
contactlistitems.sethouse(c1.getString(c1.getColumnIndex("HouseNo")));
contactlistitems.setsl(c1.getString(c1.getColumnIndex("SlNo")));
contactlistitems.setage(c1.getString(c1.getColumnIndex("Age")));
contactlistitems.setpoling(c1.getString(c1.getColumnIndex("PollAddress")));
contactlistitems.setsection(c1.getString(c1.getColumnIndex("Section")));
contactlistitems.setac_name(c1.getString(c1.getColumnIndex("AcName")));
contactlistitems.setac_no(c1.getString(c1.getColumnIndex("AcNo")));
contactlistitems.setpart(c1.getString(c1.getColumnIndex("PartNo")));
contactlistitems.setSex(c1.getString(c1.getColumnIndex("Sex")));
contactlist.add(contactlistitems);
} while (c1.moveToNext());
}
}
c1.close();
ContactListAdapter contactListAdapter = new ContactListAdapter(Listdisplay.this, contactlist);
lvCustomList.setAdapter(contactListAdapter);
}
}
ContactListAdapter.java
public class ContactListAdapter extends BaseAdapter {
Context context;
ArrayList<contactlistitems> contactlist;
LayoutInflater mInflater;
public ContactListAdapter(Context context, ArrayList<contactlistitems> list) {
mInflater = LayoutInflater.from(context);
this.context = context;
contactlist = list;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return contactlist.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return contactlist.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.textView1);
holder.house = (TextView) convertView.findViewById(R.id.textView2);
holder.father = (TextView) convertView.findViewById(R.id.textView3);
holder.sex = (TextView) convertView.findViewById(R.id.textView4);
holder.part = (TextView) convertView.findViewById(R.id.textView5);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(contactlist.get(position).getname());
holder.house.setText(contactlist.get(position).gethouse());
holder.father.setText(contactlist.get(position).getfather());
holder.sex.setText(contactlist.get(position).getSex());
holder.part.setText(contactlist.get(position).getpart());
return convertView;
}
static class ViewHolder
{
TextView name,house,father,sex,part;
}
}
I believe that the query is perfectly right! dont know from where its going wrong, is it passing values from class search to listadisplay through intent causing problem am not sure. Kindly help with this
When i tried with the live database the query was working fine. problem was whole data base was returned instead of match, hence the query may be returning null, now have to look for code. When i tested the value which i got through getIntent() it was not receiving value, hence the problem was with intent, value was not sent. error was just to enclose double quote in intent and getintent
at Search.java double quote parameter
i.putExtra("intentname", name1);
at ListDisplay.java
String name_search = myIntent.getStringExtra("intentname");
And it worked

Categories

Resources