ListView Rows can't be inflated - android

I am very new in Android and I'm trying to make a table for my application. I use listview and a custom adapter to populate 9 data in a row. I thought that it should work, but the listview element is not shown when I run the application. Thank you for your help
Here is the part of my adapter class which I believe that have some problems:
EDIT
public class PlayerAdapter extends BaseAdapter {
ArrayList<Player> PlayerList;
private Context mContext;
public PlayerAdapter(Context c,ArrayList<Player> List) {
mContext = c;
PlayerList=List;
}
public int getCount() {
return PlayerList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return PlayerList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return PlayerList.indexOf(getItem(position));
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ViewHolder viewHolder = null;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null) {
rowView = inflater.inflate(R.layout.list_view_activity, parent, false);
viewHolder = new ViewHolder();
viewHolder.text1 = (TextView) rowView.findViewById(R.id.textView2);
viewHolder.text2 = (TextView) rowView.findViewById(R.id.textView3);
viewHolder.text3 = (TextView) rowView.findViewById(R.id.textView4);
viewHolder.text4 = (TextView) rowView.findViewById(R.id.textView5);
viewHolder.text5 = (TextView) rowView.findViewById(R.id.textView6);
viewHolder.text6 = (TextView) rowView.findViewById(R.id.textView7);
viewHolder.text7 = (TextView) rowView.findViewById(R.id.textView8);
viewHolder.text8 = (TextView) rowView.findViewById(R.id.textView9);
viewHolder.text9 = (TextView) rowView.findViewById(R.id.textView10);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.text1.setText(PlayerList.get(position)._name);
viewHolder.text2.setText(""+PlayerList.get(position)._play);
viewHolder.text3.setText(""+PlayerList.get(position)._win);
viewHolder.text4.setText(""+PlayerList.get(position)._draw);
viewHolder.text5.setText(""+PlayerList.get(position)._lose);
viewHolder.text6.setText(""+PlayerList.get(position)._GD);
viewHolder.text7.setText(""+PlayerList.get(position)._GF);
viewHolder.text8.setText(""+PlayerList.get(position)._GA);
viewHolder.text9.setText(""+PlayerList.get(position)._PTS);
// set text for other TextViews too if u want to set value for each TextView I only show for 1st one
return rowView;
}
private class ViewHolder {
TextView text1;
TextView text2;
TextView text3;
TextView text4;
TextView text5;
TextView text6;
TextView text7;
TextView text8;
TextView text9;
}
}
Here is my main program:
ArrayList<Player> PlayerList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PlayerList = new ArrayList<Player>();
PlayerList.add(new Player("BER"));
PlayerList.add(new Player("DOM"));
PlayerList.add(new Player("FER"));
PlayerList.add(new Player("HEN"));
PlayerList.add(new Player("GAR"));
PlayerList.add(new Player("JER"));
PlayerList.add(new Player("JON"));
PlayerList.add(new Player("IRV"));
PlayerList.add(new Player("LIM"));
ListView listView = (ListView) findViewById(R.id.player_list);
listView.setAdapter(new PlayerAdapter(this,PlayerList));
}
And, here is the Player Class:
ADDED
public class Player {
String _name;
int _play,_win,_draw,_lose,_GF,_GA,_GD,_PTS;
public Player(String name){
_play=0;_win=0;_draw=0;_lose=0;_GF=0;_GA=0;_GD=0;_PTS=0;
_name = name;
}
void play(int goal,int concede){
_play++;_GF+=goal;_GA+=concede;_GD+=(goal-concede);
if(goal>concede){
_win++;
_PTS+=3;
}
else if(goal<concede){
_lose++;
}
else{
_draw++;
_PTS+=1;
}
}
activity_main:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:background="#000000" android:layout_margin="1dip" android:id="#+id/textView1" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="PLAYER" android:textColor="#fff"/>
<TextView android:background="#000000" android:layout_margin="1dip" android:id="#+id/textView1" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center_horizontal" android:text="P" android:textColor="#fff"/>
<TextView android:background="#000000" android:layout_margin="1dip" android:id="#+id/textView1" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center_horizontal" android:text="W" android:textColor="#fff"/>
<TextView android:background="#000000" android:layout_margin="1dip" android:id="#+id/textView1" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center_horizontal" android:text="L" android:textColor="#fff"/>
<TextView android:background="#000000" android:layout_margin="1dip" android:id="#+id/textView1" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center_horizontal" android:text="D" android:textColor="#fff"/>
<TextView android:background="#000000" android:layout_margin="1dip" android:id="#+id/textView1" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center_horizontal" android:text="GD" android:textColor="#fff"/>
<TextView android:background="#000000" android:layout_margin="1dip" android:id="#+id/textView1" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center_horizontal" android:text="GF" android:textColor="#fff"/>
<TextView android:background="#000000" android:layout_margin="1dip" android:id="#+id/textView1" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center_horizontal" android:text="GA" android:textColor="#fff"/>
<TextView android:background="#000000" android:layout_margin="1dip" android:id="#+id/textView1" android:layout_weight="0.3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center_horizontal" android:text="PTS" android:textColor="#fff"/>
</LinearLayout>
<ListView
android:id="#+id/player_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

Remove that for loop, get view method will take care of looping itself..
EDIT
You must initilize all the 10 textviews then only if you loop through will apply the text..
so initilize all 10 textviews outside the loop..

getView() is called for each row in the array you supplied to adapter. Hence you need to apply a loop in getView().
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_view_activity, parent, false);
TextView textView;
textView = (TextView) rowView.findViewById(tv[i]);
textView.setText(playerArray[position]._name);
return rowView;
}
*Note: Also you need to make only row-layout, like in your case it seems you want to display every row with a single textview. Hence, your R.layout.list_view_activity should contain a textView only.
Say textView in R.layout.list_view_activity has id description. So following change would be needed.
textView = (TextView) rowView.findViewById();

public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_view_activity, parent, null);
TextView textView;
// **for(int i=0;i<9;i++){// Remove this for loop inside your code**
textView = (TextView) rowView.findViewById(tv[position]);
textView.setText(playerArray[position]._name);
// }
return rowView;
}

Don't have to declare int[] tv
int[] tv = {R.id.textView2,R.id.textView3,R.id.textView4,R.id.textView5,R.id.textView6
,R.id.textView7,R.id.textView8,R.id.textView9,R.id.textView10};
and for loop
for(int i=0;i<9;i++){
textView = (TextView) rowView.findViewById(tv[i]);
textView.setText(playerArray[position]._name);
}
Changes u have to do
1. In getItemId(int position) u returning null return the position like this
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
2. In getItem(int position) u returning null return the specific position of ur array like this
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return playerArray[position];
}
3. In getView(int position, View convertView, ViewGroup parent) do something like this and try to follow ViewHolder pattern
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
View rowView = convertView;
if(rowView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_view_activity, parent, false);
viewHolder = new ViewHolder();
viewHolder.text1 = (TextView) rowView.findViewById(R.id.textView2);
viewHolder.text2 = (TextView) rowView.findViewById(R.id.textView3);
viewHolder.text3 = (TextView) rowView.findViewById(R.id.textView4);
viewHolder.text4 = (TextView) rowView.findViewById(R.id.textView5);
viewHolder.text5 = (TextView) rowView.findViewById(R.id.textView6);
viewHolder.text6 = (TextView) rowView.findViewById(R.id.textView7);
viewHolder.text7 = (TextView) rowView.findViewById(R.id.textView8);
viewHolder.text8 = (TextView) rowView.findViewById(R.id.textView9);
viewHolder.text9 = (TextView) rowView.findViewById(R.id.textView10);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) rowView.getTag();
}
viewHolder.text1.setText(playerArray[position]);
// set text for other TextViews too if u want to set value for each TextView I only show for 1st one
return rowView;
}
private class ViewHolder {
TextView text1;
TextView text2;
TextView text3;
TextView text4;
TextView text5;
TextView text6;
TextView text7;
TextView text8;
TextView text9;
}
Example of BaseAdapter to populate ListView using ArrayList(Ur Class)
Why ViewHolder pattern see this
Official tutorial
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
SCREENSHOT :
EDIT :
except String setText like this
viewHolder.text2.setText(String.valueOf(playerDataList.get(position)._play));
list_view_activity xml code
<?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="horizontal" >
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView10"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

View rowView = inflater.inflate(R.layout.list_view_activity, parent, false);
->
View rowView = inflater.inflate(R.layout.list_view_activity, null);

Related

Check box selection and list item click on the same listview in android

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>

button in fragmentlist only responds once, then "releases" when clicking on the row

When I click the button in the ListView, I see the toast once. Then further clicks don't show anything when I click away from the button and in an empty space in the ListView, all the button click events that were not responding earlier appear at once (many toasts pop up one after another). Any idea how to fix this?
class Adapter extends ArrayAdapter
{
HashMap<Integer, View> rowViews = new HashMap<Integer, View>();
public Adapter(Context context) {
super(context, R.layout.manage_member_row, members);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final View rowView;
if(rowViews.containsKey(position)) {
rowView = rowViews.get(position);
} else {
rowView = inflater.inflate(R.layout.manage_member_row, parent, false);
((TextView) rowView.findViewById(R.id.manage_member_row_name)).setText(members.get(position).name);
rowViews.put(position, rowView);
}
((Button) rowView.findViewById(R.id.manage_member_row_delete)).setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "btnclick", Toast.LENGTH_SHORT).show();
}
});
Log.i("CRC", "called");
return rowView;
}
//manage_member_row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:descendantFocusability="blocksDescendants"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:paddingTop="8dip"
android:paddingBottom="8dip"
android:background="#FFFFFF"
android:orientation="vertical">
<TextView
android:id="#+id/manage_member_row_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:textSize="18sp"
android:textColor="#53585E"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="15dp" />
<Button
android:id="#+id/manage_member_row_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="0dip"
android:paddingBottom="0dip"
android:paddingLeft="8dip"
android:clickable="true"
android:focusable="true"
android:textSize="18sp"
android:textColor="#4E4E4E"
android:background="#EAEAEA"
android:paddingRight="8dip"
android:textStyle="bold"
android:text="REMOVE"
android:layout_alignParentRight="true"
android:layout_marginRight="15dip" />
</RelativeLayout>
//fragment_manage_members.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="activities.ManageMembersActivity$PlaceholderFragment">
<ListView
android:layout_width="wrap_content"
android:descendantFocusability="blocksDescendants"
android:layout_marginTop="0dip"
android:dividerHeight="2.0dip"
android:divider="#EEEDF3"
android:layout_height="wrap_content"
android:id="#android:id/list" />
</RelativeLayout>
The problem is that sometimes position from getView are not synchronized have that problem before..
Also note that you use a HashMap<Integer, View> just to check if the view is already there that cost a lot of memory and will cause OutOfMemoryException..
so instead of putting it in a hashMap just create a ViewHolder for all your views. and add the object of the ViewHolder to the tag of the View..
example:
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder viewHolder = null;
if(view == null)
{
viewHolder = new ViewHolder();
view = mInflater.inflate(R.layout.chat_online_user_list_view_item, null);
viewHolder.userName = (TextView) view.findViewById(R.id.chat_online_user_name);
view.setTag(viewHolder);
}
else
viewHolder = (ViewHolder) view.getTag();
//DO THE ON CLICK LISTENER HERE
return view;
}
private class ViewHolder
{
private TextView userName;
}

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>

How to change Custom Listview row color change alternatively?

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);
}

RelativeLayout Buttons on top and custom listview below buttons

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

Categories

Resources