I've searched but not found any examples for what I want to do. I'm sure its trivial but I'm stumped. I have a custom listview adapter and a defined relative layout. The entire layout is being called for each item, I want my buttons to only appear once on top. Any advice would be greatly appreciated.
This is how it looks now:
http://img440.imageshack.us/img440/2803/device20120104173817.png
This is how I want it to look:
http://img215.imageshack.us/img215/805/devicewant.png
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/relativeLayout2">
<TextView
android:id="#+id/postinfo"
android:layout_below="#+id/FirstPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
<TextView
android:id="#+id/dateinfo"
android:layout_alignRight="#+id/postinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
<TextView
android:id="#+id/post"
android:layout_below="#+id/postinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
<ImageView
android:id="#+id/icon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/post"
android:contentDescription="OCForums thread icon"
android:focusable="false"
/>
<RelativeLayout
android:id="#+id/relativeLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top" >
<Button
android:id="#+id/LastPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/last" />
<Button
android:id="#+id/NextPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/LastPage"
android:text="#string/next" />
<Button
android:id="#+id/FirstPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/first" />
<Button
android:id="#+id/PreviousPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/FirstPage"
android:text="#string/previous" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
static class MyCustomAdapter2 extends BaseAdapter {
private LayoutInflater mInflater;
public MyCustomAdapter2(Context context, int textViewResourceId,List<List<String>> objects) {
super();
mInflater = LayoutInflater.from(context);
rid = textViewResourceId;
fcontext = context;
usernames = objects.get(0);
usernamecolors = objects.get(1);
dates = objects.get(2);
messages = objects.get(3);
Log.i("is it empy?(messages)",Integer.toString(messages.size()));
Log.i("is it empy?(dates)",Integer.toString(dates.size()));
Log.i("is it empy?(usernamecolors)",Integer.toString(usernamecolors.size()));
Log.i("is it empy?(usernames)",Integer.toString(usernames.size()));
}
public int getCount() {
return lists.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) {
convertView = mInflater.inflate(rid, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.postinfo);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(usernames.get(position));
holder.text.setTextColor(Color.parseColor(usernamecolors.get(position)));
ViewHolder holderd;
if (convertView == null) {
convertView = mInflater.inflate(rid, null);
holderd = new ViewHolder();
holderd.text = (TextView) convertView.findViewById(R.id.dateinfo);
convertView.setTag(holderd);
} else {
holderd = (ViewHolder) convertView.getTag();
}
holderd.text.setText(dates.get(position));
ViewHolder holderm;
if (convertView == null) {
convertView = mInflater.inflate(rid, null);
holderm = new ViewHolder();
holderm.text = (TextView) convertView.findViewById(R.id.post);
convertView.setTag(holderm);
} else {
holderm = (ViewHolder) convertView.getTag();
}
holderm.text.setText(messages.get(position));
ImageView icon=(ImageView)convertView.findViewById(R.id.icon2);
if (newlist.size() > 0 && lists.get(position).matches(newlist.get(position))){
icon.setImageResource(R.drawable.forum_new);
}
else{
icon.setImageResource(R.drawable.forum_old);
}
return convertView;
}
static class ViewHolder {
TextView text;
}
}
This is where I am calling it all.
ListView listview = (ListView) findViewById(R.id.listView3);
listview.setAdapter(new CustomListView.MyCustomAdapter2(ThreadActivity.this, R.layout.row2, output));
You need to define the header as a separate layout (separate xml file) then instantiate it and add it to the listview as a header -- then this layout will show as the first item in the list.
See this question: Android ListView with complex header
Related
I do design. And i am not know how.
if I clik on the item, should appear red view in right side.
I am use GridView and my adapter public class BasketAdapter extends BaseAdapter
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="142dp"
android:layout_height="wrap_content"
android:background="#eceff3"
android:paddingBottom="1dp">
<RelativeLayout
android:orientation="vertical"
android:layout_width="141.25dp"
android:layout_height="138.75dp"
android:background="#fff">
<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:id="#+id/goodImage"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="27dp"
android:layout_below="#+id/goodImage"
android:layout_marginTop="11dp"
android:id="#+id/linearLayout3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginLeft="28.75dp"
android:layout_marginRight="28.75dp">
<TextView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="bascetName"
android:id="#+id/bascetName"
android:textSize="12.50sp"
android:textStyle="bold"
android:textColor="#333333"
android:ellipsize="end"
android:singleLine="false"
android:gravity="center_horizontal" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="$1.23"
android:id="#+id/bascetPrice"
android:textSize="13.75sp"
android:layout_gravity="center_horizontal"
android:textColor="#ffc600"
android:layout_below="#+id/linearLayout3"
android:layout_centerHorizontal="true"
android:layout_marginTop=" 8.75dp" />
<ImageView
android:layout_width="32dp"
android:layout_height="38.50dp"
android:id="#+id/countImage"
android:src="#drawable/count"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="#+id/basketQuantity"
android:textSize="12.50sp"
android:textStyle="bold"
android:layout_gravity="right"
android:autoText="false"
android:textColor="#ffffff"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp" />
</RelativeLayout>
</LinearLayout>
adapter
public class BasketAdapter extends BaseAdapter implements View.OnClickListener {
private List<ShoppingCart> items;
private AnimateFirstDisplayListener animateFirstDisplayListener = new AnimateFirstDisplayListener();
private DisplayImageOptions options = ILOptions.getOption();
private Typeface font;
public BasketAdapter(List<ShoppingCart> items) {
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public ShoppingCart getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (null == convertView) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
font = Fonts.getBlockBertholdRegular(parent.getContext());
convertView = inflater.inflate(R.layout.basket_item, parent, false);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.bascetName);
holder.price = (TextView) convertView.findViewById(R.id.bascetPrice);
holder.imageView = (ImageView) convertView.findViewById(R.id.goodImage);
holder.imageCount = (ImageView) convertView.findViewById(R.id.countImage);
holder.quantity = (TextView) convertView.findViewById(R.id.basketQuantity);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(items.get(position).getItem().getName().toUpperCase());
holder.name.setTypeface(font, Typeface.BOLD);
holder.price.setText("$" + items.get(position).getTotal());
holder.price.setTypeface(font, Typeface.NORMAL);
ImageLoader.getInstance().displayImage(items.get(position).getItem().getImage(), holder.imageView, options, animateFirstDisplayListener);
if (items.get(position).getItemCount() == 1) {
holder.imageCount.setVisibility(View.GONE);
holder.quantity.setVisibility(View.GONE);
} else {
holder.quantity.setText(String.valueOf(items.get(position).getItemCount()));
holder.quantity.setTypeface(font, Typeface.BOLD);
}
convertView.setOnClickListener(this);
return convertView;
}
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(),"cvcxvfdg",Toast.LENGTH_LONG).show();
}
private static class ViewHolder {
TextView name;
TextView price;
ImageView imageView;
ImageView imageCount;
TextView quantity;
}
}
I will add something else if necessary. may have a library ready?
Change your ImageView by click gridview item:
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ViewHolder holder = (ViewHolder)view.getTag();
//set your new image here
holder.imageview.setImageResource(R.id.yourImage);
... ...
//maybe also update items content in 'position' if need
}
});
Hope this help!
I'm using an inflated ListView in my project. The layout file is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background2" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:cacheColorHint="#00000000"
android:fastScrollEnabled="true"
android:smoothScrollbar="true" >
</ListView>
</RelativeLayout>
And the inflated layout file is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="70dp"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/contact" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
The above code works fine when I'm using API level 11 or above, but below API level 11, it behaves in an awkward fashion, like the textView1 becomes totally invisible while textView2 is still visible. Also, while scrolling, all the items are VISIBLE but when the scrolling is finished, it again turns invisible. I'm using Actionbarsherlock. And yeah, one last thing, I'm using SearchView in actionbar, the bottom part of which strangely appears like a blue highlighted line in API 8 while everything works perfect in higher android levels
EDIT
This is my code snippet of ListView adapter
private class ContactViewHolder {
CheckBox checkBox;
TextView textView;
TextView textView2;
ImageView iv;
}
private class ContactArrayAdapter extends ArrayAdapter<Contact> {
//private LayoutInflater inflater;
List<Contact> myList;
Context mContext;
//Cursor cursor;
public ContactArrayAdapter(Context context, List<Contact> planetList) {
super(context, R.layout.simplerow, R.id.textView1, planetList);
// Cache the LayoutInflate to avoid asking for a new one each time.
//inflater = LayoutInflater.from(context);
mContext = context;
myList = planetList;
}
#Override
public int getCount() {
if(myList != null)
return myList.size();
return 0;
}
#Override
public Contact getItem(int position) {
if(myList != null)
return myList.get(position);
return null;
}
#Override
public long getItemId(int position) {
if(myList != null)
return myList.get(position).hashCode();
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Contact to display
ContactViewHolder holder;
//If the listview does not have an xml layout ready set the layout
if (convertView == null){
//we need a new holder to hold the structure of the cell
holder = new ContactViewHolder();
//get the XML inflation service
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Inflate our xml cell to the convertView
convertView = inflater.inflate(R.layout.simplerow, null);
//Get xml components into our holder class
holder.textView = (TextView) convertView.findViewById(R.id.textView1);
holder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
holder.iv = (ImageView) convertView.findViewById(R.id.imageView1);
holder.checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Contact contact = (Contact) cb.getTag();
contact.setChecked(cb.isChecked());
}
});
//Attach our holder class to this particular cell
convertView.setTag(holder);
}else{
//The listview cell is not empty and contains already components loaded, get the tagged holder
holder = (ContactViewHolder) convertView.getTag();
}
//Fill our cell with data
//get our person object from the list we passed to the adapter
Contact contact = getItem(position);
//Fill our view components with data
holder.textView.setText(name1.get(position));
holder.textView2.setText(num1.get(position));
holder.checkBox.setChecked(contact.checked);
return convertView;
}
}
The Contact class is
private static class Contact {
private String name = "";
private boolean checked = false;
public Contact() {
}
public Contact(String name) {
this.name = name;
}
public Contact(String name, boolean checked) {
this.name = name;
this.checked = checked;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public String toString() {
return name;
}
public void toggleChecked() {
checked = !checked;
}
}
Here is how to write a good adapater for your listview
public class MyAdapter extends ArrayAdapter<Person> {
Context context;
List<Person>myList;
public MyAdapter(Context context, int resource, List<Person> objects) {
super(context, resource, objects);
this.context = context;
this.myList = objects;
}
#Override
public int getCount() {
if(myList != null)
return myList.size();
return 0;
}
#Override
public Person getItem(int position) {
if(myList != null)
return myList.get(position);
return null;
}
#Override
public long getItemId(int position) {
if(myList != null)
return myList.get(position).hashCode();
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
//If the listview does not have an xml layout ready set the layout
if (convertView == null){
//we need a new holder to hold the structure of the cell
holder = new Holder();
//get the XML inflation service
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Inflate our xml cell to the convertView
convertView = inflater.inflate(R.layout.person_cell, null);
//Get xml components into our holder class
holder.txtName = (TextView)convertView.findViewById(R.id.person_cell_txtName);
holder.txtSurname = (TextView)convertView.findViewById(R.id.person_cell_txtSurname);
holder.imageView = (ImageView)convertView.findViewById(R.id.person_cell_imageview);
//Attach our holder class to this particular cell
convertView.setTag(holder);
}else{
//The listview cell is not empty and contains already components loaded, get the tagged holder
holder = (Holder)convertView.getTag();
}
//Fill our cell with data
//get our person object from the list we passed to the adapter
Person person = getItem(position);
//Fill our view components with data
holder.txtName.setText(person.getName());
holder.txtSurname.setText(person.getSurname());
Picasso.with(context).load(person.getImageUrl()).fit().into(holder.imageView);
return convertView;
}
/**
* This holder must replicate the components in the person_cell.xml
* We have a textview for the name and the surname and an imageview for the picture
*/
private class Holder{
TextView txtName;
TextView txtSurname;
ImageView imageView;
}
}
Notice that for Holder class you only set the properties reflecting the inflated xml. You dont need any constructor or getter/setters
In the get view you have to attach the holder class to the convertView tag property (convertView.setTag(holder))
retrieve the tagged holder if the convertView is not null
try to follow this pattern for the adapter
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/contact" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="fill_vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp" />
</LinearLayout>
</LinearLayout>
I have implemented a custom listview with checkboxes in the single choice mode of the android listview.I want a functionality such that when I click on the checkbox the row corresponding to that check box must get added(or rather say the "Person" object shown in that row should get added to another list and be removed from that list) and on itemclick on that listview should take me to another screen in android.I tried the other ways but they specify that the checkbox needs to be focasable:false.Also I want the click listener to work on the check box only.Please any suggestions or help on these.Thanks in advance
This id the xml code.
<?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" >
<CheckBox
android:id="#+id/projects_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:focusable="false" />
<TextView
android:id="#+id/project_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/projects_check"
android:layout_alignBottom="#+id/projects_check"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_toEndOf="#+id/projects_check"
android:layout_toRightOf="#+id/projects_check"
android:text="TextView" />
</RelativeLayout>
And this is my onitem click listener code..
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long arg3) {
Projects info = (Projects) parent.getItemAtPosition(position);
info.toggleChecked();
ProjectsHolder viewHolder = (ProjectsHolder) view.getTag();
viewHolder.getmChoiceSelect().setChecked(info.isChecked());
if (viewHolder.getmChoiceSelect().isChecked()) {
completed_projects.add(info);
mCompProjAdapter.notifyDataSetChanged();
projects_list.remove(info);
mProjAdapter.notifyDataSetChanged();
}
}
To manage click onto the row itself, you can just write listener for listView itself.
This is how i manage checkbox click.
class CustomAdapter extends ArrayAdapter<BeanClass> {
private ArrayList<BeanClass> items;
public FieldPlannedAdapter(Context context,
ArrayList<BeanClass> items) {
super(context, R.layout.custom_list_row, items);
this.items = items;
}
public class ViewHolder {
protected CheckBox checkbox;
protected TextView name;
}
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.custom_list_row,
null);
viewHolder = new ViewHolder();
viewHolder.checkbox = (CheckBox) convertView
.findViewById(R.id.checkIsMissed);
viewHolder.name = (TextView) convertView
.findViewById(R.id.txtViewMTPname);
convertView.setTag(viewHolder);
viewHolder.checkbox
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final CheckBox cb = (CheckBox) v;
final BeanClass item = (BeanClass) cb
.getTag();
item.setSelected(cb.isChecked());
if (cb.isChecked()) {
// DO SOMETHING
} else {
}
}
});
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
BeanClass item = items.get(position);
viewHolder.name.setTag(item);
viewHolder.checkbox.setTag(item);
viewHolder.name.setText(item.getName());
viewHolder.checkbox.setChecked(item.isSelected());
if (viewHolder.checkbox.isChecked()) {
// DO SOMETHING
} else {
// DO SOMETHING
}
return convertView;
}
}
Custom row example :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
<LinearLayout
android:id="#+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/card_ui"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<TextView
android:id="#+id/txtViewMTPname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Customer Name"
android:textColor="#drawable/text_selector"
android:textSize="14sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtViewMtpDarCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="dgdgdgdg"
android:textColor="#drawable/text_selector"
android:textSize="16sp"
android:textStyle="italic" />
<CheckBox
android:id="#+id/checkIsMissed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:gravity="center"
android:text="Missed" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
I have created custom LISTVIEW for showing searched result list want to show each row with background color I tried out with some code but not working as per my requirement please help me out
here is my code
public class ListCustomBaseAdapter extends BaseAdapter {
private static ArrayList<SearchResult> searchArrayList;
private LayoutInflater mInflater;
public ListCustomBaseAdapter(Context context,
ArrayList<SearchResult> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.custNm);
holder.txtProsNm = (TextView) convertView.findViewById(R.id.prosNm);
holder.txtFrmPort = (TextView) convertView
.findViewById(R.id.frmPort);
holder.txtToPort = (TextView) convertView.findViewById(R.id.ToPort);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (position % 2 == 0) {
holder.txtName.setBackgroundColor(Color.WHITE);
holder.txtProsNm.setBackgroundColor(Color.WHITE);<----- change bgcolor of textview not whole row of View
holder.txtFrmPort.setBackgroundColor(Color.WHITE);
holder.txtToPort.setBackgroundColor(Color.WHITE);
} else {
holder.txtName.setBackgroundColor(Color.DKGRAY);
holder.txtProsNm.setBackgroundColor(Color.DKGRAY);
holder.txtFrmPort.setBackgroundColor(Color.DKGRAY);
holder.txtToPort.setBackgroundColor(Color.DKGRAY);
}
holder.txtName.setText(searchArrayList.get(position).getCustNm());
holder.txtProsNm.setText(searchArrayList.get(position).getProsNm());
holder.txtFrmPort.setText(searchArrayList.get(position).getFrmPort());
holder.txtToPort.setText(searchArrayList.get(position).getToPort());
return convertView;
}
static class ViewHolder {
TextView txtName;
TextView txtFrmPort;
TextView txtToPort;
TextView txtProsNm;
}
}
calling adapter
public void onCreate(Bundle savedInstanceState) throws SQLException {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Connection object for Spinner
con = new DatabaseConnection(this);
try {
// Get Filtermap from Parent Activity
// this Bundle contains HashMap
Bundle wrapper = getIntent().getBundleExtra("salesActList");
salesLstObj = (Map<String, Object>) wrapper.getSerializable("salesActCriteriaList");
salesLst = con.searchSalesActivity(salesLstObj);
ArrayList<SearchResult> searchResults = getSearchResults();
lv = (ListView) findViewById(R.id.srListView);
lv.setAdapter(new ListCustomBaseAdapter(this, searchResults));
});
} catch (SQLException se) {
se.getStackTrace();
String flag = "fail";
dialogBox(flag);
}
here is output img
want to show Blue color area to be in gray alternately how to make it?
Custome row view code
<TableLayout
style="#style/TableLayoutStyle"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<TableRow
android:id="#+id/tblRwCust"
style="#style/TableRowStyle" >
<TextView
android:id="#+id/custNm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF7F24"
android:textSize="18sp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/tblRwPros"
style="#style/TableRowStyle" >
<TextView
android:id="#+id/prosNm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
</TableRow>
<TableRow
android:id="#+id/tblRwPort"
style="#style/TableRowStyle" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/frmPort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:textColor="#808080" />
<TextView
android:id="#+id/ToPort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#808080" />
</LinearLayout>
</TableRow>
</TableLayout>
if(position%2 == 0 ){
holder.convertView.setBackgroundColor(Color.CYAN);
}else{
holder.convertView.setBackgroundColor(Color.YELLOW);
}
Here is my solution with Table layout instead Relative layout
<TableLayout
style="#style/TableLayoutStyle"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:id="#+id/myTable"
android:background="#FFFFFF">
in adapter
holder.myTable = (TableLayout) convertView.findViewById(R.id.myTable);
if (position % 2 == 0) {
holder.myTable.setBackgroundColor(Color.WHITE);
} else {
holder.myTable.setBackgroundColor(Color.GRAY);
}
i am trying to implement listView through setListAdapter and Efficient Adapter. I want that when list is show then the background should not be repeat. My code is repeating the whole layout of list.xml due to which my list item are showing with so much gap.
Right now my list is working like that:
But i want this type of view:
Here is my editText.xml in which i type the word and a list View is opened.
<EditText
android:id="#+id/start_edit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="top|left"
android:ems="10"
android:hint="Type to search"
android:paddingLeft="50dp" >
<requestFocus />
</EditText>
this layout is for list.xml :
<RelativeLayout
android:id="#+id/RelativeLayout_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/blue_cellbar" >
the list.xml file is repeating the layout in Effecient adapter:
here is my code:
listAdapter = new EfficientAdapter2(this);
setListAdapter(listAdapter);
public static class viewHolder2 {
TextView word;
TextView meaning;
ImageView image;
ImageView image_color;
RelativeLayout cell;
}
private class EfficientAdapter2 extends BaseAdapter implements Filterable,OnItemClickListener {
private Context context;
LayoutInflater inflater;
public EfficientAdapter2(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
}
public int getCount() {
// if(SearchWordString.isEmpty()==false)
// {
return SearchWordString.size();
/// }
//return 0;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder2 holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list, null);
ViewToUse=parent;
holder = new viewHolder2();
// Log.i("View","is Null");
convertView.setTag(holder);
} else {
//Log.i("View","is not Null");
holder = (viewHolder2) convertView.getTag();
}
holder.cell = (RelativeLayout) convertView
.findViewById(R.id.RelativeLayout_list);
return convertView;
}
UPDATE
public class Start extends ListActivity implements OnTouchListener,
android.view.GestureDetector.OnGestureListener {
// ////////////////////////////////////////////////////
/************* INNER CLASS VIEWHOLDER ****************/
// ////////////////////////////////////////////////////
onCreate()
{
ListView list_to_use = getListView();
listAdapter = new EfficientAdapter2(this);
list_to_use.setAdapter(listAdapter);
list_to_use.setBackgroundColor(2);
viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
search = (EditText) findViewById(R.id.start_edit);
search.addTextChangedListener(myTextWatcher);
}
public static class viewHolder2 {
TextView word;
TextView meaning;
ImageView image;
ImageView image_color;
RelativeLayout cell;
}
// ////////////////////////////////////////////////////
/*********** INNER CLASS EfficientAdapter ************/
// ////////////////////////////////////////////////////
private class EfficientAdapter2 extends BaseAdapter implements Filterable,OnItemClickListener {
private Context context;
LayoutInflater inflater;
public EfficientAdapter2(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
}
public int getCount() {
// if(SearchWordString.isEmpty()==false)
// {
return SearchWordString.size();
/// }
//return 0;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder2 holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_search_item, null);
ViewToUse=parent;
holder = new viewHolder2();
// Log.i("View","is Null");
convertView.setTag(holder);
holder.word = (TextView) convertView.findViewById(R.id.title_list);
holder.meaning = (TextView) convertView
.findViewById(R.id.textView_meaning_list);
holder.image = (ImageView) convertView
.findViewById(R.id.image_list);
holder.image_color = (ImageView) convertView
.findViewById(R.id.imageView_color_list);
holder.cell = (RelativeLayout) convertView
.findViewById(R.id.RelativeLayout_list);
} else {
//Log.i("View","is not Null");
holder = (viewHolder2) convertView.getTag();
}
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<EditText
android:id="#+id/start_edit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="top|left"
android:ems="10"
android:hint="Type to search"
android:paddingLeft="50dp" >
<requestFocus />
</EditText>
<ViewFlipper
android:id="#+id/viewFlipper1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top|left"
android:layout_marginTop="50dp" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/blue_home"
android:fastScrollEnabled="true"
android:smoothScrollbar="true"
android:divider="#drawable/blue_dic"
android:dividerHeight="250sp" >
</ListView>
</FrameLayout>
</ViewFlipper>
rows for listView:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="#+id/image_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/abacus_thumbnail"
android:scaleType="centerCrop" />
<ImageView
android:id="#+id/imageView_color_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/blue_thumbnail" />
<TextView
android:id="#+id/title_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/image_list"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/image_list"
android:gravity="center"
android:text="Abacus"
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/textView_meaning_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/title_list"
android:layout_below="#+id/title_list"
android:layout_marginTop="10dp"
android:text="TextView"
android:textColor="#000000"
android:textSize="25sp" />
I hope you have not provide android:dividerHeight attribute in your layout file under <ListView/>
If so please remove it.
this problem occurs when you have set the adapter class xml, parent root
match_content instead of wrap_content.
set the root content height wrap_content
As i can see in adapter layout there is no parent root like relative, linearlayout, Framelayout and ConstraintLayout etc.