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);
Related
I implemented my custom list but when I click an element of my list a Toast message should be displayed. It looks like that the setOnItemClickListener does not work.
Here there is the code of of my fragment layout in which there is my ListView
<?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"
android:background="#ffffff">
<ListView
android:id="#+id/listSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:paddingStart="15dp"
android:paddingEnd="15dp"
android:focusable="false"/>
</LinearLayout>
here there is the structure of my list (each single line)
<?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="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/imageContact"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp"
android:focusable="false"/>
<TextView
android:id="#+id/nameSurnameContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#33CC33"
android:focusable="false"/>
<TextView
android:id="#+id/idContact"
android:layout_width="match_parent"
android:layout_height="0dp"
android:visibility="invisible"
android:focusable="false"
/>
</LinearLayout>
</LinearLayout>
and here there is the mothod of my fragment that creates the layout and populate the list
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedBundle) {
View firstAccessView;
if(savedBundle==null) {
firstAccessView = inflater.inflate(R.layout.search_fragment_layout, null);
for(int i=0; i<8; i++){
Contact c = new Contact(idContact[i], nameSurname[i], facebookId[i], timeStamp[i]);
this.rows.add(c);
}
adapter = new SearchListAdapter(getActivity(), this.rows);
list = (ListView) firstAccessView.findViewById(R.id.listSearch);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getActivity().getApplicationContext(), "item selected", Toast.LENGTH_SHORT).show();
}
});
list.setOnScrollListener(this);
}else{
firstAccessView = getView();
}
return firstAccessView;
}
is there something that I missed in the xml files of in the Fragment file?
Remove android:focusable="false" from ListView.
android:focusable="false"
Remove this line from your listview xml file
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
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;
}
}
I want to put a context menu on a listview. This seems very simple to do but I am getting some interesting behavior. The listview has two fields, a textview and an editText. The long press opens up the menu on the EditText but nowhere else on the listview.
Here are the pieces of code.
public class ManageClass extends ListActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.manageclass);
//Tons of not relevant stuff that I would be happy to provide.
pfdata = new PortfolioData(this);
try {
Cursor cursor = getClasses();
showClasses(cursor);
} finally {
pfdata.close();
}
}
private Cursor getClasses() {
String sql = "select c._id, c.NAME as NAME , c.percentage as PERCENTAGE "
+ "from asset_classes c;";
SQLiteDatabase db = pfdata.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, null);
startManagingCursor(cursor);
return cursor;
}
private void showClasses(Cursor cursor) {
adapter = new MySimpleCursorAdapter(this, R.layout.classrow, cursor,
FROM, TO, t);
// Log.d("TEST", Integer.toString(adapter.getCount()));
setListAdapter(adapter);
adapter.notifyDataSetChanged();
//I've tried moving this line around but it hasn't made a difference.
registerForContextMenu(getListView());
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(Menu.NONE, 0, 0, "A");
menu.add(Menu.NONE, 1, 1, "B");
menu.add(Menu.NONE, 2, 2, "C");
}
Lastly, the layout itself which I admit is overly complicated
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/editText1"
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="#string/TCLabel" />
<TextView
android:id="#+id/classtotalpercentage"
android:layout_width="109dp"
android:layout_height="wrap_content"
android:textSize="50px" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/AddClassLabel"
android:layout_width="200px"
android:layout_height="wrap_content"
android:layout_weight="0.38"
android:text="#string/AddClassLabel" />
<Button
android:id="#+id/addclass_button"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="#string/addclassbutton_label" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/grey"
android:orientation="horizontal"
android:padding="10sp" >
<TextView
android:id="#+id/assetclassHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/assetclassHeader"
android:width="200dp" />
<TextView
android:id="#+id/percentageHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/assetclassHeader"
android:text="#string/percentageHeader"
android:width="100dp" />
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:fadeScrollbars="false"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_list_data" />
</RelativeLayout>
</LinearLayout>
I appreciate any advice. I would like the menu on the textview and not the edittext, if possible.
An update. I have found that it has something to do with the EditText. If I change it to a textview or remove it alltogether, then everything works as it should.
I had a similiar problem with checkboxes and found the issue was caused by the checkbox getting the focus. I changed the attribue to:
android:focusable="false"
and it now works correctly.
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) {
}