List View selection to start a new Activity - android

So I'm working with a list view trying to get specific activities to open when a certain item is selected. This is the code that I'm working with right now. I know its wrong but am not sure what I need to change it make it work. I think I need an intent and tried a few ideas but did not come up with much. Thanks.
package table.periodic;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class List extends ListActivity{
String[] elements={"actinium", "aluminum", "americium",
"antimony", "argon", "arsenic", "astatine", "barium",
"berkelium", "beryllium", "bismuth", "bohrium", "boron",
"bromine", "cadmium", "calcium", "californium", "carbon",
"cerium", "cesium", "chlorine", "chromium", "cobalt",
"copper", "copernicium", "curium", "darmstadtium", "dubnium",
"dysprosium", "einsteinium", "erbium", "europium", "fermium",
"fluorine", "francium", "gadolinium", "gallium", "germanium",
"gold", "hafnium", "hassium", "helium", "holmium",
"hydrogen", "indium", "iodine", "iridium", "iron",
"krypton", "lanthanum", "lawrencium", "lead", "lithium",
"lutetium", "magnesium", "manganese", "meitnerium", "mendelevium",
"mercury", "molybdenum", "neodymium", "neon", "neptunium",
"nickel", "niobium", "nitrogen", "nobelium", "osmium",
"oxygen", "palladium", "phosphorus", "platinum", "plutonium",
"polonium", "potassium", "praseodymium", "promethium", "protactinium",
"radium", "radon", "rhenium", "rhodium", "roentgenium", "rubidium",
"ruthenium", "rutherfordium", "samarium", "scandium", "seaborgium",
"selenium", "silicon", "silver", "sodium", "strontium", "sulfur",
"tantalum", "technetium", "tellurium", "terbium", "thallium", "thorium",
"thulium", "tin", "titanium", "tungsten", "ununhexium", "ununoctium",
"ununpentium", "ununquadium", "ununseptium", "ununtrium", "uranium",
"vanadium", "xenon", "ytterbium", "yttrium", "zinc", "zirconium"};
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.list);
setListAdapter(new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1,
elements));
}
public void onListItemClick(ListView parent, View v, int position,
long id){
if ("actinium".equals(elements[position]))
{startActivity(Actinium.class);}
else if ("aluminum".equals(elements[position]))
{startActivity(Aluminum.class);}
}

I've edited your code a bit. You need to create one ListView element in your layout XML file, and then in your Java file, find it using findViewById:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class List extends Activity {
String[] elements = { … }; // (omitted for better legibility)
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
ListView lv = (ListView)findViewById(R.id.ListView01);
lv.setAdapter(new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1,
elements));
}
public void onListItemClick(ListView parent, View v, int position,
long id) {
//if ("actinium".equals(elements[position]))
// {startActivity(Actinium.class);}
//else if ("aluminum".equals(elements[position]))
// {startActivity(Aluminum.class);}
}
}

Related

How to disable a single row and change its background color in a listview, without a button (or a method if possible)?

The title of this post says it all.
This code works without any problems:
package abc.AvailableCars;
import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class carListActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.car_list_layout);
final ListView carListview = (ListView) findViewById(R.id.list_view);
final Button dButton = (Button) findViewById(R.id.disable_button);
String[] cars = {"Maxima GXE", "Passat", "Focus SE", "Mazda6", "Avalon", :Sentra GXE"};
final List<String> list_of_cars = new ArrayList<String>(Arrays.asList(cars));
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, list_of_cars);
carListview.setAdapter(arrayAdapter);
dButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int chosenRow = 3;
carListview.getChildAt(3).setEnabled(false);
carListview.getChildAt(3).setBackgroundColor(Color.parseColor("#3f51b5"));
}
});
}
}
This is in my listview .xml file:
<Button
android:id="#+id/disable_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Disable A Row"
/>
But, when I comment-out everything that belongs to the button, like below, and the Car List class is called, the app crashes with the error in the Logcat:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setEnabled(boolean)' on a null object reference:
final ListView carListview = (ListView) findViewById(R.id.list_view);
//final Button dButton = (Button) findViewById(R.id.disable_button);
String[] cars = {"Maxima GXE", "Passat", "Focus SE", "Mazda6", "Avalon"};
final List<String> list_of_cars = new ArrayList<String>(Arrays.asList(cars));
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, list_of_cars);
carListview.setAdapter(arrayAdapter);
//dButton.setOnClickListener(new View.OnClickListener() {
//#Override
//public void onClick(View v) {
int chosenRow = 3;
carListview.getChildAt(chosenRow).setEnabled(false);
carListview.getChildAt(chosenRow).setBackgroundColor(Color.parseColor("#3f51b5"));
}
//});
}
//}
I'm not an Android newbie anymore, but this is eluding me.
I want the chosen row to be disabled and the color set as soon as the listview is shown.
How can I do this programmatically without a button?
I have tried every variation I can think of, getView(), even a fake click.
Just in case it makes a difference, this code is in a separate class and file than the MainActivity.java file, and is called in that file.
There has to be a simple answer. What do I need to change?
Please be verbose.
Thank you.
You are calling carListview.getChildAt(chosenRow) when you set up your list view, in onCreate. Your list view is not ready yet. Try moving this code to your onResume - should look something like this:
#Override
public void onResume(){
super.onResume();
arrayAdapter.notifyDataSetChanged();
int chosenRow = 3;
carListview.getChildAt(chosenRow).setEnabled(false);
carListview.getChildAt(chosenRow).setBackgroundColor(Color.parseColor("#3f51b5"));
}
This is a pretty simple case - your chosenRow number is generated by you. You might need a custom Adapter if you need it to be algorithmic or user-driven. Have a look at this tutorial.
From my understanding, since listViews are views, they have to be Overridden for some things to be changed in them.
I chose not to disable the required rows, but check for them in code.
The complete code that works is below.
Some credit goes to Raghunandan for his/her answer at-
Android - Change background color of specific item of ListView
Again, sorry, but the indentation of the code wouldn't work correctly for some reason.
import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CarListActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.car_list_layout);
final ListView carListview = (ListView) findViewById(R.id.list_view);
String[] cars = {"European Cars:", "Mercedes", "Passat", "Bently", "Porsche", "BMW", "Yugo","Land Rover",
"Japanese Cars:", "Maxima GXE", "Mazda6", "Avalon", "Toyota", "Honda", ""};
final List<String> list_of_cars = new ArrayList<String>(Arrays.asList(cars));
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, list_of_cars);
//------------------------------------------
carListview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list_of_cars) {
// Since listViews are views, they have to be Overrdden for some things to be changed in them.
#Override
public View getView(int rowPosition, View convertView, ViewGroup parent) {
View row = super.getView(rowPosition, convertView, parent);
//------------------------------------------
// This works. I have only tried this for two rows, the two I wanted. I expected this line to crash the app, but it didn't.
if(getItem(rowPosition).equals("European Cars:") || getItem(rowPosition).equals("Japanese Cars:")) {
// Make the two rows have a white background color.
row.setBackgroundColor(Color.WHITE); // this command WORKS fine by itself.
// row.setEnabled(false); this command caused "bleeding" over into other rows, so I will check for the rows in a condition.
} // both of the getItems end here.
else {
// All of the other rows should have this color.
row.setBackgroundColor(Color.parseColor("#EEE8AA"));
// the default color
} // else ends here.
//------------------------------------------
return row;
//------------------------------------------
} // getView ends here.
}); // carListview.setAdapter ends here.
} // onCreate ends here.
} // CarListActivity ends here.
Thanks, and I hope this helps others.

In listview not displaying some languages but for the same code in other device it is showing

ChooseLanguage.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import picker.novasyslabs.com.picker.R;
import picker.novasyslabs.com.picker.SplashScreen;
import picker.novasyslabs.com.picker.Volley.RegisterUser;
public class ChooseLanguage extends AppCompatActivity {
ArrayAdapter adapter;
ListView choose_lang;
String[] languagelist;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
languagelist = getResources().getStringArray(R.array.Langusges);
setContentView(R.layout.activity_choose_language);
adapter = new ArrayAdapter<String>(this,R.layout.language_list,languagelist);
choose_lang = (ListView) findViewById(R.id.languages);
choose_lang.setAdapter(adapter);
choose_lang.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SplashScreen.Language = parent.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(),SplashScreen.Language,Toast.LENGTH_SHORT).show();
RegisterUser registerUser = new RegisterUser();
}
});
}
}
strings.xml
<array name="Langusges">
<item>हिंदी</item>
<item>తెలుగు</item>
<item>தமிழ்</item>
<item>മലയാളം</item>
<item>ಕನ್ನಡ</item>
<item>English</item>
<item>मराठी</item>
<item>বাংলা</item>
<item>ગુજરાતી</item>
<item>ਪੰਜਾਬੀ</item>
<item>ଓଡିଆ</item>
<item>भोजपुरी</item>
<item>नेपाली</item>
<item>हरयनवी</item>
<item>कश्मीरी</item>
<item>অসমীয়া</item>
<item>कोंकणी</item>
</array>
The result as I'm getting:
All languages are displaying but as shown in this image 3 languages are not displaying and even when I click on that item blank will be toasted and when I click on other languages then shows the clicked languages.In my tab it is not showing 3 languages and in other sony phone it is showing other languages only one languages is showing but that is in another format.
any help will be appreciated.
Thanks!
Your tab does not support these languages. But the other devices in which you are testing does.

Android: Listview select items

Using android studio and I have a large product list for a grocery store and want to be able to select items from that list. I then want the selected items to appear in another listview such as a users shopping cart.
#Updated
I have now created a checkable list view. How can I take the checked boxes and add them to another activity/listview that I have created.
enter code here import android.os.Bundle;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ListView;
import android.view.View;
public class MainActivity extends AppCompatActivity{
ArrayList<String> selectedItems=new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView chl=(ListView)findViewById(R.id.checkable_list);
chl.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
String[] items={"Bacon","Sports Drinks","Detergent","Towels","Frozen
Pizza","Water"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>
(this,R.layout.rowlayout,R.id.txt_title,items);
chl.setAdapter(adapter);
chl.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItem=((TextView)view).getText().toString();
if (selectedItem.contains(selectedItem)) {
selectedItems.remove(selectedItem);
}
else
selectedItems.add(selectedItem);
}
});
}
public void showSelectedItems(View view){
String items="";
for (String item:selectedItems){
items+="-"+item+"\n";
}
Toast.makeText(getApplicationContext(),"Added To Your Cart",Toast.LENGTH_SHORT).show();}}
Please give me MORE information for your Question.
First of all
You have to make a custom listview that call mOneListView and set method OnItemClickListener to mOneListView's ListViewAdapter.
and make another mTwoListView be called by mOneListView's Item.
I think your problem is do not know how to make a custom listview and make a listener for Item of List.
CHECK IT OUT.

Android ListView with CheckBox can distinguish the click

I am trying to create a ListView with CheckBox's...Th ListView should allow the user to both select an item or open that item to select other choice inside it. In other words, the ListView should be able to distinguish between the click on the checkbox and the click on the item itself.
I tried to implement it using android.R.layout.simple_list_item_multiple_choice but this one allows me to only check the checkbox even if I click outside the checkbox (on the item).
anyone can help?
Here's my code,
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ListViewActivity extends Activity implements OnItemClickListener {
ListView directoryList;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ArrayList<String> contacts = new ArrayList<String>();
contacts.add("zaid");
contacts.add("hazem");
contacts.add("Oubai");
directoryList= (ListView) findViewById(R.id.directoryList);
final ArrayAdapter<String> arrayAdapter;
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
contacts);
directoryList.setAdapter(arrayAdapter);
directoryList.setOnItemClickListener(this);
directoryList.setClickable(true);
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
}
You need a customized array adapter. In your getView(), get a hold of your checkbox and set the OnCheckedChangeListener()
Here is a tutorial on how you can make your own custom adapter. The example has a clickable textbox but you can change it to work for a checkbox.
http://www.shubhayu.com/android/listview-with-arrayadapter-and-customized-items

(Pre)select checkboxes in a ListActivity using 'simple_list_item_checked'

I'm using the ListActivity class in conjunction with the simple_list_item_checked-layout which implements simple list items with checkboxes. Everything is working fine - clicking added items calls onListItemClick() and I can check/uncheck respective checkboxes of entries via the 'View v' parameter.
However what I wasn't able to figure out yet is, how to (pre)select checkboxes without any userinteraction?
Minimal so far working code snippet showing my intend:
package org.test;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
public class TestActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> list_elems = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked);
list_elems.add("foobar");
//TODO: check added entry labeled "foobar"
setListAdapter(list_elems);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
CheckedTextView check = (CheckedTextView)v;
check.setChecked(!check.isChecked());
}
}
Thanks a lot in advance!
daten
This works for me:
You have to set the choicemode of the underlying ListView to single or multiple.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> list_elems = new ArrayAdapter<String>(this, Android.R.layout.simple_list_item_checked);
list_elems.add("foobar");
//TODO: check added entry labeled "foobar"
setListAdapter(list_elems);
ListView lv = getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setItemChecked(0, true);
}
Use a SimpleListAdapter as the ListAdapter for your ListActivity. Use two columns (one for your string and the other for the checked value), and the system should take care of it by itself. Here is a good example

Categories

Resources