I have a list of items in android app, and i want to start another activity if the user clicks on any item in that list. I have the following piece of (relevant) code.
Strangely enough, its working good enough on HTC velocity 4g (android 2.3.7), whereas I have tried the same app on HTC one S (android 4.1.1) and nothing happens when i click any item in the list. Does anyone have any idea what could be the problem here???
lv = (ListView)findViewById(R.id.listView1);
this.adapter = new SimpleAdapter(DisplayWiFiListActivity.this, arraylist, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.abcd });
lv.setAdapter(this.adapter);
// React to user clicks on item
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
long id) {
String text = parentAdapter.getItemAtPosition(position).toString();
text = text.substring(5,text.length()-1);
Intent intent = new Intent(DisplayWiFiListActivity.this, MainActivity.class);
DisplayWiFiListActivity.this.startActivity(intent);
intent.putExtra(EXTRA_MESSAGE, text);
startActivity(intent);
Toast.makeText(DisplayWiFiListActivity.this, text, Toast.LENGTH_SHORT).show();
}
});
row.xml is following
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/abcd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:clickable="true"
android:layout_marginTop="20dp" />
</LinearLayout>
activity_display_wifi_list.xml is following
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".DisplayWiFiListActivity" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:clickable="true"
android:layout_marginTop="100dp" >
</ListView>
<Button
android:id="#+id/buttonScan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/refresh_list" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/buttonScan"
android:textStyle="normal|italic"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"/>
</RelativeLayout>
Please post your code of SimpleAdapter or if you have any Button Widget in listitem layout pls make it focus-able false;
Related
I am not able to make my list view clickable.I have tried all the given solutions for same type of questions and had tried to change focusable and
focusableintouchmode values to both true and false.
Sometimes it works (i.e text in listview gets clicked) after i click the button 10/12 times.
Below are my xml and java files.
I have also attached the screenshot of logcat when my row number 9 got clicked after i pressed it more than 20 times.
enter image description here
Please help..
Activity_card.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rlLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hppc.business.Card">
<ListView
android:id="#+id/lvUsers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants">
</ListView>
</RelativeLayout>
entry1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:orientation="horizontal"
android:padding="13dp">
<ImageView
android:layout_width="90dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/image" />
<linearlayout>.....</linearlayout>
</linearLayout>
card.java
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(Card.this, Manual.class);
Log.i("HelloListView", "You clicked Item: " + id + " at position:" + position);
Log.e("YESSSS",Integer.toString(position));
startActivity(i);
}
});
you have to make your list view focusable:
android:focusable="true"
android:focusableInTouchMode="true"
I have two ListViews for discovered and paired Bluetooth devices in same XML file but OnItemClickListener only reacts to clicks on my paired devices ListView. I wonder should I have own OnItemClickListener for both ListViews. Actually I tried that option without success but maybe I just made some mistake there.
private AdapterView.OnItemClickListener discoveredDeviceClickListener
= new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int position, long id) {
// Cancel discovery because it's costly and we're about to connect
cBluetoothAdapter.cancelDiscovery();
// Get the device MAC address, which is the last 17 chars in the View
String info = ((TextView) v).getText().toString();
String address = info.substring(info.length() - 17);
// Start new activity and send the MAC address
Intent BluetoothTransferIntent = new Intent(MainActivity.this, BluetoothTransferActivity.class);
BluetoothTransferIntent.putExtra(EXTRA_DEVICE_ADDRESS, address);
startActivity(BluetoothTransferIntent);
// Set result and finish this Activity
//setResult(Activity.RESULT_OK, BluetoothTransferIntent);
}
};
pairedBluetoothDeviceList = (ListView) findViewById(R.id.paired_device_list);
pairedBluetoothDeviceList.setOnItemClickListener(discoveredDeviceClickListener);
discoveredBluetoothDeviceList = (ListView) findViewById(R.id.discovered_device_list);
discoveredBluetoothDeviceList.setOnItemClickListener(discoveredDeviceClickListener);
discoveredBluetoothDevices = new ArrayList();
discoveredBluetoothAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, discoveredBluetoothDevices);
pairedBluetoothDevices = new ArrayList();
pairedBluetoothAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, pairedBluetoothDevices);
discoveredBluetoothDeviceList.setAdapter(discoveredBluetoothAdapter);
pairedBluetoothDeviceList.setAdapter(pairedBluetoothAdapter);
And XML file:
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/bluetooth"
tools:context="com.example.jake.bluetooth.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/discovered_device_list"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
android:divider="#000000"
android:dividerHeight="1dp" >
</ListView>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/paired_device_list"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
android:divider="#000000"
android:dividerHeight="1dp" >
</ListView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/SetOnBluetooth"
android:id="#+id/SetOnBluetoothButton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="133dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/SetOffBluetooth"
android:id="#+id/SetOffBluetoothButton"
android:layout_above="#+id/SetOnBluetoothButton"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Query"
android:id="#+id/QueryButton"
android:layout_above="#+id/ReceiveButton"
android:layout_alignLeft="#+id/ReceiveButton"
android:layout_alignStart="#+id/ReceiveButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/DiscoverDevices"
android:id="#+id/DiscoverDevicesButton"
android:layout_centerHorizontal="true"
android:layout_above="#+id/SetOffBluetoothButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Connect"
android:id="#+id/ConnectButton"
android:layout_alignTop="#+id/SetOnBluetoothButton"
android:layout_toLeftOf="#id/SetOnBluetoothButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Send"
android:id="#+id/SendButton"
android:layout_toLeftOf="#+id/SetOffBluetoothButton"
android:layout_alignTop="#+id/SetOffBluetoothButton"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Receive"
android:id="#+id/ReceiveButton"
android:layout_alignTop="#+id/SetOnBluetoothButton"
android:layout_toRightOf="#+id/SetOnBluetoothButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Visible"
android:id="#+id/VisibleButton"
android:layout_above="#+id/DiscoverDevicesButton"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Exit"
android:id="#+id/ExitButton"
android:layout_above="#+id/VisibleButton"
android:layout_centerHorizontal="true"/>
Big thanks in advance!
Edit: I don't know if this help but Android Studio's Android Monitor gives this message after I have pressed item in the ListView: ViewPostImeInputStage ACTION_DOWN
Yes u have to use two OnClickListener since you have to use two list views.
Your not writing the code in proper order. I rewrote it .Once try it
pairedBluetoothDevices = new ArrayList();
pairedBluetoothAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, pairedBluetoothDevices);
pairedBluetoothDeviceList = (ListView) findViewById(R.id.paired_device_list);
pairedBluetoothDeviceList.setAdapter(pairedBluetoothAdapter);
pairedBluetoothDeviceList.setOnItemClickListener(discoveredDeviceClickListener);
discoveredBluetoothDeviceList = (ListView) findViewById(R.id.discovered_device_list);
discoveredBluetoothDeviceList.setOnItemClickListener(discoveredDeviceClickListener);
discoveredBluetoothDevices = new ArrayList();
discoveredBluetoothAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, discoveredBluetoothDevices);
discoveredBluetoothDeviceList.setAdapter(discoveredBluetoothAdapter);
private AdapterView.OnItemClickListener discoveredDeviceClickListener
= new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int position, long id) {
// Cancel discovery because it's costly and we're about to connect
cBluetoothAdapter.cancelDiscovery();
// Get the device MAC address, which is the last 17 chars in the View
String info = ((TextView) v).getText().toString();
String address = info.substring(info.length() - 17);
// Start new activity and send the MAC address
Intent BluetoothTransferIntent = new Intent(MainActivity.this, BluetoothTransferActivity.class);
BluetoothTransferIntent.putExtra(EXTRA_DEVICE_ADDRESS, address);
startActivity(BluetoothTransferIntent);
// Set result and finish this Activity
//setResult(Activity.RESULT_OK, BluetoothTransferIntent);
}
};
New to Android, sorry if there's something I should be including here that I'm not.
Anyway, I'm trying to make a custom layout for a list. Whenever I try to run the app, it closes and sends a "AppName has crashed" error modal. When I use a built-in android layout, this doesn't happen. I'm not sure if the issue is my code or my emulator (Running on a Nexus 5 with 1GB RAM)
Here's layout/row_layout.xml
<LinearLayout
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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView1"
android:textSize="50sp"
android:textStyle="bold"
android:padding="15dp"/>
</LinearLayout>
Here's layout/activity_main.xml
<LinearLayout 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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/theListView"></ListView>
</LinearLayout>
And here's the onCreate method in MainActivity.java (I haven't edited anything else)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] favoriteTVShows = {"The Office", "The Wire", "Mr. Robot", "Parks and Rec",
"Fool Us", "Garbage Time", "Last Week Tonight", "Silicon Valley"};
ListAdapter theAdapter = new ArrayAdapter<String>(this, R.layout.row_layout,
favoriteTVShows);
ListView theListView = (ListView) findViewById(R.id.theListView);
theListView.setAdapter(theAdapter);
theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView,
View view, int position,
long id) {
String tvShowPicked = "You selected " +
String.valueOf(adapterView
.getItemAtPosition(position));
Toast.makeText(MainActivity.this, tvShowPicked,
Toast.LENGTH_SHORT).show();
}
});
}
You have to set the adapter to a view that can contain the text, meaning that instead of using a LinearLayout as a base, use a TextView:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView1"
android:textSize="50sp"
android:textStyle="bold"
android:padding="15dp"/>
I am having problems with ListView on android. I have an Activity with an EditText view and a button. The idea is user should enter some info in the EditText field, touch the Search button which retrieves a list from a web service that it is displayed in the same activity below the EditText and the Button. Everything fine, I got the data from Internet but items weren't displaying. I was using the notifyDataSetChanged(). After a couple of hours with no success, I decided trying to put some items manually and it turns out that again nothing was displayed, hence I think I am doing something wrong when I am trying to set the listview and the adapter. So here is the code..
the xml of the activity activity_search.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp">
<EditText
android:id="#+id/edit_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:inputType="text"
android:hint="Enter info"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:drawable/ic_menu_search"
android:contentDescription="Search"
android:ems="10" />
</LinearLayout>
<ListView
android:id="#+id/listview_items"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false" />
The XML of the item row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="5dp">
<TextView
android:id="#+id/row_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Common_TextContent"
android:textAppearance="#android:style/TextAppearance.Holo.Medium" />
<TextView
android:id="#+id/row_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="#string/Common_TextContent"
android:textAppearance="#android:style/TextAppearance.Holo.Small" /></LinearLayout>
the custom Adapter:
public class ItemsListAdapter extends ArrayAdapter<ItemsList> {
private int[] colors = new int[] { 0x30ffffff, 0x30808080 };
public AssetListAdapter(Context context,
List<ItemsList> itemsList) {
super(context, 0, itemsList);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
}
convertView.setBackgroundColor(colors[position % colors.length]);
TextView code = (TextView) convertView.findViewById(R.id.row_code);
code.setText(getItem(position).getCode());
TextView name = (TextView) convertView.findViewById(R.id.row_name);
name.setText(getItem(position).getName());
return convertView;
}
and the onCreate method on the Activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
_listView = (ListView) findViewById(R.id.listview_items);
_listItems = new ArrayList<ItemsList>();
_listItems.add(new ItemsList ("cosa1", "cosa1", "cosa1", "cosa1"));
_listItems.add(new ItemsList ("cosa2", "cosa2", "cosa2", "cosa2"));
_listItems.add(new ItemsList ("cosa3", "cosa3", "cosa3", "cosa3"));
_adapter = new ItemsListAdapter(this, _listItems);
_listView.setAdapter(_adapter);
}
The ItemsList is just an Object with 4 strings with all the getters implemented.
When I debug in the getView method of the Adapter, the view (convertView) is created and it has the right information. It is just that is not showing those in the screen. What am I doing wrong?
Thanks...
Your second LinearLayout has a height of match_parent. Try wrap_content instead.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
This is my list layout:
<?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">
<LinearLayout android:layout_width="fill_parent" android:id="#+id/linearLayout1"
android:layout_height="wrap_content">
<LinearLayout android:layout_width="wrap_content" android:id="#+id/linearLayout2"
android:layout_height="fill_parent">
<CheckBox android:id="#+id/checkBox1" android:layout_width="wrap_content"
android:layout_height="wrap_content"></CheckBox>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:id="#+id/linearLayout3" android:layout_height="fill_parent" android:orientation="vertical">
<TextView android:text="TextView" android:id="#+id/row1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:text="TextView" android:id="#+id/row2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Although I have an excess of Layouts, all this is is a checkbox and a couple of TextViews. I want each listItem to be clickable(as in go to a different intent), but when I click a button (DELETE BUTTON)in my activity, I want to be able to show the checkboxes and select the items to delete and delete it. Sample code appreciated.
Java code:
adapter = new SimpleAdapter(this, painItems, R.layout.mylistlayout,
from, to);
listthings.setAdapter(adapter);
listthings.setOnItemClickListener(this);
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent intent = new Intent(this, Item1.class);
intent.putExtra("com.painLogger.painLevel", painLevelString);
intent.putExtra("com.painLogger.painTime", timeOfPainString);
intent.putExtra("com.painLogger.treatment", textTreatmentString);
intent.putExtra("painLocation", painLocation);
startActivity(intent);
}
Delete checked items
private void deleteCheckedItems() {
int count = this.mainListView.getAdapter().getCount();
for (int i = 0; i < count; i++) {
if (this.mainListView.isItemChecked(i)) {
painItems.remove(i)
}
}
}
More info: http://developer.android.com/reference/android/widget/AbsListView.html#isItemChecked(int)
and then notify the adapter to update:
adapter.notifyDataSetChanged();
public void notifyDataSetChanged ()
Since: API Level 1 Notifies the attached View that the underlying data
has been changed and it should refresh itself.