ListView Onclick? - android

i have a question about adding a onClick to the a ListView, i have tried to follow the Android NotePad tutorial as much as posible but with my layout i dont quite understand how to add it in.
this is the activity class and it simpley populates the view
public class RemoveForm extends ListActivity {
private DB_Conn DB_Handler;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DB_Handler = new DB_Conn(this);
DB_Handler.open();
fillData();
}
protected void onListItemClick(View view) {
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = DB_Handler.fetchAll();
startManagingCursor(c);
String[] from = new String[] { DB_Conn.KEY_MODULECODE,
DB_Conn.KEY_DAY,
DB_Conn.KEY_TIME,
DB_Conn.KEY_DURATION,
DB_Conn.KEY_TYPE,
DB_Conn.KEY_ROOM,DB_Conn.KEY_ROWID };
int[] to = new int[] { R.id.MODULECODE,
R.id.DAY,
R.id.TIME,
R.id.DURATION,
R.id.TYPE,
R.id.ROOM,
R.id.ROWID};
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter entry =
new SimpleCursorAdapter(this, R.layout.list, c, from, to);
setListAdapter(entry);
}
}
and this is the xml for each record.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical">
<LinearLayout android:id="#+id/linearLayout1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1"
android:baselineAligned="false">
</LinearLayout>
<LinearLayout android:id="#+id/linearLayout2" android:layout_height="wrap_content"
android:layout_width="match_parent" android:layout_weight="1"
android:baselineAligned="false">
</LinearLayout>
<RelativeLayout
android:layout_height="wrap_content"
android:id="#+id/relativeLayout1" android:layout_width="match_parent">
<TextView android:id="#+id/ROWID"
android:text="TextView"
android:layout_marginRight="5dip"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"></TextView>
<TextView android:id="#+id/MODULECODE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_toRightOf="#+id/ROWID"
android:layout_alignTop="#+id/ROWID"></TextView>
<TextView android:layout_marginLeft="25dip"
android:id="#+id/DAY"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_below="#+id/MODULECODE"
android:layout_alignLeft="#+id/MODULECODE"></TextView>
<TextView android:id="#+id/TIME"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_toRightOf="#+id/DAY"
android:layout_alignTop="#+id/DAY"
android:layout_alignBottom="#+id/DAY"></TextView>
<TextView android:id="#+id/DURATION"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_toRightOf="#+id/TIME"
android:layout_alignTop="#+id/TIME"
android:layout_alignBottom="#+id/TIME"></TextView>
<TextView android:id="#+id/ROOM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_toRightOf="#+id/MODULECODE"
android:layout_alignTop="#+id/MODULECODE"
android:layout_alignBottom="#+id/MODULECODE"></TextView>
<TextView android:id="#+id/TYPE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_toRightOf="#+id/ROOM"
android:layout_alignTop="#+id/ROOM"
android:layout_alignBottom="#+id/ROOM"></TextView>
</RelativeLayout>
if anyone can help i'd be so grateful right now.

You have to override the,
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
}

Related

How can I change the visibility of an item inside Simple Cursor Adapter that not comes from "to" parameter

I have a SimpleCursorAdapter that contains some textViews and ImageViews his structure is this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/holo_red_dark"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="#+id/ImgViewIconFoodList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#android:color/transparent"
android:src="#drawable/ic_spice" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/ImgViewIconFoodList"
android:orientation="vertical"
android:paddingLeft="10dp">
<TextView
android:id="#+id/txtViewFoodCategoryList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Category"
android:textColor="#949494"
android:textSize="12dp" />
<TextView
android:id="#+id/txtViewFoodNameList"
style="#style/foodListText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="15dp" />
</LinearLayout>
<ImageView
android:id="#+id/imgAddedCart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:adjustViewBounds="false"
android:visibility="invisible"
app:srcCompat="#drawable/ic_shopping_cart_verified_symbol" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
When I'm populating most of items (textviews,imgviews,etc... ) are from a db and works well when are in parameters "from" and "to" in the SimpleCursorAdapter creation.
But my problem is when I need to acces to the (ImageView) id/imgAddedCart .
This ImageView is not in the "to" parameter but i need to interactuate with this.
I wanna change his poperty visibility in function of a condition(if
(id_alimento == codigo))
but i can't do this because I don't know how to acces to this item.
Can someone help me ?
lots of thanks.
String[] from = new String[]{MyDBAdapter.KEY_NOMBRE,MyDBAdapter.KEY_CAT,MyDBAdapter.KEY_ID_CAT};
int[] to = new int[]{R.id.txtViewFoodNameList,R.id.txtViewFoodCategoryList,R.id.ImgViewIconFoodList};
private void fillData() {
//obtain all codes for principal cursor
myCursor = mDBHelper.fetchAllCodes();
//obtain all codes for other cursor to compare with principal
myShoppingListCursor = mDBHelper.fetchShoppingList();
getActivity().startManagingCursor(myShoppingListCursor);
getActivity().startManagingCursor(myCursor);
SimpleCursorAdapter codes = new SimpleCursorAdapter(rootView.getContext(),R.layout.row,myCursor,from,to,0);
codes.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
#Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int viewId = view.getId();
String id_alimento = cursor.getString(cursor.getColumnIndex("_id"));
if (myShoppingListCursor.moveToFirst()) {
//Recorremos el cursor hasta que no haya más registros
do {
String codigo = myShoppingListCursor.getString(myShoppingListCursor.getColumnIndex("id_alimento"));
if (id_alimento == codigo){
// HERE I WANNA CHANGE THE #+id/imgAddedCart visibility property to Visible
}
} while(myShoppingListCursor.moveToNext());
}
if (viewId == R.id.txtViewFoodNameList) {((TextView) view).setText(cursor.getString(columnIndex)); }
if (viewId == R.id.txtViewFoodCategoryList){((TextView) view).setText(cursor.getString(columnIndex));};
if (viewId == R.id.ImgViewIconFoodList){
icName = "ic_oil";
}
Context c = getActivity().getApplicationContext();
int id = c.getResources().getIdentifier("drawable/"+icName, null, c.getPackageName());
((ImageView) view).setImageResource(id);
}
return true;
}});
lv.setAdapter(codes);
}

How can i align content left and right into the listview in android?

code:
{
private ListView l1;
Dbhelper mydb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.e_view);
l1 = (ListView)findViewById(R.id.listView1);
mydb = new Dbhelper(this);
ArrayList<Map<String, String>> list = mydb.getAllexps();
String[] from = { "name", "purpose" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2, from, to);
//adding it to the list view.
l1.setAdapter(adapter);
l1.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int id_To_Search = arg2 + 1;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(),Ex_add.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
plz help me out
I need to display amount right side of the list and other things to left side.
U need to create custom Listview
refere this link and u will get idea what i m telling u ;)
http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92
In the following line you are using android's default list item layout android.R.layout.simple_list_item_2.
SimpleAdapter adapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2, from, to);
Instead of using this default layout you can define your own custom layout by putting a XML file into your layout folder. In this custom layout file you can align the content as your need.
you should use this
<?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" >
<LinearLayout
android:id="#+id/pop_up_child_container_topview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"
android:weightSum="100" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="50" >
<TextView
android:id="#+id/pop_up_child_inventoryno_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="#string/str_fullhistory_popup_child_inv_no"
android:textSize="#dimen/list_title_textsize" />
<TextView
android:id="#+id/pop_up_child_inventoryno_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/pop_up_child_inventoryno_title"
android:padding="2dp"
android:text="30vuasou3209"
android:textSize="#dimen/subtext_textsize" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="50"
android:gravity="right" >
<TextView
android:id="#+id/pop_up_child_quantity_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="2dp"
android:text="#string/str_fullhistory_popup_child_qty"
android:textSize="#dimen/list_title_textsize" />
<TextView
android:id="#+id/pop_up_child_quantity_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/pop_up_child_quantity_title"
android:padding="2dp"
android:text="30vuasou3209"
android:textSize="#dimen/subtext_textsize" />
</RelativeLayout>
</LinearLayout>
<View
android:id="#+id/popup_child_seperator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/pop_up_child_container_topview"
android:background="#color/fleet_guard_seperator_grey" />
<LinearLayout
android:id="#+id/pop_up_child_container_balance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/popup_child_seperator"
android:baselineAligned="false"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:weightSum="100" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="50" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="50"
android:gravity="right" >
<TextView
android:id="#+id/pop_up_child_balance_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/pop_up_child_balance_tv"
android:text="#string/str_fullhistory_popup_child_balance"
android:textSize="#dimen/list_title_textsize" />
<TextView
android:id="#+id/pop_up_child_balance_tv"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="12"
android:textSize="#dimen/list_title_textsize" />
</RelativeLayout>
</LinearLayout>
Hi Om please use linearlayout with horizontal and add textview and set custom xml file to your adapter

ListView OnItemClickListener not Working

I create a statistics activity with listView . but onItemClick Not working I surffed internet but given solution not working for me.
try
{
int counter=0;
db = helper.getReadableDatabase();
c = db.query(DBHelper.Result, null, null, null, null, null, null);
c.moveToFirst();
do
{
counter++;
States state = new States(c.getString(2), c.getString(3), false);
stateList.add(state);
}while(c.moveToNext());
Log.d("counter", ""+counter);
/*adapter = new SimpleCursorAdapter(this, R.layout.row, c, new String [] {DBHelper.R_test,DBHelper.R_testNM}, new int []{R.id.rowTxt1,R.id.rowTxt2});
Lv.setAdapter(adapter);
Lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);*/
db.close();
}
catch(Exception e)
{
e.printStackTrace();
Log.d("Error", ""+e.getMessage());
}
// create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this, R.layout.row, stateList);
//ListView listView = (ListView) findViewById(R.id.LvStatistics);
// Assign adapter to ListView
Lv.setAdapter(dataAdapter);
Lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
Log.d("click", "0");
}
});
row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/Background"
android:orientation="vertical" >
<TextView
android:id="#+id/rowTxt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/ExamFontColor"
/>
<TextView
android:id="#+id/rowTxt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/rowTxt1"
android:layout_marginLeft="20dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/FontBlack"
/>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:text="1" />
</RelativeLayout>
exam_statistics.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/Background"
android:orientation="vertical" >
<TextView
android:id="#+id/statisticsHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#color/HeaderColor"
android:gravity="center"
android:text="#string/ButtonStatistics"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/FontWhite"
android:textSize="30sp" />
<ListView
android:id="#+id/LvStatistics"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/btnDelete"
android:background="#color/Background"
>
</ListView>
<Button
android:id="#+id/btnDelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/statisticsHeader"
android:layout_marginBottom="10dp"
android:background="#drawable/button_green_effect"
android:text="#string/ButtonDelete"
android:textColor="#color/FontWhite"
android:textSize="27sp" />
</RelativeLayout>
I tried a Lot but not Working help me to solve this problem
write this in ur xml inside checkbox tag
android:focusable="false"
try after using this..
If your MyCustomAdapter implements OnItemClickListener so you have to use the following code :
MyCustomAdapter.setOnItemclikListener(dataAdapter);
As there is a checkBox in the row, ListView OnitemClickListener wont work. You can write OnItemClickLister in the row (Adapter) , to get the click event.
If you add args2 in log does it return the click position?
Log.d("click", "Row " + args.toString);

custom listview edittext is not editable on button click when i use descedantsfocusability="blockdescedant" [duplicate]

I have a customized ListView. Each row has an EditText, Buttons & TextView. In order to make the ListView items clickable I have kept android:descendantFocusability="blocksDescendants" for row layout. If I don't keep the descendantFocusability I am not able to implement an action for onItemClick. If I keep descendantFocusability the EditTextwhich is present in my row is not gaining focus. I want the EditText focusable and also I should be able to click on each row to navigate to another Activity. Can anyone please help me in this. Thanks all.
Edit : In the CustomAdapter for EditText, I tried keeping onTouchListenerand also onClickListenerwhere I requestFocus but that does not seem to work.
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/recentrowLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/margin_left_5"
android:clickable="true"
tools:ignore="UseCompoundDrawables,HardcodedText,ContentDescription,UselessParent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edit_text_bg"
android:padding="#dimen/margin_left_5" >
<RelativeLayout
android:id="#+id/rl1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/addSubscribe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/flikart_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="FLIKART"
android:textColor="#color/gray"
android:textSize="#dimen/medium_text_size" />
<ImageView
android:id="#+id/addToFav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
<LinearLayout
android:id="#+id/rl2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/rl1"
android:layout_marginLeft="#dimen/margin_30"
android:layout_marginRight="#dimen/margin_30"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/text_desciption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:lines="2"
android:text="20% off on Smart Phones and basic Handsets has upto 50% OFF only"
android:textColor="#color/blue" />
<TextView
android:id="#+id/text_desciption2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:lines="1"
android:text="somethign something,......."
android:textColor="#color/gray" />
<TextView
android:id="#+id/couponTypeText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_left_10"
android:background="#drawable/button_bg"
android:gravity="center"
android:padding="#dimen/margin_left_10"
android:text="STEAL THE DEAL"
android:textColor="#color/white"
android:textSize="#dimen/little_small_text_size" />
</LinearLayout>
<RelativeLayout
android:id="#+id/rl3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/rl2"
android:layout_marginTop="#dimen/margin_left_10" >
<TextView
android:id="#+id/text_offer_expiry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="#dimen/margin_left_5"
android:text="Ends 10 days"
android:textColor="#color/red"
android:textSize="16sp" />
<RelativeLayout
android:id="#+id/rightLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp" >
<ImageView
android:id="#+id/comment_image_total"
android:layout_width="#dimen/dimenstion_25"
android:layout_height="#dimen/dimenstion_20"
android:background="#drawable/comments" />
<TextView
android:id="#+id/text_comments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/comment_image_total"
android:text="100 Comments"
android:textColor="#color/gray"
android:textSize="16sp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/ll2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="#id/rightLayout"
android:layout_toRightOf="#id/text_offer_expiry"
android:gravity="center_horizontal" >
<ImageView
android:id="#+id/like_image_total"
android:layout_width="#dimen/dimenstion_25"
android:layout_height="#dimen/dimenstion_25"
android:background="#drawable/like" />
<TextView
android:id="#+id/text_total_likes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/like_image_total"
android:text="999 Likes"
android:textColor="#color/gray"
android:textSize="16sp" />
</RelativeLayout>
</RelativeLayout>
<View
android:id="#+id/view"
android:layout_width="fill_parent"
android:layout_height="4dp"
android:layout_below="#id/rl3"
android:layout_marginTop="#dimen/margin_5"
android:background="#color/purple_clor" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="#dimen/title_bar_height"
android:layout_below="#id/view"
android:layout_centerVertical="true"
android:layout_marginTop="#dimen/margin_5" >
<RelativeLayout
android:id="#+id/likesLayout"
android:layout_width="#dimen/dimenstion_40"
android:layout_height="#dimen/dimenstion_40"
android:layout_centerVertical="true"
android:background="#drawable/unratedbkg" >
<Button
android:id="#+id/likesBtn"
android:layout_width="#dimen/dimenstion_30"
android:layout_height="#dimen/dimenstion_30"
android:layout_centerInParent="true"
android:background="#drawable/unrated" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/footermain"
android:layout_width="fill_parent"
android:layout_height="#dimen/dimenstion_40"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/likesLayout"
android:background="#color/white" >
<RelativeLayout
android:id="#+id/animLayout"
android:layout_width="100dp"
android:layout_height="#dimen/dimenstion_40"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/likesLayout"
android:background="#drawable/ratingbkg"
android:visibility="gone" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="#dimen/margin_5" >
<ImageView
android:id="#+id/like_image"
android:layout_width="#dimen/dimenstion_30"
android:layout_height="#dimen/dimenstion_30"
android:layout_centerInParent="true"
android:background="#drawable/like" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="#dimen/margin_5" >
<ImageView
android:id="#+id/dislike_image"
android:layout_width="#dimen/dimenstion_30"
android:layout_height="#dimen/dimenstion_30"
android:layout_centerInParent="true"
android:background="#drawable/dislike" />
</RelativeLayout>
</RelativeLayout>
<EditText
android:id="#+id/add_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#id/animLayout"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="Add Comment"
android:inputType="text" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
please Don't use setOnItemClickListener for item click .. i think that you should be use item view click inside adapter method
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "click item",Toast.LENGTH_LONG).show();
}
});
Remove this from main list item layout
android:descendantFocusability="blocksDescendants"
Thanks and enjoy this code !
Try to add this line to your activity in the manifest.xml
android:windowSoftInputMode="stateHidden|adjustResize|adjustPan"
you do like this:
first set in your listview's android:focusable="false";
then you want row to be clicked:
for this in your customAdapter you should do like this
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "Hello world",2000).show();
}
});
It will work.
By default, your items should now have the click option, when you made android:focusable="false" in listview.
No need to use descendantFocusability in listview.
main.xml
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animationCache="false"
android:scrollingCache="false"
android:smoothScrollbar="true" >
</ListView>
Create ArrayList add data into the arraylist.
lv=(ListView)findViewById(R.id.listView1);
lv.setItemsCanFocus(true);
for(int i=0;i<30 data-blogger-escaped-br="" data-blogger-escaped-i=""> list.add(i);
}
1)create adapter for the listview and set the position as tag for the edittext.
2)Normally,when scrolling the item position will change.So,you have to get the edittext tag and set it into the edittext id.from that you can avoid the change of the item position.
holder.caption = (EditText) convertView
.findViewById(R.id.editText12);
holder.caption.setTag(position);
holder.caption.setText(list.get(position).toString());
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
int tag_position=(Integer) holder.caption.getTag();
holder.caption.setId(tag_position);
Finally,add the text watcher to the edittext and store the changes into correct position in the list.
holder.caption.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
final int position2 = holder.caption.getId();
final EditText Caption = (EditText) holder.caption;
if(Caption.getText().toString().length()>0){
list.set(position2,Integer.parseInt(Caption.getText().toString()));
}else{
Toast.makeText(getApplicationContext(), "Please enter some value", Toast.LENGTH_SHORT).show();
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
}
});
Refer this link..http://velmuruganandroidcoding.blogspot.in/2014/08/edittext-in-listview-android-example.html
main.xml
<?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">
<ListView android:id="#+id/MyList" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:descendantFocusability="beforeDescendants">
</ListView>
</LinearLayout>
item.xml
<?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">
<EditText android:id="#+id/ItemCaption"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:layout_marginLeft="2dip" android:singleLine="true">
</EditText>
</LinearLayout>
AndroidCustomListViewActivity
public class AndroidCustomListViewActivity extends Activity {
private ListView myList;
private MyAdapter myAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myList = (ListView) findViewById(R.id.MyList);
myList.setItemsCanFocus(true);
myAdapter = new MyAdapter();
myList.setAdapter(myAdapter);
}
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ArrayList myItems = new ArrayList();
public MyAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < 20; i++) {
ListItem listItem = new ListItem();
listItem.caption = "Caption" + i;
myItems.add(listItem);
}
notifyDataSetChanged();
}
public int getCount() {
return myItems.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item, null);
holder.caption = (EditText) convertView
.findViewById(R.id.ItemCaption);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//Fill EditText with the value you have in data source
holder.caption.setText(myItems.get(position).caption);
holder.caption.setId(position);
//we need to update adapter once we finish with editing
holder.caption.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
final int position = v.getId();
final EditText Caption = (EditText) v;
myItems.get(position).caption = Caption.getText().toString();
}
}
});
return convertView;
}
}
class ViewHolder {
EditText caption;
}
class ListItem {
String caption;
}
}

Changing font face of a ListView without SimpleAdapter in Android

I want to change the font face of the list items in my ListView. I created a custom items for that because I want to let the user see the basic information on the database from that activity. How can I change the fonts of my views?
MainActivity.java
//Method in that fetch the data to list view
private void displayListView() {
final DatabaseHandler db = new DatabaseHandler(this);
Cursor cursor = db.fetchClinicByName("");
// The desired columns to be bound
String[] columns = new String[] { Constants.CLINIC_ID, Constants.CLINIC_NAME, Constants.CLINIC_ADD };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.textviewId, R.id.tv_ClinicName, R.id.tv_ClinicAdd };
simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.attribute_clinic, cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.lv_clinic);
listView.setAdapter(simpleCursorAdapter);
registerForContextMenu(listView);
attribute_clinic.xml
<?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" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clinic Name:"
android:textSize="20sp"
android:layout_marginRight="40dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_ClinicName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="19sp"
android:text="clinic"/>
</TableRow>
<TableRow
android:id="#+id/tableRow01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clinic Address:"
android:textSize="20sp"
android:layout_marginRight="20dp"
android:textStyle="bold"
android:paddingBottom="5dp"
android:layout_marginBottom="5dp" />
<TextView
android:id="#+id/tv_ClinicAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="19sp"
android:text="clinicAdd"
android:paddingBottom="5dp"
android:layout_marginBottom="5dp" />
</TableRow>
</LinearLayout>
<TextView
android:id="#+id/textviewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="114dp"
android:layout_marginTop="16dp"
android:visibility="gone" />
I have not actually tried this yet, but I believe this should work.
You need to override SimpleCursorAdapter's setViewText(). It would be something like this when your fonts are inside of your assets/fonts folder.
private void displayListView() {
final DatabaseHandler db = new DatabaseHandler(this);
Cursor cursor = db.fetchClinicByName("");
// The desired columns to be bound
String[] columns = new String[] { Constants.CLINIC_ID, Constants.CLINIC_NAME, Constants.CLINIC_ADD };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.textviewId, R.id.tv_ClinicName, R.id.tv_ClinicAdd };
simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.attribute_clinic,
cursor, columns, to, 0) {
#Override
public void setViewText(TextView v, String text) {
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/yourfont.ttf");
v.setTypeface(face);
v.setText(text);
}
};
ListView listView = (ListView) findViewById(R.id.lv_clinic);
listView.setAdapter(simpleCursorAdapter);
registerForContextMenu(listView);

Categories

Resources