• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problem with onItemClickListener and listView

Hey Folks I'm quite new to programming in general and am stuck on this matter. I've set up a listview to read from a database with 4 selections - Daily, Weekly, Monthly, and Yearly. I want to launch a different Activity for each one of the list items. When I click on an item it retrieves the correct string value but my 'if' statements are not recognizing them. Could someone point out where I'm going wrong? Here is the code for my onItemClickListener..........

listView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long itemId) {

String item = String.valueOf(itemId);

String vals[] = {item};
String cols[] = {"length_name"};
Cursor c = mDb.query("length", cols, "_id=?", vals, null, null, null);

if (c.moveToFirst()){

String item2 = c.getString(0);

if(item2 == "Daily") {

Intent myIntent = new Intent(getApplicationContext(), DateActivity.class);
startActivity(myIntent);
}

else if(item2 == "Weekly"){

Intent myIntent = new Intent(getApplicationContext(), MonthYearActivity.class);
startActivity(myIntent);

}

else if(item2 == "Monthly"){

Intent myIntent = new Intent(getApplicationContext(), MonthYearActivity.class);
startActivity(myIntent);



}

else if(item2 == "Yearly"){

Intent myIntent = new Intent(getApplicationContext(), YearActivity.class);
startActivity(myIntent);


}
}

c.close();
}
});
 
I'm going to venture a guess and say its Java programming issue. Someone correct me if I'm wrong (I'm still learning) but String variables are actually objects. So you can't just do a straight string comparison.

So your if statements for string need to look like this

String myString = "Hello"

if (myString.equals("Hello")) {
//do as I command
}

So you are creating a String object that has the value "Hello" and call the method to do a comparison. Strings aren't variables, they are objects

Good luck.
 
Back
Top Bottom