listview onItemClick not getting called while BaseAdapter - android

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>

Related

Can't click on element in ListView Android

I am trying to create a custom list. My list is contained in a Fragment that correctly implements onScrollListener and populates the list using an adapter. The problem is that I cannot click on each item and I cannot figure out why. Here there is the code of my layout fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:clickable="true">
<ListView
android:id="#+id/listNotification"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:paddingStart="15dp"
android:paddingEnd="15dp"
android:clickable="true"
/>
</LinearLayout>
and here there is the code of my custom list
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:clickable="true">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/imageNotification"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp"
android:focusable="false"/>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#33CC33"
android:focusable="false"/>
<TextView
android:id="#+id/idQuestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:focusable="false"
/>
<TextView
android:id="#+id/typeNotification"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:visibility="gone"
android:focusable="false"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
here there is the code that creates my list using the adapter and setting the onclicklistener
adapter = new NotificationListAdapter(getActivity(), this.rows);
list = (ListView) firstAccessView.findViewById(R.id.listNotification);
list.setAdapter(adapter);
list.setOnScrollListener(this);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(adapter.getItem(position).getTypeNotification()==0) {
mNotificationInteface.readQuestion(adapter.getItem(position).getQuestionId());
}
if(adapter.getItem(position).getTypeNotification()==1){
mNotificationInteface.readAnswer(adapter.getItem(position).getQuestionId());
}
}
});
and here there is the code of my adapter
public class NotificationListAdapter extends ArrayAdapter<NotificationItem> {
private View view;
private final Activity context;
private List<NotificationItem> rows;
private int count = 1;
public NotificationListAdapter(Activity context, List<NotificationItem> firstRows ) {
super(context, R.layout.list_notifications, firstRows);
this.context = context;
this.rows = firstRows;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.list_notifications, null);
view.setPadding(0,10,0,10);
holder = new ViewHolder();
holder.textNotification = (TextView) view.findViewById(R.id.textNotification);
holder.idQuestion = (TextView) view.findViewById(R.id.idQuestion);
holder.typeNotification = (TextView) view.findViewById(R.id.typeNotification);
holder.imageNotification = (ImageView) view.findViewById(R.id.imageNotification);
view.setTag(holder);
} else {
view=convertView;
holder = (ViewHolder) convertView.getTag();
}
int typeNotification = this.rows.get(position).getTypeNotification();
holder.textNotification.setTextColor(Color.BLACK);
holder.idQuestion.setText(String.valueOf(this.rows.get(position).getQuestionId()));
holder.typeNotification.setText(String.valueOf(this.rows.get(position).getTypeNotification()));
if(typeNotification==0){
holder.textNotification.setText(R.string.askQuestion);
holder.imageNotification.setImageResource(R.mipmap.iconuseranonymous);
}
if(typeNotification==1){
//nome da recuperare da con id notifica, quindi id utente quindi dome
holder.textNotification.setText(R.string.answerQuestion);
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").transform(new CircleTransform()).fit().centerCrop().into(holder.imageNotification);
}
if(typeNotification==2){
//nome e immagine da recuperare
holder.textNotification.setText(R.string.newFriend);
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").transform(new CircleTransform()).fit().centerCrop().into(holder.imageNotification);
}
return view;
}
#Override
public NotificationItem getItem(int position){
return this.rows.get(position);
}
#Override
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
static class ViewHolder {
ImageView imageNotification;
TextView textNotification;
TextView idQuestion;
TextView typeNotification;
int position;
}
Remove android:clickable="true" from the ListView and it's parent in your XML layout, and also from the root of your list item layout.

How to change view after click?

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!

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>

ListView Rows can't be inflated

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

Button under a couple of Textviews

I'm doing something quite similar to the tutorial http://www.vogella.com/articles/AndroidListView/article.html#listadvanced_interactive.
I now want to put a button at the bottom. Then click the button and do something with the selected Values.
I understand, that I need some new layout, because adding the button to the existing one would just add a button to every textview. But I cant figure out how to do it.
Some help would be very nice. Thanks in advance. :)
Edit: That's my layout at the moment.
<?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="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="30px" >
</TextView>
<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>
</RelativeLayout>
This creates something that looks like this:
http://i.stack.imgur.com/cN8Dl.png
My adapter looks like this:
public class AttributeAdapter extends ArrayAdapter<Attribute> {
private final List<Attribute> list;
private final Activity context;
public AttributeAdapter(Activity context, List<Attribute> list) {
super(context, R.layout.activity_select_attributes, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.activity_select_attributes, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Attribute element = (Attribute) viewHolder.checkbox
.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName() + " " + list.get(position).getType());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
Now i want a button at the bottom to continue with the selected values.
Create a new layout or xml file named activity_base and add following:
<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=".BaseActivity" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
>
</ListView>
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
in your ListActivity in the Oncreate add setContentView(R.layout.activity_base);
This should do the job.

Categories

Resources