getting the position onItemClick implementation in LinearLayout - android

I am trying to show a ListView and my Java class extendsListFragment. While setting setOnItemClickListener on ListItem, the click Listener doesn't seem to work. Here is my code:
ListView_rssfeeds_tab.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"
android:orientation="vertical"
android:id="#+id/relativeLayout"
tools:context=".interestingReads.RSSFeedsTab">
<ListView
android:id="#+id/rssFeedsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/diffBackgroundWhite"
/>
</RelativeLayout>
rss_item_list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rssFeeds_LL_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:orientation="vertical"
android:clickable="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="6dp"
>
<ImageView
android:id="#+id/rssFeeds_Image_list_view"
android:layout_width="60dp"
android:layout_height="60dp"
android:contentDescription="#string/app_name"
android:padding="0dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="7dp">
<TextView
android:id="#+id/rssFeeds_Title_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp"
android:textStyle="bold" />
<TextView
android:id="#+id/rssFeeds_Description_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
RSSFeedsTab.java (Please NOTE: RSSFeedsTab is Fragment and not Activity)
public class RSSFeedsTab extends ListFragment implements OnItemClickListener {
.
.
.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.
.
.
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
.
.
.
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.rss_item_list_row, from, to);
setListAdapter(simpleAdapter);
View RootView = getActivity().getLayoutInflater().inflate(R.layout.fragment_rssfeeds_tab, container, false);
ListView RSSFeedsItemLL = (ListView) RootView.findViewById(R.id.rssFeedsFragment);
RSSFeedsItemLL.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
System.out.println("********Reached onClick*********");
Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
}
});
return super.onCreateView(inflater, container, savedInstanceState);
}
With this much implementation, I am able to get the ListView rssFeeds_LL_list_view. I am not sure, how I can use click event on those ListView such that I can get position in ListView ?

you have to use the listener OnItemClickListener to get the item which was clicked :
OnItemClickListener on Android developer
****Update****
In your code, you are using a ListFragment that already contains a Listview.
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.rss_item_list_row, from, to);
setListAdapter(simpleAdapter);
With this lines, you create an Adapter and give this adapter to the Fragment's ListView. Then you recover the RSSFeedsItemLL ListView from your layout and you set the OnItemClickListener on it :
ListView RSSFeedsItemLL = (ListView) RootView.findViewById(R.id.rssFeedsFragment);
RSSFeedsItemLL.setOnItemClickListener(new AdapterView.OnItemClickListener(){...}
But this ListView is never linked to an adapter.
You have two solutions :
Replace the ListFragment with a Fragment in your class definition and use the ListView in your layout file
Use the RSSFeedsItemLL ListView in your fragment
For the second case, you just have to override the method onListItemClick in your RSSFeedsTab class :
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
System.out.println("********Reached onClick*********");
Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
super.onListItemClick(l, v, position, id);
}
You also have to remove the line android:clickable="true" in your LinearLayout definition in rss_item_list_row.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rssFeeds_LL_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="6dp"
>
...

Use onItemClickListener on ListView view: RSSFeedsItemLL
RSSFeedsItemLL.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
System.out.println("********Reached onClick*********");
Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
}});
References: OnItemClickListener

Finally I have resolved the issue by overriding method getView()
So, I changed only the line:
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.rss_item_list_row, from, to);
to
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.rss_item_list_row, from, to){
#Override
public View getView (int position, View convertView, ViewGroup parent)
{
View v = super.getView(position, convertView, parent);
final int finalposition = position;
LinearLayout RSSFeed_singleItem=(LinearLayout) v.findViewById(R.id.rssFeeds_LL_list_view);
RSSFeed_singleItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),"Link:==>"+localLinkArray[finalposition],Toast.LENGTH_SHORT).show();
}
});
return v;
}
};

Related

Event setOnItemClickListener doesn't work in my list

I call the event OnItemClickListener in my list lista, but it doesn't work, the Toast not is shown.
below my code fragment1.java
public class Fragment1 extends Fragment {
Switch mySwitch;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//nome dos reles
String[] atividades = new String[]{"lampada","tomada quarto","tomada cozinha","geladeira"};
//interface
View view=inflater.inflate(R.layout.layout_acionamento, container,false);
//lista
ListView lista = (ListView)view.findViewById(R.id.mylistView);
//botão acionar
Button button = (Button) view.findViewById(R.id.buttonAcionar);
//adaptador de string para reles
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.modelo_lista_reles,atividades);
lista.setAdapter(adapter);
//evento do botão acionar
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "CUBANAJARRA", getId()).show();
}
});
lista.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "CUBANAJARRA_lista", getId()).show();
}
});
return(view);
}
}
My code for modelo_lista_rele.xml(I use this for create a list with switch):
<?xml version="1.0" encoding="utf-8"?>
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:focusable="false"
android:focusableInTouchMode="false"/>
my code for layout_acionamento.xml(used for view that I return in fragment):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/mylistView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/buttonAcionar"
android:layout_alignParentTop="true"
android:clickable="true" >
</ListView>
<Button
android:id="#+id/buttonAcionar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Acionar" />
</RelativeLayout>
Toast.makeText(getActivity(), "CUBANAJARRA", getId()).show();
The third parameter is length not id.
Try to use Toast.LENGTH_LONG or Toast.LENGTH_SHORT instead like:
Toast.makeText(getActivity(), "CUBANAJARRA", Toast.LENGTH_LONG).show();
Wrap you Switch around with a LinearLayout, and make sure that you either block descendantFocusability or set the Switch's xml params focusable and focusableInTouchMode to false. This should get things working. What's happening is that the Switch is consuming the touch event so the ListView never has a chance to fire the OnItemClickListener.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="50dp">
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Edit: Based on comment: Take a look at this YouTube video by Romain Guy on ListViews. It should give you a good idea of what's going on below.
Create a ViewHolder class to keep track of individual widgets in your item layout:
public static class ViewHolder{
public Switch mSwitch;
}
Then, in your adapter, override getView. The code below may look weird at first, but it's the proper way to get the benefits of recycling views so your list scrolls fast.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = mInflater.inflate(R.layout.modelo_lista_rele.xml, parent, false);
holder = new ViewHolder(convertView);
holder.switch = convertView.findViewById(R.id.my_switch);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
String item = getItem(position);
//Do something with item here
//holder.mSwitch.setText(item);
//holder.mSwitch.setOnCheckedChangeListener(...)
return convertView;
}

Multiple Items Selected on ListView, selects wrong items

I have this code for a ListView on a Fragment. My goal is to select multiple items on the list and to highlight them. My code does highlight the item, but when I scroll the list, more items are highlighted and when I scroll up again, more items are highlighted again and it goes crazy over and over highlighting and changing to default state, whenever I scroll the list. I don't know why it happens.
NOTE: I've tried CHOICE_MODE_SINGLE, CHOICE_MODE_MULTIPLE, CHOICE_MODE_MULTIPLE_MODAL and I also tried MultiChoiceModeListener but my App should be working on API above 9. So that's not a solution for me.
My Fragment (Don't want to use ListFragment)
public class InventarioFragment extends Fragment {
ListView listInventario;
public static InventarioFragment newInstance()
{
return new InventarioFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_inventario_list,container,false);
listInventario = (ListView) view.findViewById(R.id.list_inv);
listInventario.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
v.setBackgroundColor(Color.BLUE);
}
});
return view;
}
}
This is the XML of my Fragment (Notice that I am using an array for the items on the list).
My Fragment XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:weightSum="1">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5">
<ImageButton
android:id="#+id/ib_cancel_inventario"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/btn_cancel"
android:layout_centerInParent="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5">
<ImageButton
android:id="#+id/ib_submit_inventario"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/btn_ok"
android:layout_centerInParent="true"/>
</RelativeLayout>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="20dp"
android:background="#DF0101"/>
<ListView
android:id="#+id/list_inv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:entries="#array/list_inventario"
>
</ListView>
</LinearLayout>
</LinearLayout>
I'm not using and Adapter because I set the list items from a default array.
Sorry for my english, and I hope you can help me. (I can't upload images because I don't have enough reputation yet).
You can use the following adapter to achieve your goal.
Remove android:entries="#array/list_inventario" in your Listview and add choicemode as follows in your XML.
<ListView
android:id="#+id/list_inv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="multipleChoice">
</ListView>
InventarioFragment
public class InventarioFragment extends Fragment {
private Activity activity;
private ListView listInventario;
private String[] inventatioItems;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.activity=activity;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_inventario_list,container,false);
listInventario = (ListView) view.findViewById(R.id.list_inv);
inventatioItems=getResources().getStringArray(R.array.list_inventario);
InverntarioAdapter adapter=new InverntarioAdapter(activity);
listInventario.setAdapter(adapter);
listInventario.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int pos, long arg3) {
listInventario.setItemChecked(pos, true);;
}
});
return view;
}
private class InverntarioAdapter extends BaseAdapter{
private LayoutInflater inflator;
public InverntarioAdapter(Context context){
inflator=(LayoutInflater) context.getSystemService(activity.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return inventatioItems.length;
}
#Override
public Object getItem(int position) {
return inventatioItems[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
TextView tv;
if(convertView == null){
convertView=inflator.inflate(android.R.layout.simple_list_item_1, null);
convertView.setBackground(getResources().getDrawable(R.drawable.selector_background));
}
tv=(TextView) convertView.findViewById(android.R.id.text1);
tv.setText(inventatioItems[position]);
if(listInventario.isItemChecked(position)){
//background Color of selected items
convertView.setBackgroundColor(Color.BLUE);
}
else{
convertView.setBackgroundColor(Color.WHITE);
}
return convertView;
}
}
}

Get Item from a ListView

My app is displaying 3 String with 3 Text-view in a list-view.
My Problem is that I create on layout for my list-view and i am not able to get the item from the first text view in the onListItemClick method.
How can I achieve achievement this?
Layout of the ListView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/outputlayout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="#color/ligthgrey">
<TextView
android:id="#+id/txOutputDeparture"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Abfahrt "
android:textColor="#color/black"
android:layout_alignParentLeft="true"
android:maxLength="#android:integer/config_shortAnimTime"
android:layout_marginTop="5dp" />
<TextView
android:id="#+id/txOutputDuration"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/txOutputDeparture"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Dauer"
android:layout_marginTop="5dp" />
<TextView
android:id="#+id/txOutputTransition"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/txOutputDuration"
android:text="Umstieg"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginTop="5dp" />
</RelativeLayout>
Method where i create and fill the listView and its and ListActivity
public void getRoute() {
mdbH = new DatabaseHelperActivity(this);
cursor = mdbH.fetchallRoutes(intent.getStringExtra("StartHaltestelle"),intent.getStringExtra("ZielHaltestelle"), intent.getStringExtra("Zeit"));
ArrayList<DefineRouteActivity> route = new ArrayList<DefineRouteActivity>();
while(cursor.moveToNext()) {
route.add(new DefineRouteActivity(cursor.getString(0),cursor.getString(2),cursor.getString(4)));
}
ArrayAdapter<DefineRouteActivity> adapter = new RouteAdapterActivity(this, route);
setListAdapter(adapter);
}
AdapterActivity:
Activity context;
ArrayList<DefineRouteActivity> arraylist;
public RouteAdapterActivity(Activity context, ArrayList list) {
super(context,R.layout.outputlayout,list);
this.context = context;
arraylist = list;
}
#Override
public int getCount() {
return arraylist.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View contentview, ViewGroup viewGroup) {
DefineRouteActivity routeItems = arraylist.get(position);
LayoutInflater inflater = context.getLayoutInflater();
View view = inflater.inflate(R.layout.outputlayout,null);
TextView tvDeparture = (TextView)view.findViewById(R.id.txOutputDeparture);
tvDeparture.setText(routeItems.getAbfahrtszeit());
TextView tvDuration = (TextView)view.findViewById(R.id.txOutputDuration);
tvDuration.setText(routeItems.getDauer());
TextView tvTransition = (TextView)view.findViewById(R.id.txOutputTransition);
tvTransition.setText(routeItems.getUmstieg());
return view;
}
Here is my onClick Method:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
((View)v).
Intent detail = new Intent(getApplicationContext(),DetailOutputActivity.class);
detail.putExtra("StartStop",l.getItemAtPosition(0).toString());
detail.putExtra("EndStop","ich");
detail.putExtra("Time","du");
detail.putExtra("Route","er");
startActivity(detail);
}
Here is my Layout containing the listView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
You need another xml containing the listView.
For example: sample.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/lv1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
In your mainActivity.java:
setContentView(R.layout.sample.xml);
lv = (ListView)findViewById(R.id.lv1);
lv.setOnItemClickListener(this);
Your listActivity must implement OnItemClickListener.
Then implements onItemClick method. Example:
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
// TODO Auto-generated method stub
int id_object = ((object)a.getAdapter().getItem(position)).getIdInstalacion();
Intent i = new Intent(this, Other.class);
i.putExtra("id_", id_object);
startActivity(i);
}

drag-sort-list-view item click does not work

I use DragSortListView library. It works fine - drag and remove items from my listview, but i also want to click ListView items. When I set OnClickListener it don't work. What could be the problem? Sorry for my bad english
My fragment:
private DragSortListView listView;
private MakeSingleBetAdapter adapter;
private View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
MyLog.d(TAG, "onCreateView");
BugSenseHandler.initAndStartSession(getActivity(), APP_Parameters.BugSense);
view = inflater.inflate(R.layout.fragment_make_single_bet,
container, false);
listView = (DragSortListView) view.findViewById(R.id.makeBetList);
listView.setRemoveListener(this);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getActivity(), "Click: " + position, Toast.LENGTH_SHORT).show();
}
});
cursor = dbHelper.getAllBets();
adapter = new MakeSingleBetAdapter(getActivity(), cursor, 3, dbHelper);
listView.setAdapter(adapter);
return view;
}
#Override
public void remove(int which) {
Cursor cursorId = (Cursor) (listView.getItemAtPosition(which));
String id = cursorId.getString(cursor
.getColumnIndex(DBHelper.COL_BET_ID));
dbHelper.deleteBetById(id);
adapter.swapCursor(dbHelper.getAllBets());
}
My XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/ch.bettings"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical" >
....
<com.mobeta.android.dslv.DragSortListView
android:id="#+id/makeBetList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/View1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:cacheColorHint="#color/slidingMenu"
android:divider="#b5b5b5"
app:drag_enabled="true"
app:drag_start_mode="onMove"
app:remove_enabled="true"
app:remove_mode="flingRemove"
app:sort_enabled="false" />
....
</RelativeLayout>
If I add ClickListener in my adapter - it work's fine, but I can not drag list items
Try adding these to your listview row:
android:focusable="false"
android:focusableInTouchMode="false"
This worked for me, I have been clicking the item in the list and its going to touch event so my code is as below. I wanted to only perform Onclick but not Ontouch just override the method and do nothing.
private DragSortListView list;
list = view.findViewById(android.R.id.list);
list.addHeaderView(footerView);
list.setFocusable(false);
list.setHeaderDividersEnabled(false);
list.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(getActivity(),"its a touch" , Toast.LENGTH_LONG).show();
Log.d("stack", "TOUCH EVENT");
return false;
}
});
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(),"Position :"+position+" ListItem : " +id , Toast.LENGTH_LONG).show();
Log.i("edit recipe click", "clicked");
EditableIngredient ingredient = (EditableIngredient) parent.getItemAtPosition(position);
host.handleIngredientSelected(ingredient);
}
});
return view;
}
My Xml looks like
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mobeta.android.dslv.DragSortListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
app:drag_handle_id="#id/drag_handle"
app:float_background_color="#android:color/white"
/>
</FrameLayout>

How can i add CheckBox in the list view items?

In My application i am using two list view's in one activity, one for only items and one list view i am have add check box in every row.
My requirement is in below list view if item is checked i have to display on text view in the main activity if not have to display another activity .
i was not used ListActivity for this i just created from Activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_weight="0.5"
android:layout_width="fill_parent"
android:layout_height="60px">
<ListView
android:id="#+id/list1"
android:layout_width="fill_parent"
android:layout_weight="0.5"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="0.5"
android:layout_width="fill_parent"
android:layout_height="60px">
<ListView android:id="#+id/list2"
android:layout_width="fill_parent"
android:layout_weight="0.5"
android:layout_height="wrap_content" />
</LinearLayout>
above list view for only items for below list view i have to add check box. how can i achieve this.
my activity is..
public class Settings extends Activity {
#SuppressWarnings({ "unchecked", "rawtypes" })
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
ListView list1 = (ListView) findViewById(R.id.list1);
ListView list2 = (ListView) findViewById(R.id.list2);
list2.setAdapter(new ArrayAdapter(this,
android.R.layout.simple_expandable_list_item_1, Display));
list2.setTextFilterEnabled(true);
list2.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
}
});
list1.setAdapter(new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, Categories));
list1.setTextFilterEnabled(true);
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
}
});
}
static final String[] Categories = new String[] {
"Alarm1","Alarm2","Alarm3","Alarm4","Alarm5"
};
static final String[] Display = new String[] {
"24 Hours","Display Seconds"
};
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = mInflater.inflate(R.layout.provider, null);
TextView location = (TextView) convertView
.findViewById(R.id.description);
checkBoxImage = (ImageView) convertView.findViewById(R.id.check_id);
Provider p = (Provider) getItem(position);
location.setText(p.Description + " - " + p.Location);
return convertView;
}
Well here is the Complete Tutorial how you can include CheckBox in a List. Its the Best Tutorial Regarding the ListView & ListActivity.

Categories

Resources