The problem is this kind of, I define a ListViewAdapter inherited BaseAdapter, but a problem in the getView method, for example, I click on a button in the Item, position is 0, the result is 0,5,10,15 response (data on a total of 20, position up to 19), this is what happens Yeah, well, if my own use ViewHolder write, then the response is random, the other buttons directly.
public View getView (final int position, View view, ViewGroup parent) {
/ / Holder = new ViewHolder ();
if (view == null) {
view = inflater.inflate (R.layout.custom_list_item, null);
}
final Button praise = (Button) view.findViewById (R.id.praise);
praise.setText (. allMessage.get (position) getPraNum () + "");
praise.setOnClickListener (new OnClickListener () {
@ Override
public void onClick (View v) {
if (isPraised (allMessage.get (position). getMsgId ())) {
ViewUtil.toastShort ("like you've had a", (MainActivity) context);
return;
}
praise.setText (. allMessage.get (position) getPraNum () + 1 + "");
praise.setBackgroundResource (R.drawable.message_praise_yes);
new DownLoadThread (handler, allMessage.get (position). getMsgId ())
. Start ();
}
});
} Great God seeking advice Yeah. . . . I do not know what the situation. . . This problem has never happened before, listening confusion, swollen do? Jijiji. . .<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Nobody top of it? I top it. . .
Reply:
Click on an event in the outer OnItemClickListener in writing.
Reply:
Ah, how to write in the outer layer? You mean and ListView layer it? ? ? I not only want to monitor the controls inside, but also monitor the total Item
Reply:
It seems we have not met estimates, who can give me a formal method getView Yeah, there are a variety of listening Item inside the control. . . E-mail 1154786190@qq.com. . . . Greatly appreciated. . . .
Reply:
I not only want to monitor the controls inside, but also monitor the total Item
What is the meaning of this sentence?
With listView.setOnItemClickListener same ah
I feel your error should be clicked position and convertView confusion caused.
First wrote OnItemClickListener in try.
Reply:
Not a lot of the ListView Item Mody, there are a lot of controls in each Item, I also listen to each control. Understand? ? ?
Reply:
public View getView (final int position, View view, ViewGroup parent) {
/ / Holder = new ViewHolder ();
if (view == null) {
view = inflater.inflate (R.layout.custom_list_item, null);
}
final Button praise = (Button) view.findViewById (R.id.praise);
praise.setText (. allMessage.get (position) getPraNum () + "");
praise.setOnClickListener (new OnClickListener () {
@ Override
public void onClick (View v) {
if (isPraised (allMessage.get (position). getMsgId ())) {
ViewUtil.toastShort ("like you've had a", context);
return;
}
Button btn = (Button) v;
btn.setText (. allMessage.get (position) getPraNum () + 1 + "");
btn.setBackgroundResource (R.drawable.message_praise_yes);
new DownLoadThread (handler, allMessage.get (position). getMsgId ())
. Start ();
}
});
}
Reply:
I write this, listen for events OnClickListener inside each list item controls inside the getView registration, and list the click event OnItemClickListener written document that inherits listActivity
Reply:
Reply eighth floor, I have to extend the Adapter does not then write. . .
Reply:
Reply seventh floor Brother, I it's actually own button to change the background color, and you changed me how to use it?
Reply:
Expansion is what is meant by, Adapter just add data to the list of container, getview is the time to add the data call is not invoked when clicked
Reply:
GetView give examples of it.
Reply:
Do not write in class listening adapter class inside it.
You can refer to this example
http://download.csdn.net/detail/fzllz/5369093
Reply:
listener classes do not write in adapter class inside it
.You can refer to this example
http://download.csdn.net/detail/fzllz/5369093
Reply:
Problem solving, which is a rolling layout confusion, I should think the outset, you can listen to something when scrolling occurs in other places, the solution is to define a List or a map of the state of each Item saved on the line. . . . Thank you brother. Amount. .
Reply:
Code looked a long time did not see the problem, the layout will this affect?
Reply:
I have encountered this problem, ListFragment the listener item click event, the results point to the first item, 14, 21 will respond, I phone is Samsung note2, then for a colleague Meizu, the result is 0,5, 10,15 together response. The landlord how to solve specific code copy to me, ctz830404@163.com
Reply:
Encounter this same problem, find the source 1041190806@qq.com
Reply:
I have encountered this problem, headache, solve ah 361907106@qq.com, thanks!
Reply:
Can the landlord said specific point ah, Jia, thanks
Reply:
How to solve the landlord can send me copies of the source code 2682404611@qq.com
Reply:
The landlord how to solve ah send me copies of the source code to 1246805489@qq.com Jiqiu Thanks
Reply:
Landlord, I encountered a problem with you, can you send me the code share refer you, thank you! 469197334@qq.com
Reply:
package com.mars.library.adapter;
import java.util.ArrayList;
import java.util.HashMap;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.mars.library.entry.BookMyBorrow;
import com.mars.library.main.R;
import com.mars.library.util.BookDetailHandler;
import com.mars.library.util.ConstantUtil;
import com.mars.library.util.RequestThread;
import com.mars.library.util.VarUtil;
import com.mars.library.util.ViewUtil;
/ **
* Book simple adapter
*
* @ Author Administrator
*
* /
@ SuppressLint ({"HandlerLeak", "UseSparseArrays"})
public class ListViewBookMyBorrowAdapter extends BaseAdapter {
private Context context;
private ArrayList
private LayoutInflater inflater;
/ / Students: Save state of the button, to determine whether there is a button. If the state is not saved, the pull-down may not be shown later on.
/ / Students: Because this is all saved saved and mechanisms ListView is to save the current display, so that do not fly. But the memory of that province. So you need to save the state and then save, no, do not save
private HashMap
public ListViewBookMyBorrowAdapter (Context context, ArrayList
this.context = context;
this.allBook = allBook;
this.inflater = LayoutInflater.from (context);
}
@ Override
public View getView (final int position, View view, ViewGroup parent) {
/ / If you do not add this line of code, it is easy to overflow
if (view == null) {
view = inflater.inflate (R.layout.book_my_borrow_item, null);
}
if (allBook.isEmpty ()) {
return view;
}
final ViewHolder holder = new ViewHolder ();
holder.item = (RelativeLayout) view.findViewById (R.id.item);
holder.number = (TextView) view.findViewById (R.id.number);
holder.name = (TextView) view.findViewById (R.id.name);
holder.date = (TextView) view.findViewById (R.id.date);
holder.renew = (Button) view.findViewById (R.id.renew);
holder.progress = (ProgressBar) view.findViewById (R.id.progressBar);
holder.number.setText (allBook.get (position) getId ().);
holder.name.setText (allBook.get (position) getName ().);
holder.date.setText (allBook.get (position) getDate ().);
/ / Click to view details list of books
holder.item.setOnClickListener (new OnClickListener () {
@ Override
public void onClick (View v) {
lookUpBookDetail (position);
}
});
/ / Students: Look here
isCanRenew.put (position, allBook.get (position). isRenew ()) ;/ / save
/ / Students: Look here
if (isCanRenew.get (position) == false) {/ / If not
holder.renew.setVisibility (View.GONE);
} Else {/ / If you can renew
holder.renew.setVisibility (View.VISIBLE);
holder.renew.setEnabled (true);
}
holder.renew.setOnClickListener (new OnClickListener () {
@ Override
public void onClick (View v) {
String url = VarUtil.URL + "/ renew";
HashMap
allParams.put (ConstantUtil.ACCOUNT, VarUtil.student.getAccount ());
allParams.put (ConstantUtil.PASSWORD, VarUtil.student.getPassword ());
allParams.put (ConstantUtil.BARCODE, allBook.get (position) getBarcode ().);
allParams.put (ConstantUtil.DEPARTMENTID, allBook.get (position) getDepartmentId ().);
allParams.put (ConstantUtil.LIBRARYID, allBook.get (position) getLibraryId ().);
VarUtil.threadPool.submit (new RequestThread (url, new RenewHandler (holder),
allParams));
}
});
/ / If (allBook.get (position). IsRenew () == false) {
/ / Holder.renew.setVisibility (View.GONE);
/ /} Else {
/ / Holder.renew.setVisibility (View.VISIBLE);
/ / Holder.renew.setEnabled (true);
/ /
/ / Holder.renew.setOnClickListener (new OnClickListener () {
/ / @ Override
/ / Public void onClick (View v) {
/ / String url = VarUtil.URL + "/ renew";
/ / HashMap
/ / AllParams.put (ConstantUtil.ACCOUNT, VarUtil.student.getAccount ());
/ / AllParams.put (ConstantUtil.PASSWORD, VarUtil.student.getPassword ());
/ / AllParams.put (ConstantUtil.BARCODE, allBook.get (position) getBarcode ().);
/ / AllParams.put (ConstantUtil.DEPARTMENTID, allBook.get (position)
. / / GetDepartmentId ());
/ / AllParams.put (ConstantUtil.LIBRARYID, allBook.get (position) getLibraryId ().);
/ / VarUtil.threadPool.submit (new RequestThread (url, new RenewHandler (holder),
/ / AllParams));
/ /}
/ /});
/ /}
return view;
}
private void lookUpBookDetail (int position) {
/ / According to the parameters, request data
String url = ConstantUtil.URL + "/ lib / detail.aspx";
HashMap
allParams.put (ConstantUtil.DETAIL, "true");
allParams.put (ConstantUtil.BARCODE, allBook.get (position) getBarcode ().);
BookDetailHandler handler = new BookDetailHandler ((Activity) context);
VarUtil.threadPool.submit (new RequestThread (url, handler, allParams));
}
private class RenewHandler extends Handler {
private ViewHolder holder;
public RenewHandler (ViewHolder holder) {
this.holder = holder;
}
@ Override
public void handleMessage (Message msg) {
switch (msg.what) {
/ / Task start
case ConstantUtil.START:
holder.renew.setVisibility (View.INVISIBLE);
holder.progress.setVisibility (View.VISIBLE);
break;
/ / End of the mission
case ConstantUtil.END:
if (VarUtil.result.equals (ConstantUtil.SUCCESS)) {
holder.renew.setVisibility (View.GONE);
holder.progress.setVisibility (View.GONE);
} Else if (VarUtil.result.equals (ConstantUtil.FAIL)) {
holder.renew.setVisibility (View.VISIBLE);
holder.progress.setVisibility (View.INVISIBLE);
ViewUtil.toastShort ("renewal failed!", (Activity) context);
}
break;
/ / Task error
case ConstantUtil.INTERNET_ERROR:
ViewUtil.toastShort ("client network anomalies please try again later!!", (Activity) context);
break;
default:
break;
}
}
}
class ViewHolder {
RelativeLayout item;
TextView number;
TextView name;
TextView date;
Button renew;
ProgressBar progress;
}
@ Override
public int getCount () {
return allBook.size ();
}
@ Override
public Object getItem (int arg0) {
return allBook.get (arg0);
}
@ Override
public long getItemId (int arg0) {
return arg0;
}
}
I do not know and then send mail 1154786190@qq.com
Reply:
package com.mars.library.adapter;
import java.util.ArrayList;
import java.util.HashMap;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.mars.library.entry.BookMyBorrow;
import com.mars.library.main.R;
import com.mars.library.util.BookDetailHandler;
import com.mars.library.util.ConstantUtil;
import com.mars.library.util.RequestThread;
import com.mars.library.util.VarUtil;
import com.mars.library.util.ViewUtil;
/ **
* Book simple adapter
*
* @ Author Administrator
*
* /
@ SuppressLint ({"HandlerLeak", "UseSparseArrays"})
public class ListViewBookMyBorrowAdapter extends BaseAdapter {
private Context context;
private ArrayListallBook;
private LayoutInflater inflater;
/ / Students: Save state of the button, to determine whether there is a button. If the state is not saved, the pull-down may not be shown later on.
/ / Students: Because this is all saved saved and mechanisms ListView is to save the current display, so that do not fly. But the memory of that province. So you need to save the state and then save, no, do not save
private HashMapisCanRenew = new HashMap ();
public ListViewBookMyBorrowAdapter (Context context, ArrayListallBook) {
this.context = context;
this.allBook = allBook;
this.inflater = LayoutInflater.from (context);
}
@ Override
public View getView (final int position, View view, ViewGroup parent) {
/ / If you do not add this line of code, it is easy to overflow
if (view == null) {
view = inflater.inflate (R.layout.book_my_borrow_item, null);
}
if (allBook.isEmpty ()) {
return view;
}
final ViewHolder holder = new ViewHolder ();
holder.item = (RelativeLayout) view.findViewById (R.id.item);
holder.number = (TextView) view.findViewById (R.id.number);
holder.name = (TextView) view.findViewById (R.id.name);
holder.date = (TextView) view.findViewById (R.id.date);
holder.renew = (Button) view.findViewById (R.id.renew);
holder.progress = (ProgressBar) view.findViewById (R.id.progressBar);
holder.number.setText (allBook.get (position) getId ().);
holder.name.setText (allBook.get (position) getName ().);
holder.date.setText (allBook.get (position) getDate ().);
/ / Click to view details list of books
holder.item.setOnClickListener (new OnClickListener () {
@ Override
public void onClick (View v) {
lookUpBookDetail (position);
}
});
/ / Students: Look here
isCanRenew.put (position, allBook.get (position). isRenew ()) ;/ / save
/ / Students: Look here
if (isCanRenew.get (position) == false) {/ / If not
holder.renew.setVisibility (View.GONE);
} Else {/ / If you can renew
holder.renew.setVisibility (View.VISIBLE);
holder.renew.setEnabled (true);
}
holder.renew.setOnClickListener (new OnClickListener () {
@ Override
public void onClick (View v) {
String url = VarUtil.URL + "/ renew";
HashMapallParams = new HashMap ();
allParams.put (ConstantUtil.ACCOUNT, VarUtil.student.getAccount ());
allParams.put (ConstantUtil.PASSWORD, VarUtil.student.getPassword ());
allParams.put (ConstantUtil.BARCODE, allBook.get (position) getBarcode ().);
allParams.put (ConstantUtil.DEPARTMENTID, allBook.get (position) getDepartmentId ().);
allParams.put (ConstantUtil.LIBRARYID, allBook.get (position) getLibraryId ().);
VarUtil.threadPool.submit (new RequestThread (url, new RenewHandler (holder),
allParams));
}
});
/ / If (allBook.get (position). IsRenew () == false) {
/ / Holder.renew.setVisibility (View.GONE);
/ /} Else {
/ / Holder.renew.setVisibility (View.VISIBLE);
/ / Holder.renew.setEnabled (true);
/ /
/ / Holder.renew.setOnClickListener (new OnClickListener () {
/ / @ Override
/ / Public void onClick (View v) {
/ / String url = VarUtil.URL + "/ renew";
/ / HashMapallParams = new HashMap ();
/ / AllParams.put (ConstantUtil.ACCOUNT, VarUtil.student.getAccount ());
/ / AllParams.put (ConstantUtil.PASSWORD, VarUtil.student.getPassword ());
/ / AllParams.put (ConstantUtil.BARCODE, allBook.get (position) getBarcode ().);
/ / AllParams.put (ConstantUtil.DEPARTMENTID, allBook.get (position)
. / / GetDepartmentId ());
/ / AllParams.put (ConstantUtil.LIBRARYID, allBook.get (position) getLibraryId ().);
/ / VarUtil.threadPool.submit (new RequestThread (url, new RenewHandler (holder),
/ / AllParams));
/ /}
/ /});
/ /}
return view;
}
private void lookUpBookDetail (int position) {
/ / According to the parameters, request data
String url = ConstantUtil.URL + "/ lib / detail.aspx";
HashMapallParams = new HashMap ();
allParams.put (ConstantUtil.DETAIL, "true");
allParams.put (ConstantUtil.BARCODE, allBook.get (position) getBarcode ().);
BookDetailHandler handler = new BookDetailHandler ((Activity) context);
VarUtil.threadPool.submit (new RequestThread (url, handler, allParams));
}
private class RenewHandler extends Handler {
private ViewHolder holder;
public RenewHandler (ViewHolder holder) {
this.holder = holder;
}
@ Override
public void handleMessage (Message msg) {
switch (msg.what) {
/ / Task start
case ConstantUtil.START:
holder.renew.setVisibility (View.INVISIBLE);
holder.progress.setVisibility (View.VISIBLE);
break;
/ / End of the mission
case ConstantUtil.END:
if (VarUtil.result.equals (ConstantUtil.SUCCESS)) {
holder.renew.setVisibility (View.GONE);
holder.progress.setVisibility (View.GONE);
} Else if (VarUtil.result.equals (ConstantUtil.FAIL)) {
holder.renew.setVisibility (View.VISIBLE);
holder.progress.setVisibility (View.INVISIBLE);
ViewUtil.toastShort ("renewal failed!", (Activity) context);
}
break;
/ / Task error
case ConstantUtil.INTERNET_ERROR:
ViewUtil.toastShort ("client network anomalies please try again later!!", (Activity) context);
break;
default:
break;
}
}
}
class ViewHolder {
RelativeLayout item;
TextView number;
TextView name;
TextView date;
Button renew;
ProgressBar progress;
}
@ Override
public int getCount () {
return allBook.size ();
}
@ Override
public Object getItem (int arg0) {
return allBook.get (arg0);
}
@ Override
public long getItemId (int arg0) {
return arg0;
}
}
Reply:
Landlord, hello! I also appear like you situation. My code is as follows, trouble help us to see where the problem is. Small Bucai, you provide that code, I do not see a specific piece of code on which to solve the problem.
public class ContactInviteAdapter extends BaseAdapter
{
private Context mContext;
private List
private LayoutInflater inflater;
private boolean img_Flag = false;
HashMap
public ContactInviteAdapter (Context context, List
{
mContext = context;
this.contactLists = contactFriends;
inflater = LayoutInflater.from (mContext);
initMap ();
}
private void initMap ()
{
for (int i = 0; i
state.put (i, false);
}
}
@ Override
public int getCount ()
{
return contactLists == null 0: contactLists.size ();
?}
@ Override
public Object getItem (int arg0)
{
return contactLists == null 0: contactLists.get (arg0);
?}
@ Override
public long getItemId (int arg0)
{
return contactLists.indexOf (contactLists.get (arg0));
}
@ Override
public View getView (final int position, View convertView, ViewGroup parent)
{
final ViewHolder holder = new ViewHolder ();
View view = convertView;
if (view == null)
{
view = inflater.inflate (R.layout.contact_find_all, null);
}
else
{
view = convertView;
}
holder.imgCheck = (ImageView) view.findViewById (R.id.contact_find_all_edit_id);
holder.imgHead = (ImageView) view.findViewById (R.id.contact_img_id);
holder.tvName = (TextView) view.findViewById (R.id.contact_name_id);
holder.tvNumber = (TextView) view.findViewById (R.id.contact_number_id);
holder.tvInvite = (TextView) view.findViewById (R.id.contact_inviteby_id);
holder.imgHead.setBackgroundResource ((Integer) contactLists.get (position) get ("contactinviteimgHead").);
holder.tvName.setText (. contactLists.get (position) get ("contactinvitename") toString ().);
holder.tvNumber.setText (. contactLists.get (position) get ("contactinvitenumber") toString ().);
holder.tvInvite.setText (. contactLists.get (position) get ("contactinviteBy") toString ().);
holder.imgCheck.setOnClickListener (new View.OnClickListener ()
{
@ Override
public void onClick (View v)
{
if (! state.get (position))
{
holder.imgCheck.setImageDrawable (mContext.getResources () getDrawable (R.drawable.invitebuttonbk33).);
} Else
{
;
}
! img_Flag = state.get (position);
state.put (position, img_Flag);
notifyDataSetChanged ();
}
});
return view;
}
static class ViewHolder
{
ImageView imgCheck;
ImageView imgHead;
TextView tvName;
TextView tvNumber;
TextView tvInvite;
}
}
No comments:
Post a Comment