setOnFocusChangeListener in ListView item - android

What I want to do is display actions in ActionBar, if item of ListView is selected.
So I create :
answer_item.xml:
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:weightSum="1">
<EditText android:id="#+id/simple_answer_item"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:singleLine="true"
android:layout_weight="1.0"
android:focusableInTouchMode="true"
android:focusable="true"
/>
<ImageView
android:id="#+id/file_loaded_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
/>
</LinearLayout>
And AnswerAdapter class :
public class AnswerAdapter extends ArrayAdapter<Answer>{
private final LayoutInflater mInflater;
private final Fragment parentFragment;
public AnswerAdapter(Context context, int textViewResourceId,Fragment parentFragment) {
super(context, textViewResourceId);
mInflater = LayoutInflater.from(getContext());
this.parentFragment = parentFragment;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final ViewHolder holder;
if(convertView == null) {
convertView = mInflater.inflate(R.layout.answer_item, null);
holder = new ViewHolder();
holder.answer = (EditText) convertView.findViewById(R.id.simple_answer_item);
holder.loadObjectImageView = (ImageView) convertView.findViewById(R.id.file_loaded_view);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
final Answer answer = getItem(position);
holder.answer.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
Toast.makeText(getContext(), "click " + position, Toast.LENGTH_SHORT).show();
if (parentFragment != null && parentFragment instanceof onAnswerListItemFocusChange)
((onAnswerListItemFocusChange) parentFragment).onListItemFocusChange(true, answer);
}
else {
Toast.makeText(getContext(), "unclick "+ position, Toast.LENGTH_SHORT).show();
final int position = view.getId();
final EditText answerBody = (EditText) view;
getItem(position).setBody(answerBody.getText().toString());
if (parentFragment != null && parentFragment instanceof onAnswerListItemFocusChange)
((onAnswerListItemFocusChange) parentFragment).onListItemFocusChange(false,answer);
view.clearFocus();
}
}
});
holder.answer.setText(answer.getBody());
holder.answer.setId(position);
return convertView;
}
private static class ViewHolder{
public EditText answer;
public ImageView loadObjectImageView;
}
}
Moreover I have android:descendantFocusability="afterDescendants" in ListView:
<ListView
android:id="#+id/bad_answer_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:descendantFocusability="afterDescendants">
</ListView>
So when I run app and focused EditText at position for exmaple 2 in ListView , I expected to see only "click 2" but in result I get "click 2" "unclick 2" "click 0" "unclick0" "click2"
And becouse of that I think in my ActionBar I see only one action
But normally in EditText not in ListView works fine and I see 3.
So please tell me what I'm doing wrong ?

Related

Android listView with editText : how to edit text?

I created a listview with a textview and an edittext inside of it.
I’ve also done an adapter with an arraylist of string as parameter.
I want to edit the elements of my arraylist with the edittext but it works only with numbers whereas I want to edit text.
When the inputtype is “text”, the “onFocusChange” doesn’t work and I cannot enter text but when the inputtype is number, it works.
Here is an extract of my code :
Adapter :
public View getView(int position, View view, ViewGroup parent) {
ViewHolder viewHolder;
if (view == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.lv_editetext_reponse, null, true);
viewHolder.textView1 = (TextView) view.findViewById(R.id.Item_name);
viewHolder.editText1 = (EditText) view.findViewById(R.id.Item_price);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.textView1.setText(reponses.get(position));
viewHolder.editText1.setId(position);
viewHolder.ref = position;
// Add listener for edit text
viewHolder.editText1
.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
int itemIndex = v.getId();
String enteredPrice = ((EditText) v).getText()
.toString();
reponses.set(itemIndex, enteredPrice);
}
}
});
return view;
}
private class ViewHolder {
TextView textView1;
EditText editText1;
int ref;
}
XML :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/Item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:paddingTop="5dp"/>
<EditText
android:id="#+id/Item_price"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_alignParentRight = "true"
android:layout_gravity="right"
android:hint="Price"
android:focusable="true"
android:focusableInTouchMode="true"
android:editable="true"
android:lines="1" />
</RelativeLayout>

Button in header when implementing StickyGridHeaders Android

I am trying to use StickyGridHeaders in my Android app and it is working great except when I try to add a clicklistener to a clickable ImageView in the headerview. In getHeaderView() in my BaseAdapter I am trying to do the following:
getHeaderView
#Override
public View getHeaderView(final int pos, View view, ViewGroup viewGroup) {
view = inflater.inflate(R.layout.gallery_item,viewGroup, false);
TextView title = (TextView) view.findViewById(R.id.title);
TextView date = (TextView) view.findViewById(R.id.date);
ImageView settings = (ImageView) view.findViewById(R.id.folder_settings);
settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(mContext, "The Click Worked.", Toast.LENGTH_SHORT).show();
}
});
GalleryItem galleryItem = galleryItems.get(pos);
icon.setImageResource(setIcon(galleryItem.getMode()));
title.setText(galleryItem.getTitle());
Date da = galleryItem.record.getDate("FILE_DATE");
SimpleDateFormat dateFormat = new SimpleDateFormat("LLLL-dd-yyyy");
String mDate = dateFormat.format(da);
date.setText(mDate);
return view;
}
gallery_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="55dp"
android:background="#color/lightgraymain"
android:id="#+id/linearLayout">
<ImageView
android:layout_width="55dp"
android:layout_height="match_parent"
android:id="#+id/icon"
android:src="#drawable/ic_gallery_mode_tag" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="521 North 7th Street, Lincoln, NE"
android:id="#+id/title"
android:textColor="#color/black"
android:layout_weight="1"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:padding="5dp"
android:fontFamily="sans-serif-condensed"
android:enabled="true"
android:ellipsize="marquee"
android:textIsSelectable="false"
android:singleLine="true"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="August-25-2014"
android:id="#+id/date"
android:layout_gravity="center_vertical"
android:fontFamily="sans-serif-light"
android:textColor="#color/gray" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/folder_settings"
android:src="#drawable/ic_gallery_options"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:padding="10dp"
android:layout_gravity="center"
/>
</LinearLayout>
I cannot get the toast to appear. I have tried implementing onHeaderClick() in the adapter as well to no avail. Any help would be greatly appreciated.
Thank you,
-Zach
You should implement the listener in the fragment/activity who hosts the StickyGrid
I made it to work like this:
#Override
public void onHeaderClick(AdapterView<?> adapterView, View view, long l) {
view.findViewById(R.id.folder_settings).performClick();
}
For a header with just one clickable element it's ok. If the header has more than one element which can be clickable this solution won't work.
Ok so after hours and hours of mind numbing searching for this answer, I finally figured it out. I was looking through the example here and noticed that in the header layout, they didn't specify the layout as
//remove the following from your header layout
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
I removed these properties from my header view completely and viola! it works. I feel pretty dumb after that, but hope it helps someone else.
UPDATE
Here is a little more detail in how I have implemented it.
Fragment Class
public static class PlaceholderFragment extends Fragment implements
StickyGridHeadersGridView.OnHeaderClickListener,
StickyGridHeadersGridView.OnItemClickListener,
StickyGridHeadersGridView.OnItemLongClickListener {
#Override
public void onHeaderClick(AdapterView<?> adapterView, final View view, long l) {
Log.i("asd","THE HEADER IS CLICKED");
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.i("asd","ITEM "+i+" IS CLICKED");
}
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, final View view, int i, long l) {
Log.i("asd","ITEM "+i+" IS LONG CLICKED");
return true;
}
Custom Base Adapter
public class MediaAdapter extends BaseAdapter implements StickyGridHeadersBaseAdapter{
private Context mContext;
ArrayList<MediaItem> mediaitems;
ArrayList<GalleryItem> galleryItems;
LayoutInflater inflater;
HashMap<String,ImageView> imageHolder;
public MediaAdapter(Context c,ArrayList<GalleryItem> l) {
mediaitems = new ArrayList<MediaItem>();
galleryRef = new HashMap<String, Integer>();
mContext = c;
galleryItems = l;
imageHolder = new HashMap<String, ImageView>();
inflater = LayoutInflater.from(c);
}
public GalleryItem getSelectedGallery(String id){
return ((Tidy)mContext.getApplicationContext()).startOrGetFileStore().getGalleryItem(id);
}
public int getCount() {
return mediaitems.size();
}
public Object getItem(int position) {
return mediaitems.get(position);
}
public long getItemId(int position) {
return position;
}
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public View getView(int position, View convertView, ViewGroup parent) {
GridView gv =(GridView)parent;
ViewHolder holder;
if (convertView == null) {// if it's not recycled, initialize some attributes
convertView = inflater.inflate(R.layout.media_item, parent, false);
holder = new ViewHolder();
holder.img = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
imageManager.displayImage(holder.img);
return convertView;
}
#Override
public int getCountForHeader(int i) {
return galleryItems.get(i).getMediaLength();
}
#Override
public int getNumHeaders() {
return galleryItems.size();
}
#Override
public View getHeaderView(final int pos, View view, ViewGroup viewGroup) {
HeaderViewHolder holder = new HeaderViewHolder();
if(view == null) {
view = inflater.inflate(R.layout.gallery_item, viewGroup, false);
holder.title = (TextView) view.findViewById(R.id.title);
holder.date = (TextView) view.findViewById(R.id.date);
holder.icon = (ImageView) view.findViewById(R.id.icon);
holder.settings = (ImageView) view.findViewById(R.id.folder_settings);
view.setTag(holder);
}
else{
holder = (HeaderViewHolder) view.getTag();
}
GalleryItem galleryItem = galleryItems.get(pos);
holder.icon.setImageResource(setIcon(galleryItem.getMode()));
holder.title.setText(galleryItem.getTitle());
Date da = galleryItem.record.getDate("FILE_DATE");
SimpleDateFormat dateFormat = new SimpleDateFormat("LLLL-dd-yyyy");
String mDate = dateFormat.format(da);
holder.date.setText(mDate);
holder.gallery = galleryItem.record;
return view;
}
class HeaderViewHolder {
DbxRecord gallery;
RelativeLayout layout;
TextView title;
TextView date;
ImageView icon;
ImageView settings;
}
class ViewHolder {
ImageView img;
}
}
View XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.tonicartos.widget.stickygridheaders.StickyGridHeadersGridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imagegrid"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="1"
android:stackFromBottom="false"
android:numColumns="auto_fit"
android:columnWidth="80dp"
/>
</RelativeLayout>

Custom List View with OnClickListener

I have custom ListView layout with a TextView and CheckBox. Everything works fine.
What I want is, when I click on the CheckBox or TextView (on the single View from ListView) both should behave like one object. (I can click on the CheckBox and it does not effect the TextView and TextView has no effect on CheckBox.) Code has no problem.
I have implemented all possible solutions but problem is still there. (One single click on every object of list should consider ONE COMPLETE CLICK for complete row.) I hope I explained very well.
MAIN ACTIVITY
package com.example.smsplanner;
public class SMSPlanner extends ListActivity {
ListView contactsListView;
private String TAG = "SMSPlanner"; CheckBox check;
int count;
List<ContactInfo> list = new ArrayList<ContactInfo>();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ph = new String[3];
phType = new String[3];
LoadContactListFromPhone();
ContactsAdapter contactadAdapter = new ContactsAdapter(this, list);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
setListAdapter(contactadAdapter);
}
#Override
public void onListItemClick(ListView parent, View v, int position, long id)
{
TextView tx =(TextView)v.findViewById(R.id.firstname);
TextView ph =(TextView)v.findViewById(R.id.phone);
Toast.makeText(this, tx.getText().toString() + " " + ph.getText().toString() + " " + Integer.toString(count), Toast.LENGTH_SHORT).show();
}
final class ContactHolder{
TextView txtviewfirstname;
CheckBox chkselected;
TextView txtviewphone;
}
void LoadContactListFromPhone()
{
loadlistandreturns();
}
void call()
{
Toast toast = Toast.makeText(this, "Called...",Toast.LENGTH_LONG);
toast.show();
}
}
CUSTOM ADAPTER
public class ContactsAdapter extends ArrayAdapter<ContactInfo>
{
private final Activity context;
int resourceid;
List<ContactInfo> list = null;
public ContactsAdapter(Activity context, List<ContactInfo> list) {
super(context, R.layout.contactrow, list);
this.context = context;
this.list = list;
}
#Override
public View getView(int position, View convertview, ViewGroup viewgroup){
View view = null;
if(convertview == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.contactrow, null);
ContactHolder holder = new ContactHolder();
holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
holder.chkselected = (CheckBox)view.findViewById(R.id.check);
view.setTag(holder);
}
else{
view = convertview;
}
ContactHolder holder2 = (ContactHolder) view.getTag();
holder2.txtviewfirstname.setText(list.get(position).firstname);
holder2.txtviewphone.setText(list.get(position).phonenumber);
holder2.chkselected.setChecked(list.get(position).selected);
return view;
}
final class ContactHolder{
TextView txtviewfirstname;
CheckBox chkselected;
TextView txtviewphone;
}
}
LAYOUT
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100" >
<RadioGroup
android:id="#+id/rgStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="15"
android:orientation="vertical" >
<TextView
android:id="#+id/firstname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RadioGroup>
<RadioGroup
android:id="#+id/rgStyle2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="85"
android:orientation="vertical" >
<CheckBox
android:id="#+id/check"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="false"
android:focusable="false"
android:focusableInTouchMode="false" >
</CheckBox>
</RadioGroup>
</LinearLayout>
1. Bydefault all the Rows of the ListView are enabled to listen to click....
You must implement onItemClickListener() for the ListView....
See this example:
http://www.mkyong.com/android/android-listview-example/
you can use a CheckedTextView, or you can create a Checkable Layout, like this one :
http://tokudu.com/2010/android-checkable-linear-layout/
i think you should make adapter as
public class ContactsAdapter extends BaseAdapter {
ArrayList<ContactInfo> mlist;
Context mcontext;
public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {
mlist = mchtlist;
mcontext = context;
}
#Override
public int getCount() {
return mlist.size();
}
#Override
public Object getItem(int postion) {
return mlist.get(postion);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertview, ViewGroup viewgroup){
View view = null;
if(convertview == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.contactrow, null);
ContactHolder holder = new ContactHolder();
holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
holder.chkselected = (CheckBox)view.findViewById(R.id.check);
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// to open the selected file in resp
// do your work here
}});
chkselected .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(context,// "checked is clicke="+pos, 12).show();
if (chkselected.isChecked())
{
// do your work here
} else {
// do your work here
}
}
});
view.setTag(holder);
}
else{
view = convertview;
}
ContactHolder holder2 = (ContactHolder) view.getTag();
holder2.txtviewfirstname.setText(list.get(position).firstname);
holder2.txtviewphone.setText(list.get(position).phonenumber);
holder2.chkselected.setChecked(list.get(position).selected);
return view;
}
}

The visibility of the items change when scrolled ListView

I am using a list view and an adapter for loading a list,each list item has a TextView,EditText and Image..I set the visibility of the arrow and the Edit text according to the position of the list row,everything works fine when I load the list for the first time...
But when I scroll through the list,visibility of the items keep changing...Kindly help me in this issue...The relevant codes has been attached...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:background="#FFFFFF">
<TextView android:layout_height="wrap_content" android:layout_width="0dip"
android:textSize="20dip" android:layout_weight="1"
android:id="#+id/textview_add_lot_list" android:textColor="#android:color/black"
android:paddingTop="10dip" android:paddingBottom="10dip"
android:paddingLeft="10dip"/>
<EditText android:layout_height="fill_parent" android:layout_width="0dip"
android:layout_weight="1" android:id="#+id/et_add_lot_list"
android:layout_gravity="center_vertical"/>
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content"
android:id="#+id/imageview_arrow_add_lot_list" android:layout_gravity="center_vertical"
android:visibility="invisible" android:src="#drawable/more_reviews_arrow"
android:paddingRight="10dip"/>
</LinearLayout>
Java code activity...
final ArrayList<String> listItems = new ArrayList<String>();
listItems.add("Parking name");
listItems.add("Address");
listItems.add("City");
listItems.add("State");
listItems.add("Zip");
listItems.add("Phone");
listItems.add("Web Address");
listItems.add(" ");
listItems.add("Parking Image");
listItems.add(" ");
listItems.add("Open Hours");
listItems.add(" ");
listItems.add("Web Reviews");
final AddParkingLotAdapter adapter = new AddParkingLotAdapter(mAppContext,0,listItems);
lv.setAdapter(adapter);
Java code...adapter
public class AddParkingLotAdapter extends ArrayAdapter<String> {
private ArrayList<String> mStrings;
private LayoutInflater mInflater;
private AppContext mContext;
private static int NON_EMPTY_ROW = 1;
private static int EMPTY_ROW = 0;
public AddParkingLotAdapter(Context context, int resId, List<String> strings) {
super(context, resId,strings);
mStrings = (ArrayList<String>) strings;
mContext = (AppContext) context;
mInflater = LayoutInflater.from(context);
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getCount() {
return mStrings.size();
}
#Override
public String getItem(int position) {
return mStrings.get(position);
}
#Override
public int getItemViewType(int position) {
if(position==7||position==9||position==11){
return EMPTY_ROW;
}else{
return NON_EMPTY_ROW;
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView itemTextView = null;
//different inflations for different type rows..
if(getItemViewType(position) == EMPTY_ROW){
if (convertView == null) {
convertView = mInflater.inflate(R.layout.review_empty_row, null);
}
}else if(getItemViewType(position) == NON_EMPTY_ROW){
if (convertView == null) {
convertView = mInflater.inflate(R.layout.add_parkinglist_item, null);
}
itemTextView = (TextView) convertView.findViewById(R.id.textview_add_lot_list);
itemTextView.setText(mStrings.get(position));
if (position==3||position==8||position==10||position==12){
ImageView itemImageView = (ImageView)convertView.findViewById(R.id.imageview_arrow_add_lot_list);
itemImageView.setVisibility(View.VISIBLE);
EditText editText = (EditText)convertView.findViewById(R.id.et_add_lot_list);
editText.setVisibility(View.INVISIBLE);
}
}
return convertView;
}
}
In this code:
if (position==3||position==8||position==10||position==12){
ImageView itemImageView = (ImageView)convertView.findViewById(R.id.imageview_arrow_add_lot_list);
itemImageView.setVisibility(View.VISIBLE);
EditText editText = (EditText)convertView.findViewById(R.id.et_add_lot_list);
editText.setVisibility(View.INVISIBLE);
}
you've got no else clause. That means that if position is 0,1,2,4,5 or 6 you don't explicitly set the visibility of the views and so the visibility will be whatever it was set to when the views were recycled. If convertView is non-null, you always need to reset the visibility of any items whose visibility may be been modified earlier.

Android : Buttons and edit Text repeated in listView

My problem is that my listView repeat all items of my layout.
I need to have the edit text and the button at the bottom of this listView.
But here the edit text and button is repeated for each row of listView and i don't know why.
If someone could help me
The activity :
public class MessActivity extends ListActivity
{
public Channel chan;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setListAdapter(Network.getInstance().Messages);
Bundle b = getIntent().getExtras();
this.chan = (Channel) b.get("chanSelected");
//add all message of selected chan to the messAdapter
for (int i = 0 ; i < this.chan.getListMessage().size() ; i++) {
Message mess = this.chan.getListMessage().get(i);
Network.getInstance().addMessage(mess);
}
setContentView(R.layout.mess_list);
}
}
The adapter :
public class MessageAdapter extends ArrayAdapter<Message>
{
private Context context;
private int textViewResourceId;
public MessageAdapter(Context context, int textViewResourceId)
{
super(context, textViewResourceId);
this.context = context;
this.textViewResourceId = textViewResourceId;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
MessHolder holder = null;
//row null
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(textViewResourceId, parent, false);
holder = new MessHolder();
holder.texte = (TextView)row.findViewById(R.id.listMess);
row.setTag(holder);
}
else
{
holder = (MessHolder)row.getTag();
}
Message mess = getItem(position);
holder.texte.setText(mess.getText());
return row;
}
static class MessHolder
{
TextView texte;
}
}
The layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#android:id/list">
</ListView>
<TextView
android:id="#+id/listMess"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom" >
//edittext
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Envoyer" />
</LinearLayout>
</LinearLayout>
Thank you for your help.
Use ListView method addFooter(View v).
You can define xml layout for this view and inflate it:
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
View footer = (View)inflater.inflate(R.layout.footer, lv, false);
lv.addFooterView(footer, null, false);
Firstly Reason of this issue is Listview Reuses its view when scrolls , So we should have a logic in our get view to manage that
if(convertview == null)
{
//code here
}else {
//code here
}
, but if you dont want to reuse then you can use following :
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return 500;
}

Categories

Resources