I have a list view, this setup is like the guess that logo game where you can't proceed with the next level until you finish the level before that. I have been searching all night already, I found examples and questions about disabling other items in listview but I cannot find the specific answer that I can use in my problem. Now I am asking. how to disable other items in listview? and enable them if the level before them is complete, I'm also trying to achieve this by getting the "level status" from sqlite database.
This is the jave code extends ListActivity:
static final String[] LEVELS = new String[] { "Level 1", "Level 2", "Level 3",
"Level 4", "Level 5", "Level 6", "Level 7", "Level 8", "Level 9", "Level 10" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_play, LEVELS));
final ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(
AdapterView<?> parent, View v, int position, long id) {
//Toast.makeText(getApplicationContext(), ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
switch(position) {
case 0:
Intent lvlOne = new Intent(".LevelOneActivity");
startActivity(lvlOne);
break;
case 1:
Intent lvlTwo = new Intent(".LevelTwoActivity");
startActivity(lvlTwo);
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
default:
break;
}
}
});
}
I think that you should just make easy class which will hold your level and class which it should start
public class ListItemInfo
{
String mActivityToStart;
int mLevel
}
then just add a TAG to each of list items which you will hold in adapter when creating a view
after that
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(
AdapterView<?> parent, View v, int position, long id)
{
if (#DB GET UNLOCKED LVL) >= ((ListItemInfo)v.getTag()).mActivityToStart)
{
Intent lvlOne = new Intent(getContext(), ((ListItemInfo)v.getTag()).mLevel);
startActivity(lvlOne);
}
});
Related
public class MainActivity extends AppCompatActivity {
Spinner s1,s2;
TextView t1,t2,t3;
String ops1,ops2;
String[] op1={"Inclusive","+3%","+5%","+12%","+18%","+28%"};
String[] op2={"Exclusive","-3%","-5%","-12%","-18%","-28%"};
ArrayAdapter<String> adapter,adapter2;
EditText e1,et1,et2,et3;
Button b1;
double a,b,c,x;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s1 = (Spinner) findViewById(R.id.s1);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, op1);
s1.setAdapter(adapter);
s2 = (Spinner) findViewById(R.id.s2);
adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, op2);
s2.setAdapter(adapter2);
t1=(TextView)findViewById(R.id.tc);
t2=(TextView)findViewById(R.id.ts);
t3=(TextView)findViewById(R.id.ti);
e1=(EditText)findViewById(R.id.e1) ;
et1=(EditText)findViewById(R.id.et1);
et2=(EditText)findViewById(R.id.et2);
et3=(EditText)findViewById(R.id.et3);
b1=(Button)findViewById(R.id.bt1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t1.setText("CGST");
t2.setText("SGST");
t3.setText("IGST");
}
});
s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position)
{
case 0:
t1.setText("CGST(1.5%)");
t2.setText("SGST(1.5%)");
t3.setText("IGST(3%)");
cal1(); //error pop due to this
break;
case 1:
t1.setText("CGST(2.5%)");
t2.setText("SGST(2.5%)");
t3.setText("IGST(5%)");
break;
case 2:
t1.setText("CGST(6%)");
t2.setText("SGST(6%)");
t3.setText("IGST(12%)");
break;
case 3:
t1.setText("CGST(9%)");
t2.setText("SGST(9%)");
t3.setText("IGST(18%)");
break;
case 4:
t1.setText("CGST(14%)");
t2.setText("SGST(14%)");
t3.setText("IGST(28%)");
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
s2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position)
{
case 0:
t1.setText("CGST(-1.5%)");
t2.setText("SGST(-1.5%)");
t3.setText("IGST(-3%)");
break;
case 1:
t1.setText("CGST(-2.5%)");
t2.setText("SGST(-2.5%)");
t3.setText("IGST(-5%)");
break;
case 2:
t1.setText("CGST(-6%)");
t2.setText("SGST(-6%)");
t3.setText("IGST(-12%)");
break;
case 3:
t1.setText("CGST(-9%)");
t2.setText("SGST(-9%)");
t3.setText("IGST(-18%)");
break;
case 4:
t1.setText("CGST(-14%)");
t2.setText("SGST(-14%)");
t3.setText("IGST(-28%)");
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void cal1()
{
x=Double.parseDouble(e1.getText().toString()); //from here
}
}
please look to // error like it crashes the app
Android code I am like trying to calculate but when I initialize cal1() method in item selection listener case:0 it crashes please tell me the solution
After getting the solution please give a certain suggestion
After going on a different method it again crashes
->In your code change your switch cases from {0,1,2,3,4} to {1,2,3,4,5} because position 0 is for the index 0th of your array.
s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position)
{
case 1:
t1.setText("CGST(1.5%)");
t2.setText("SGST(1.5%)");
t3.setText("IGST(3%)");
cal1(); //error pop due to this
break;
case 2:
t1.setText("CGST(2.5%)");
t2.setText("SGST(2.5%)");
t3.setText("IGST(5%)");
break;
case 3:
t1.setText("CGST(6%)");
t2.setText("SGST(6%)");
t3.setText("IGST(12%)");
break;
case 4:
t1.setText("CGST(9%)");
t2.setText("SGST(9%)");
t3.setText("IGST(18%)");
break;
case 5:
t1.setText("CGST(14%)");
t2.setText("SGST(14%)");
t3.setText("IGST(28%)");
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
s2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position)
{
case 1:
t1.setText("CGST(-1.5%)");
t2.setText("SGST(-1.5%)");
t3.setText("IGST(-3%)");
break;
case 2:
t1.setText("CGST(-2.5%)");
t2.setText("SGST(-2.5%)");
t3.setText("IGST(-5%)");
break;
case 3:
t1.setText("CGST(-6%)");
t2.setText("SGST(-6%)");
t3.setText("IGST(-12%)");
break;
case 4:
t1.setText("CGST(-9%)");
t2.setText("SGST(-9%)");
t3.setText("IGST(-18%)");
break;
case 5:
t1.setText("CGST(-14%)");
t2.setText("SGST(-14%)");
t3.setText("IGST(-28%)");
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
-> always try to debug your code and find out errors. this will help to easily find out your errors.
Can someone please help me. I have just started Android development and I want to create an app which has a drop down menu. There seems to be two errors in my code which I am unable to resole. The errors are at implements OnItemSelectedListner and at spinner.setOnItemSelectedListener(this);
public class MainActivity extends ActionBarActivity implements OnItemSelectedListener{
int Cups = 1;
int Price = 1;
int Sum = 0;
private Spinner spinner;
private static final String[]paths = {"item 1", "item 2", "item 3", "item 4", "item 5"};
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String>adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,paths);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
switch (position)
{
case 0:
Price=0.5;
break;
case 1:
Price=1;
break;
case 2:
Price=2;
break;
case 3:
Price=3;
break;
case 4:
Price=4;
break;
}
}
The first Error is to implement the correct OnItemClickListener
implements AdapterView.OnItemClickListener
The second is to override the correct method from the listener which is
#Override
onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
I have a question about list view which confused me completely.
I am going to design a app for play songs my favortie singer and below is my implements:
AlbumActivity----->ItemActivity(contain songs)---->PlayerActivity
This is my Album activity code:
public class AlbumActivity extends Activity implements Variable {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.album_list);
populatecantactlist();//fill listview with my item
registerClickCallback();//What to do when click on item
}
private void populatecantactlist() {
ListView listview=(ListView)findViewById(R.id.list);
ArrayAdapter<String> adapter=new mylistAdapter();
listview.setAdapter(adapter);
}
public class mylistAdapter extends ArrayAdapter<String>{
private mylistAdapter(){
super(AlbumActivity.this,R.layout.album_list_item,albumnames);
}
#Override
public View getView(int position,View convertView,ViewGroup parent){
View item_view=convertView;
if(item_view== null){ item_view=getLayoutInflater().inflate(R.layout.album_list_item,parent,false);
}
ImageView iv=(ImageView)item_view.findViewById(R.id.imageView1);
iv.setImageResource(icon_num[position]);
TextView tv1=(TextView)item_view.findViewById(R.id.txt_name);
tv1.setText(albumnames[position]);
return item_view;}
}
private void registerClickCallback() {
ListView list=(ListView)findViewById(R.id.list);
list.setOnItemClickListener(new OnItemClickListener() {
//I want to after on each item select go to new activity which populate
//information about new album
#Override
public void onItemClick(AdapterView<?> parent, View viewclicked,
int position, long id) {
Intent i2=new Intent("com.example.ItemActivity");
Bundle extras=new Bundle();
extras.putInt("PositionItemClicked", position);
i2.putExtras(extras);
startActivity(i2);
}});
and this is also my item Activity:
note:I define album name first in arrays with relate names.
String [] names;
int icon_num;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.album_list);
Bundle bundle=getIntent().getExtras();
int i=bundle.getInt("PositionItemClicked");
switch (i){
case 0:
names=name1;
icon_num=R.drawable.name1;
break;
case 1:
names=name2;
icon_num=R.drawable.name2;
break;
case 2:
names=name3;
icon_num=R.drawable.name3;
break;
case 3:
names=name4;
icon_num=R.drawable.name4;
break;
case 4:
names=name5;
icon_num=R.drawable.name5;
break;
case 5:
names=name6;
icon_num=R.drawable.name6;
break;
case 6:
names=name7;
icon_num=R.drawable.name7;
break;
case 7:
names=name8;
icon_num=R.drawable.name8;
break;
case 8:
names=name9;
icon_num=R.drawable.name9;
break;
case 9:
names=name10;
icon_num=R.drawable.name10;
break;
case 10:
names=name11;
icon_num=R.drawable.name11;
break;
case 11:
names=name12;
icon_num=R.drawable.name12;
break;
}
populatecantactlist();//fill listview with my item
registerClickCallback();//What to do when click on item
}
public void populatecantactlist() {
ListView listview=(ListView)findViewById(R.id.list);
ArrayAdapter<String> adapter=new mylistAdapter();
listview.setAdapter(adapter);
}
public class mylistAdapter extends ArrayAdapter<String>{
private mylistAdapter(){
super(ItemActivity.this,R.layout.album_list_item,names);
}
#Override
public View getView(int position,View convertView,ViewGroup parent){
View item_view=convertView;
if(item_view== null){
item_view=getLayoutInflater().inflate(R.layout.album_list_item,parent,false);
}
ImageView iv=(ImageView)item_view.findViewById(R.id.imageView1);
iv.setImageResource(icon_num);
TextView tv1=(TextView)item_view.findViewById(R.id.txt_name);
tv1.setText(names[position]);
return item_view;
}
}
private void registerClickCallback() {
final ListView list=(ListView)findViewById(R.id.list);
list.setOnItemClickListener(new OnItemClickListener() {
//I want to after on each item select go to new activity which populate with
//information about new album
#Override
public void onItemClick(AdapterView<?> parent, View viewclicked,
int position, long id) {
Intent i3=new Intent("com.example.PlayerActivity");
Bundle extras=new Bundle();
extras.putInt("i",list.getCount());
i3.putExtras(extras);
startActivity(i3);
}});
and it is my PlayerActivity:
public class PlayerActivity extends Activity implements Variable {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player_layout);
Bundle bundle=getIntent().getExtras();
int i=bundle.getInt("i");
Toast.makeText(PlayerActivity.this,"hello"+i,Toast.LENGTH_LONG).show();
}}
But i can't know item selected in itemactivity belong to what album(means Album Activity)?And now After one day i Don't know what to do?
Do you think it is a complex manner ,can you guide me to a better manner?
Thanks.
Just change item activity to this:
String [] names;
int icon_num;
Bundle bundle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.album_list);
bundle=getIntent().getExtras();
int i=bundle.getInt("PositionItemClicked");
switch (i){
case 0:
names=name1;
icon_num=R.drawable.name1;
break;
case 1:
names=name2;
icon_num=R.drawable.name2;
break;
case 2:
names=name3;
icon_num=R.drawable.name3;
break;
case 3:
names=name4;
icon_num=R.drawable.name4;
break;
case 4:
names=name5;
icon_num=R.drawable.name5;
break;
case 5:
names=name6;
icon_num=R.drawable.name6;
break;
case 6:
names=name7;
icon_num=R.drawable.name7;
break;
case 7:
names=name8;
icon_num=R.drawable.name8;
break;
case 8:
names=name9;
icon_num=R.drawable.name9;
break;
case 9:
names=name10;
icon_num=R.drawable.name10;
break;
case 10:
names=name11;
icon_num=R.drawable.name11;
break;
case 11:
names=name12;
icon_num=R.drawable.name12;
break;
}
populatecantactlist();//fill listview with my item
registerClickCallback();//What to do when click on item
}
public void populatecantactlist() {
ListView listview=(ListView)findViewById(R.id.list);
ArrayAdapter<String> adapter=new mylistAdapter();
listview.setAdapter(adapter);
}
public class mylistAdapter extends ArrayAdapter<String>{
private mylistAdapter(){
super(ItemActivity.this,R.layout.album_list_item,names);
}
#Override
public View getView(int position,View convertView,ViewGroup parent){
View item_view=convertView;
if(item_view== null){
item_view=getLayoutInflater().inflate(R.layout.album_list_item,parent,false);
}
ImageView iv=(ImageView)item_view.findViewById(R.id.imageView1);
iv.setImageResource(icon_num);
TextView tv1=(TextView)item_view.findViewById(R.id.txt_name);
tv1.setText(names[position]);
return item_view;
}
}
private void registerClickCallback() {
final ListView list=(ListView)findViewById(R.id.list);
list.setOnItemClickListener(new OnItemClickListener() {
//I want to after on each item select go to new activity which populate with
//information about new album
#Override
public void onItemClick(AdapterView<?> parent, View viewclicked,
int position, long id) {
Intent i3=new Intent("com.example.PlayerActivity");
Bundle extras=new Bundle();
extras.putInt("i",bundle.getInt("PositionItemClicked");
i3.putExtras(extras);
startActivity(i3);
}});
I am developing an app that uses ActionBar tabs to display a list of options through ListFragment. The list (and ListFragment) display without a problem, but the ListView's setOnItemClickListener doesn't seems to work, as nothing happens when an item in the list is clicked. Here's the code for the ListFragment class:
package XXX.XXX;
public class AboutFrag extends SherlockListFragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.aboutfrag, container, false);
ListView lv = (ListView) view.findViewById(android.R.id.list);
String[] items = new String[] {"About 1", "About 2", "About 3"};
lv.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.list_item, items));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position)
{
case 0:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
startActivityForResult(browserIntent, 0);
break;
case 1:
Intent browserIntent2 = new Intent(Intent.ACTION_VIEW, Uri.parse("http://wikipedia.org"));
startActivityForResult(browserIntent2, 0);
break;
case 2:
Intent browserIntent3 = new Intent(Intent.ACTION_VIEW, Uri.parse("http:/android.com");
startActivityForResult(browserIntent3, 0);
break;
}
}
});
return view;
}
}
I'm assuming it does not work because the class returns the view object, so the FragmentActivity can't run the listener code, so does anyone know how to make this work? By the way, I am using ActionBarSherlock. Thanks in advance!!!
You can also override a onListItemClick method that is inherited from the SherlockListFragment.
As follows:
#Override
public void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
switch (position)
{
case 0:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
startActivityForResult(browserIntent, 0);
break;
case 1:
Intent browserIntent2 = new Intent(Intent.ACTION_VIEW, Uri.parse("http://wikipedia.org"));
startActivityForResult(browserIntent2, 0);
break;
case 2:
Intent browserIntent3 = new Intent(Intent.ACTION_VIEW, Uri.parse("http:/android.com");
startActivityForResult(browserIntent3, 0);
break;
}
}
I'm using a custom ListView with a Title and a Subtitle where you can read a brief explanation of the item.
For each item on the list, I'm displaying an AlertDialog to select an option (different for each case). When the option is selected, i want to change the Subtitle for the option selected by the user.
This is what I've tried:
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position){
case 0:
final CharSequence[] alertText1 = {"Area 1", "Area 2", "Area 3"};
ventana.setTitle("Choose an Area");
ventana.setItems(alertText1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
TextView subTitulo = (TextView) findViewById(R.id.subTitulo);
subTitulo.setText(alertText1[item]);
}
});
ventana.show();
break;
case 1:
final CharSequence[] alertText2 = {"1", "2", "3", "5", "10", "20", "60"};
ventana.setTitle("Max. duration");
ventana.setItems(alertText2, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
TextView subTitulo = (TextView) findViewById(R.id.subTitulo);
subTitulo.setText(alertText2[item]);
}
});
ventana.show();
break;
case 2:
final CharSequence[] alertText3 = {"3", "5", "10", "20", "30", "60"};
ventana.setTitle("Time between events");
ventana.setItems(alertText3, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
TextView subTitulo = (TextView) findViewById(R.id.subTitulo);
subTitulo.setText(alertText3[item]);
}
});
ventana.show();
break;
For the first item on the list it works fine, when I select an option, the subtitle get replaced by that option, but when I make a selection in the AlertDialogs of the other 2 items, the option selected replaces the subtitle of the first item!
Any idea of how can I fix that?
Since nobody answered the question and i find a solution, im going to publish it here to help other people who eventually can be facing the same problem or a similar one :D
I just remove the TextView subTitulo = (TextView) findViewById(R.id.subTitulo); from each case and added it before the switch starts but "taking" the view argument on the onClick function (the type final, is because Eclipse warned me about it :P) : final TextView subTitulo = (TextView) view.findViewById(R.id.subTitulo);
The code looks like this:
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView subTitulo = (TextView) view.findViewById(R.id.subTitulo);
switch(position){
case 0: final CharSequence[] alertText1 = {"Area 1", "Area 2", "Area 3"};
ventana.setTitle("Choose an Area");
ventana.setItems(alertText1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
subTitulo.setText(alertText1[item]);
}
});
ventana.show();
break;
[...]