ListView onItemClickListener doesnt do anything - android

I have a ListFragment with used as a tab. I am trying to use OnItemSelectedListener on my ListView which has to start another activity and put specific extra in the intend based on the selected item. I am using the following code:
View rootView = inflater.inflate(R.layout.fragment_priority_tab, container, false);
ListView listView = (ListView) rootView.findViewById(android.R.id.list);
List<String> printedList = calculateProportions();
ArrayAdapter<String> expensesAdapter = new ArrayAdapter<>(mContext,
android.R.layout.simple_list_item_1, printedList);
listView.setAdapter(expensesAdapter);
listView.setClickable(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
Intent intent = new Intent(mContext, ListPriority.class);
switch (position) {
case 0:
intent.putExtra("priority", "low");
startActivity(intent);
break;
case 1:
intent.putExtra("priority", "med");
startActivity(intent);
break;
case 2:
intent.putExtra("priority", "high");
startActivity(intent);
break;
case 3:
intent.putExtra("priority", "obsolete");
startActivity(intent);
break;
}
}
});
I dont have any error or warning but my activity doesnt start.When i debug the app and i click on any of the items i dont even reach my breakpoint.
my ListView in XML:
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
my java code is in onCreateView in the ListFragment

ListFragment handles its own OnItemClickListener. Instead of setting a listener, override onListItemClick()

In your java class, your id declaration is wrong:
Change from,
ListView listView = (ListView) rootView.findViewById(android.R.id.list);
To
ListView listView = (ListView) rootView.findViewById(R.id.list);
And your xml id declaration is wrong:
Change from,
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
To
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

Related

setOnItemClickListener doesn't work on Fragment

I have a class that extends Fragment. I have a ListView. When I click on an item, it return always the same item.
Here the row.xml of the ListView adapter.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="#+id/IdItem"/>
</LinearLayout>
And here the event:
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
myTextView = (TextView)getActivity().findViewById(R.id.IdItem);
String IdItem = myTextView.getText().toString();
Intent intent = new Intent(getActivity(), NextActivity.class);
intent.putExtra("IdItem", IdItem);
startActivity(intent);
}
});
It always returns the same IdItem, and not the choosen one.
Change this:
myTextView = (TextView)getActivity().findViewById(R.id.IdItem);
for this:
myTextView = view.findViewById(R.id.IdItem);
The rows objects of a ListView are reused in order to save memory, so you need to use the View object being sent in the onItemClick method of the ListView. This View object represents the actual clicked row inside the ListView with current child Views and values.

Not able to use custom layout in ArrayAdapter

I'm new to android, I want to use my custom layout file instead of simple_list_item_1 but its not allowing to do so. I want to add an backgroud image(which is defined in layout->custom_view.xml) to each items in the list OR help me to set the backgroud for the whole page, In the below code where should I use the setContentView Method. Also let me know if any changes are to be made in the below .xml file.
public class Planet extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
//setContentView(R.layout.custom_view); Tried to set the view from here but the application is crashing...
String[] planet_names = new String[] {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Sun"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, planet_names);
setListAdapter(adapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0)
{
Intent myIntent = new Intent(Planet.this, Galaxy.class);
startActivityForResult(myIntent, 0);
}
}
});
}
custom_view.xml file is as follows,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Simply use this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.yourcustomview_withonetextview,R.id.yourcustomlayouttextviewid, planet_names);
You don't set the contentview in a ListActivity. The ListActivity already does that for you. Use an Activity, if you want to customize your layout.
You need to call super.onCreate() before setContentView(). And if you are going to use a custom layout in a ListActivity, you need to give the ListView the id #android:id/list.

switching from a CustomAdapter ListView to an standard ArrayAdapter ListView fails

I recently got interested in Android coding and working my way through several tutorials. however, right now i m stuck for a day and hope you guys can help me out.
basically i want to create a main menu and a submenu for every main menue entry. (right now this is only the case for the first entry)
For the main menu I used a ListView with different icons (realized with a custom adapter). Selecting one of the menu entries works fine in general, but if I choose to configure the onitemclick to start a new activity which is another (simple) ListView which uses the ArrayAdapter it breaks after switching into the submenu activity.
Strangely if i use the ArrayAdapter in both ListViews it is no problem; also when using CustomAdapter in both.
Here is the code, thanks in advance for any help ;)
main menu:
public class MainActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array contentmenu
String[] contentmenu = getResources().getStringArray(R.array.mainmenu_cats);
// storing icons in Array
TypedArray iconarray = getResources().obtainTypedArray(R.array.mainmenu_icons);
// Binding resources Array to ListAdapter
this.setListAdapter(new ImageAndTextAdapter(this, R.layout.list_item, contentmenu, iconarray));
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Launching new Activity on selecting single List Item
switch( position )
{
case 0: Intent newActivity0 = new Intent(getApplicationContext(),SubActivity.class);
startActivity(newActivity0);
break;
case 1: Intent newActivity1 = new Intent(getApplicationContext(),SubActivity2.class);
startActivity(newActivity1);
break;
}
}
});
}
submenu:
public class SubActivity extends ListActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] submen_1 = getResources().getStringArray(R.array.submenu_1);
// Binding resources Array to ListAdapter
setListAdapter(new ArrayAdapter<String>(ctx, R.layout.list_item1, R.id.submen1, submen_1));
ListView sublv1 = getListView();
// listening to single list item on click
sublv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Launching new Activity on selecting single List Item
Intent newActivity = new Intent(getApplicationContext(),Page1.class);
// sending data to new activity
newActivity.putExtra("position",position);
startActivity(newActivity);
}
});
}
xml main menu:
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="#+id/option_icon"
android:layout_width="48dp"
android:layout_height="fill_parent"/>
<TextView
android:id="#+id/option_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16dp" >
</TextView>
sub menu:
list_item1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="#+id/icon1"
android:layout_width="48dp"
android:layout_height="fill_parent"
android:src="#drawable/ic_action_google_play" />
<TextView
android:id="#+id/submen1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16dp" >
</TextView>
Currently you are not defining Hight and Width for LinearLayout in both list_item.xml and list_item1.xml layouts . add height-width as :
In list_item.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- PUT YOUR ImageView or other xml elements here -->
</LinearLayout>
and do same for list_item1.xml layout

Android Dialog root view

I have a dialog in Android, as a layout I use this file:
<?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
In the Activity I need to add a Adapter to this ListView, I have this:
#Override
protected Dialog onCreateDialog(int id) {
switch(id){
case ADDPLAYERDIALOG:{
Dialog d = new Dialog(this);
d.setContentView(R.layout.training_dialog);
ListView lv = (ListView) d.getCurrentFocus().getRootView();
ListAdapter adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, createNamesList());
lv.setAdapter(adapter);
return d;
}
}
return super.onCreateDialog(id);
}
I get a NullPointerException here:
ListView lv = (ListView) d.getCurrentFocus().getRootView();
I don't have the id for this ListView widget, because it is a layout XML-file, and I can't just write lv = d.findViewById(R.id.listview);
How can I solve the problem?
You can easily set an id through xml. You use the android:id tag.
Your listview node should look something like this:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id = "#+id/listview">
</ListView>
Now your lv = d.findViewById(R.id.listview); should work.

Activity Loads, but doesn't shows up

I have this code:
ListView listView;
static final String[] Liste = {"Satz des Pythagoras", "abc-Formel", "pq-Formel"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
setTitle("CalcPlus");
listView = getListView();
//Own row layout
listView.setAdapter(new ArrayAdapter<String>(this,
R.layout.activity_home, Liste));
listView.setOnItemClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.maths:
return true;
case R.id.physics:
Intent myIntent2 = new Intent(Home.this, ActivityPhysics.class);
startActivity(myIntent2);
default:
return super.onOptionsItemSelected(item);
}
}
public void onItemClick(AdapterView<?> adapter, View view,
int position, long id) {
if ("Satz des Pythagoras".equals(Liste[position])) {
Intent sdp = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(sdp, 0);
}
else if ("abc-Formel".equals(Liste[position])) {
Intent abc = new Intent(view.getContext(), ActivityABC.class);
startActivityForResult(abc, 0);
}
else if ("pq-Formel".equals(Liste[position])) {
Intent pq = new Intent(view.getContext(), ActivityPQ.class);
startActivityForResult(pq, 0);
}
}
This should display a list and a actionbar.
It did, for some time.
It looks like I messed up the code and, since I'm not having that much experience, can't find my mistake...so could someone review my code? :)
Because now it loads the activity..but doesn't display the list or the actionbar :o
Here is the XML in \menu\activity_home.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never" />
<item android:id="#+id/maths"
android:title="#string/maths"
android:orderInCategory="1"
android:showAsAction="always|withText"
android:titleCondensed="#string/maths"
android:icon="#drawable/ic_action_calculator" />
<item android:id="#+id/physics"
android:icon="#drawable/ic_action_line_chart"
android:title="#string/physics"
android:orderInCategory="2"
android:showAsAction="always|withText"/>
This is layout\activity_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"/>
</RelativeLayout>
So basically it's empty.
If you are using a ListActivity, your R.layout.activity_home has to have a ListView like:
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Then you can create a new layout for the list view item in your adapter or use a predefined layout for the list item like:
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, Liste));
With these two lines:
setContentView(R.layout.activity_home);
...
listView.setAdapter(new ArrayAdapter<String>(this,
R.layout.activity_home, Liste));
You are using the same layout for the entire Activity and each row, which doesn't make sense. The Activity's layout should contain a ListView and the row layout shouldn't...
To use the default layouts, remove this line:
setContentView(R.layout.activity_home);
and change this one:
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, Liste));
Also you need to onListItemClick() not onItemClick() to have the row clicks work.
#Override
protected void onListItemClick (ListView l, View v, int position, long id) {...}
Okay now the only entry in LogCat is Array Adapter:"You musst supply a resource ID for a textview.
You must give your TextView an id:
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"/>
And change the way you create your adapter to:
listView.setAdapter(new ArrayAdapter<String>(this,
R.layout.activity_home, R.id.text, Liste));

Categories

Resources