Styling Spinner Action Bar - android

Hello I have problem to styling Spinner Action Bar. I have Spinner with custom adapter like this :
#Override
protected void onCreate(Bundle savedInstanceState) {
...
// Spinner list
SpinnerMenuForm = new ArrayList<SpinnerNavItem>();
SpinnerMenuForm.add(new SpinnerNavItem("02","Fill Order - HSD Bunker", "HSD"));
SpinnerMenuForm.add(new SpinnerNavItem("14","Fill Order - MFO Bunker", "MFO"));
// title drop down adapter
adapterSpinnerMenuForm = new Adapter_List_Form(getApplicationContext(), SpinnerMenuForm);
...
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate our menu from the resources by using the menu inflater.
getMenuInflater().inflate(R.menu.main, menu);
View view1= (View) MenuItemCompat.getActionView(actionbar_form);
if (view1 instanceof Spinner)
{
final Spinner spinner = (Spinner) view1;
spinner.setAdapter(adapterSpinnerMenuForm);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
TextView txtType= (TextView)spinner.getAdapter().getView(position, null, null).findViewById(R.id.txtType);
Variabel.type= txtType.getText().toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
return true;
}
and this adapter spinner
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item_form, null);
}
txtNumber = (TextView) convertView.findViewById(R.id.txtNumber);
txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
txtType = (TextView) convertView.findViewById(R.id.txtType);
txtNumber.setText(spinnerNavItem.get(position).getNumber());
txtTitle.setText(spinnerNavItem.get(position).getTitle());
txtType.setText(spinnerNavItem.get(position).getType());
txtTitle.setVisibility(View.GONE);
return convertView;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item_form, null);
}
txtNumber = (TextView) convertView.findViewById(R.id.txtNumber);
txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
txtType = (TextView) convertView.findViewById(R.id.txtType);
txtNumber.setText(spinnerNavItem.get(position).getNumber());
txtTitle.setText(spinnerNavItem.get(position).getTitle());
txtType.setText(spinnerNavItem.get(position).getType());
return convertView;
}
and this item spinner layout
<?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="fill_parent"
android:padding="5dp" >
<TextView android:id="#+id/txtNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
/>
<TextView android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/txtNumber"/>
<TextView android:id="#+id/txtType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
</RelativeLayout>
the result
I use http://jgilfelt.github.io/android-actionbarstylegenerator/ but never change Style Spinner same as at tutorial http://blog.stylingandroid.com/styling-the-actionbar-part-4/ like this
so how to solve it ? sorry for my english

<?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="20dp"
android:padding="5dp"
android:background="#ff00DDED"> /////clour code thst use
<TextView android:id="#+id/txtNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
/>
<TextView android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/txtNumber"/>
<TextView android:id="#+id/txtType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>

Related

InfoWindow containing a ListView maps v2 android

I have been trying to implement a ListView inside a custom Google maps info window.
How do I get the ListView to populate and show?
I have the info window showing with the other relevant information by creating this InfoWindowAdapter
class CustomInfoWindowAdapter implements InfoWindowAdapter {
#Override
public View getInfoContents(Marker arg0) {
// Getting view from the layout file info_window_layout
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View v = (View)inflater.inflate(R.layout.custom_info_window, null);
if (clickedClusterItem != null) {
//This is the area that does not show
ListView listitemview = (ListView) v.findViewById(R.id.ListItems);
InfoItemAdapter customadapter = new InfoItemAdapter(MapActivity.this,clickedClusterItem.ListItems);
listitemview.setAdapter(customadapter);
//Getting reference to the TextView to set footer
TextView footer = (TextView) v.findViewById(R.id.InfoFooter);
footer.setText(getResources().getString(R.string.info_window_footer));
//Getting reference to the TextView to set Title
TextView name = (TextView) v.findViewById(R.id.LocationName);
name.setText(clickedClusterItem.Name);
}
// Returning the view containing InfoWindow contents
return v;
}
#Override
public View getInfoWindow(Marker arg0) {
// TODO Auto-generated method stub
return null;
}
}
The Custom adapter for each item in the list
public class InfoItemAdapter extends ArrayAdapter<ListItem> {
private final Context context;
private final ArrayList<ListItem> listitems;
public InfoItemAdapter(Context context, ArrayList<ListItem> listitems) {
super(context, R.layout.custom_info_window_list_item);
this.context = context;
this.listitems= listitems;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.custom_info_window_list_item, parent, false);
TextView firstLine = (TextView) rowView.findViewById(R.id.firstLine);
firstLine.setText(listitems.get(position).getDescription());
TextView secondLine = (TextView) rowView.findViewById(R.id.secondLine);
secondLine.setText(listitems.get(position).getTimeString());
// Set the icon
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
imageView.setImageResource(listitems.get(position).getIcon());
return rowView;
}
}
These are my XML layouts
The parent view which contains the List and other properties which are populated
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/LocationName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:textSize="20sp"
android:textColor="#4e0e79" />
<ListView
android:id="#+id/ListItems"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="#+id/LocationName">
</ListView>
<TextView
android:id="#+id/InfoFooter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:textSize="16sp"
android:text="#string/info_window_footer" />
</RelativeLayout>
List Item
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="#string/icon_desc"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="12sp" />
<TextView
android:id="#+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="#id/icon"
android:gravity="center_vertical"
android:textSize="16sp" />
</RelativeLayout>
This poster seems to have accomplished it but I cant seem to get it to work. I do not need click functionality on the list android marker custom infowindow
OK, I could not get the list to populate within the info window.
I ended up getting my desired result by building the new views programatically within the CustomInfoWindowAdapter -> getInfoContents method.
This is my updated CustomInfoWindowClass which does work. If anyone comes across this who knows how to implement a list on a info window your input would be appreciated.
class CustomInfoWindowAdapter implements InfoWindowAdapter {
#Override
public View getInfoContents(Marker arg0) {
// Getting view from the layout file info_window_layout
LayoutInflater inflater = (LayoutInflater) MapActivity.this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View v = (View)inflater.inflate(R.layout.custom_info_window, null);
LinearLayout view = (LinearLayout)v;
if (clickedClusterItem != null) {
if(clickedClusterItem.ListItems.size()>0){
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 0, 10, 0);
for(ListItem listItem : clickedClusterItem.ListItems){
TextView listitemdesc = new TextView(MapActivity.this);
TextView listitemtime = new TextView(MapActivity.this);
listitemdesc.setText(hh.getDescription());
listitemdesc.setTextSize(18f);
listitemtime.setTextSize(12f);
listitemtime.setText(hh.getTimeString());
if(Helpers.isActive(listItem))
{
listitemdesc.setTextColor(Color.parseColor("#f7941d"));
listitemtime.setTextColor(Color.parseColor("#f7941d"));
}
else if(Helpers.isCommingSoon(listItem)){
listitemdesc.setTextColor(Color.parseColor("#008fd5"));
listitemtime.setTextColor(Color.parseColor("#008fd5"));
}
else{
listitemdesc.setTextColor(Color.parseColor("#515e64"));
}
listitemdesc.setLayoutParams(lp);
listitemtime.setLayoutParams(lp);
view.addView(listitemdesc);
view.addView(listitemtime);
}
}
//Getting reference to the TextView to set Title
TextView name = (TextView) v.findViewById(R.id.LocationName);
name.setText(clickedClusterItem.Name);
}
// Returning the view containing InfoWindow contents
return v;
}
#Override
public View getInfoWindow(Marker arg0) {
// TODO Auto-generated method stub
return null;
}
}

listview onItemClick not getting called while BaseAdapter

its been a hour im trying to figure out the issue. Im implementing my own Adapter,The onClick event of the listview is not getting called
THe custom layout xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4px"
android:layout_marginRight="10px" >
</CheckBox>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="23dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/imageView1"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="CodeLearn Chapter 1"
android:textSize="16sp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_alignLeft="#+id/textView1"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Description" />
</RelativeLayout>
The main layout 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" >
<TextView
android:id="#+id/tvUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
/>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Custom Adapter code
public class AssignmentAdapter extends BaseAdapter
{
public View getView(int rowNumber, View convertView, ViewGroup parent)
{
View view = null;
ViewHolder viewHolder;
final int row = rowNumber;
if (convertView == null)
{
LayoutInflater minflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// convertView = minflater.inflate(R.layout.listitem, parent,
// false);
view = minflater.inflate(R.layout.listitem, null);
viewHolder = new ViewHolder();
viewHolder.courseTitle = (TextView) view.findViewById(R.id.textView1);
viewHolder.assignmentTitle = (TextView) view.findViewById(R.id.textView2);
viewHolder.chkBox = (CheckBox) view.findViewById(R.id.check);
// chapterDesc.setText(myAssign.assignmentTitle);
viewHolder.chkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
// Model element = (Model) viewHolder.checkbox
// .getTag();
// element.setSelected(buttonView.isChecked());
if (isChecked)
{
Log.d(tag, "i got checked"+ row);
}
}
});
view.setTag(viewHolder);
} else
{
view = convertView;
viewHolder = (ViewHolder) convertView.getTag();
}
ViewHolder holder = (ViewHolder) view.getTag();
Assignment myAssign = listAssignment.get(rowNumber);
holder.courseTitle.setText(myAssign.courseTitle);
holder.assignmentTitle.setText(myAssign.assignmentTitle);
return view;
}
The MainActivity
public class MainActivity extends Activity
{
AssignmentAdapter assignmentAdaper;
Context context = MainActivity.this;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.home_page);
assignmentAdaper = new AssignmentAdapter(this);
ListView assignmentLists = (ListView) findViewById(R.id.listView1);
assignmentLists.setAdapter(assignmentAdaper);
Assignment myAssignment = new Assignment();
assignmentLists.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
Log.d("MainActivity", "wewew");
}
});
Since, I have checkbox in the listview layout. I need to disable the focus attribute of the checkbox
<CheckBox
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:focusable="false"
android:focusableInTouchMode="false" >
</CheckBox>

Android getView method do not read textView

I have ListAdapter there is getView(); method. This is a code for method:
#Override
public View getView(int idx, View view, ViewGroup parent)
{
if(view == null)
{
LayoutInflater vi = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.check, null);
}
return view;
}
And this is check.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
<TextView
android:id="#+id/checkedTextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/checkBox1"
android:layout_alignBottom="#+id/checkBox1"
android:layout_toRightOf="#+id/checkBox1"
android:textColor="#615742"
android:textSize="18sp" />
</RelativeLayout>
The problem is that it reads only checkbox. And it igonres my TextView. If I write in xml file only checkedtextview then text appears but then I can not implement checkbox. What is a problem in my code that it ignores textView and it shows only checkbox?
#Override
public View getView(int idx, View view, ViewGroup parent)
{
if(view == null)
{
LayoutInflater vi = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.check, null);
}
TextView tv = (TextView) view.findViewById(R.id.checkedTextView1);
tv.setText("XXXXXXXX");
return view;
}

Animate background on listview

I have a ListView that have in each row 2 TextView and a CheckBox.
I have a drawable (a line), that I want once the user check the CheckBox that the drawable will be scale as an animation at the background of the checked CheckBox row, I was able in my getView() method on my Custom Adapter, to animate the drawable as a background but it also scale the 2 TextView and the CheckBox, and I want that only the drawable background will be scale as an animation, how do i do that?
here is my getView() method on the custom adapter:
public View getView(final int position, View convertView, final ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.listview_item_row, parent, false);
// cache view fields into the holder
holder = new ViewHolder();
holder.itemTitle = (TextView) convertView.findViewById(R.id.itemTitle);
holder.itemQuantity = (TextView) convertView.findViewById(R.id.itemQuantity);
holder.itemCheck = (CheckBox) convertView.findViewById(R.id.itemCheck);
convertView.setTag(holder);
final Cursor cursor = getCursor();
final View rowView = convertView;
cursor.moveToPosition(position);
holder.itemTitle.setText(cursor.getString((cursor.getColumnIndex(MySQLiteHelper.KEY_NAME))));
holder.itemQuantity.setText(cursor.getString((cursor.getColumnIndex(MySQLiteHelper.KEY_QUANTITY))));
holder.itemCheck.setChecked(cursor.getInt(cursor.getColumnIndex(MySQLiteHelper.KEY_CHECKED)) == 1);
holder.itemCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
cursor.moveToPosition(position);
itemsDataSource.updateItemCheckBox(1, cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.KEY_ID)));
itemsDataSource.updateRoute(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)), true);
Animation drawLine = AnimationUtils.loadAnimation(context, R.anim.item_done);
rowView.setBackgroundResource(R.drawable.line);
rowView.startAnimation(drawLine);
ShowListActivity.PRIORITY++;
}
else {
cursor.moveToPosition(position);
itemsDataSource.updateItemCheckBox(0, cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.KEY_ID)));
itemsDataSource.updateRoute(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)), false);
rowView.setBackgroundResource(0);
ShowListActivity.PRIORITY--;
}
}
});
if (holder.itemCheck.isChecked()) rowView.setBackgroundResource(R.drawable.line);
else rowView.setBackgroundResource(0);
return rowView;
}
private static class ViewHolder {
TextView itemTitle;
TextView itemQuantity;
CheckBox itemCheck;
}
update
I managed to solve the problem:
first I changed the custom row layout file like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frameRowLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:weightSum="10">
<LinearLayout android:id="#+id/rowLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<CheckBox android:id="#+id/itemCheck"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="2" android:gravity="center"
android:focusable="false"
android:focusableInTouchMode="false"
android:singleLine="false" />
<TextView android:id="#+id/itemTitle"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="6"
android:gravity="center"
android:text="some text1"
android:textColor="#2B89A8"
android:textSize="22dp"
android:textStyle="bold" />
<TextView android:id="#+id/itemQuantity"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="2"
android:gravity="center"
android:text="some text2"
android:textColor="#2B89A8"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
<View
android:id="#+id/backgroundView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
the whole idea of Framelayout is that views can overlap each other, there for the View after the LinearLayout is overlapping the LinearLayout.
then I changed my getView() method in my custom adapter as Following:
public View getView(final int position, View convertView, final ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.listview_item_row, parent, false);
// cache view fields into the holder
holder = new ViewHolder();
holder.itemTitle = (TextView) convertView.findViewById(R.id.itemTitle);
holder.itemQuantity = (TextView) convertView.findViewById(R.id.itemQuantity);
holder.itemCheck = (CheckBox) convertView.findViewById(R.id.itemCheck);
holder.rowView = (View) convertView.findViewById(R.id.backgroundView);
convertView.setTag(holder);
final Cursor cursor = getCursor();
cursor.moveToPosition(position);
holder.itemTitle.setText(cursor.getString((cursor.getColumnIndex(MySQLiteHelper.KEY_NAME))));
holder.itemQuantity.setText(cursor.getString((cursor.getColumnIndex(MySQLiteHelper.KEY_QUANTITY))));
holder.itemCheck.setChecked(cursor.getInt(cursor.getColumnIndex(MySQLiteHelper.KEY_CHECKED)) == 1);
holder.itemCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
cursor.moveToPosition(position);
itemsDataSource.updateItemCheckBox(1, cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.KEY_ID)));
itemsDataSource.updateRoute(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)), true);
Animation drawLine = AnimationUtils.loadAnimation(context, R.anim.item_done);
holder.rowView.setBackgroundResource(R.drawable.line);
holder.rowView.startAnimation(drawLine);
ShowListActivity.PRIORITY++;
}
else {
cursor.moveToPosition(position);
itemsDataSource.updateItemCheckBox(0, cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.KEY_ID)));
itemsDataSource.updateRoute(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)), false);
holder.rowView.setBackgroundResource(0);
ShowListActivity.PRIORITY--;
}
}
});
if (holder.itemCheck.isChecked()) holder.rowView.setBackgroundResource(R.drawable.line);
else holder.rowView.setBackgroundResource(0);
return convertView;
}
private static class ViewHolder {
TextView itemTitle;
TextView itemQuantity;
CheckBox itemCheck;
View rowView;
}
hope you can understand what I did.

List with buttons - only first button registers

I have implemented a ListView, each element of which contains a checkbox and a button. I have written an adapter for the view as follows:
public class myadapter extends SimpleAdapter
{
LayoutInflater mLayoutInflater;
public myadapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
{
super(context, data, resource, from, to);
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View view = null;
Log.i("xx","getView called: position="+position);
if (convertView != null)
{
view = convertView;
}
else
{
view = mLayoutInflater.inflate(R.layout.custom_row_view, parent, false);
}
Button buttonEdit = (Button) view.findViewById(R.id.editbut);
CheckBox cb = (CheckBox)view.findViewById(R.id.checkbox);
buttonEdit.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Log.i("xx","Button pressed! position ="+position);
}
});
cb.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1)
{
Log.i("xx","Checkbox changed! position ="+position);
}
});
return super.getView(position, convertView, parent);
}
}
At runtime, if I click on a button in the top row of my list I always see "Button pressed! position = 0" in the log output. Similarly, if I click on the checkbox I see "Checkbox changed! position=0". The problem comes if I click on a button on any other element in the list. Sometimes I see the correct log message, but other times I see nothing at all. Any ideas?
EDIT: here's my custom_row_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="142dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView android:id="#+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#FFFF00"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
<TextView android:id="#+id/text2"
android:textSize="12sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
<TextView android:id="#+id/text3"
android:typeface="sans"
android:textSize="14sp"
android:textStyle="italic"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right" >
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="" />
<Button
android:id="#+id/editbut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Edit" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
do like this
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View view = convertView;
Log.i("xx","getView called: position="+position);
if (view == null)
{
view = convertView;
view = mLayoutInflater.inflate(R.layout.custom_row_view, parent, false);
}
Button buttonEdit = (Button) view.findViewById(R.id.editbut);
CheckBox cb = (CheckBox)view.findViewById(R.id.checkbox);
return view;
}
you are returning the superclass value, with convertview as a parameter.
return super.getView(position, convertView, parent);
but you are doing this:
if (convertView != null)
{
view = convertView;
}
else
{
view = mLayoutInflater.inflate(R.layout.custom_row_view, parent, false);
}
If convertView is null, you are inflating a new view, but you are not returning it.
my suggestion is
return view;
instead of
return super.getView(position, convertView, parent);
That should be enough
do this:
Button buttonEdit = (Button) view.findViewById(R.id.editbut);
CheckBox cb = (CheckBox)view.findViewById(R.id.checkbox);
buttonEdit.setTag(String.valueOf(positin));
cb.setTag(String.valueOf(positin));
buttonEdit.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Log.i("xx","Button pressed! position ="+arg0.getTag());
}
});
cb.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1)
{
Log.i("xx","Checkbox changed! position ="+arg0.getTag(););
}
});
you are using OnClickListener() inner as a inner class. for inner class the value of position remains zero. until you setTag() on button and then getTag from button.

Categories

Resources