程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java-安卓開發為何我點中EditText會調用BaseAdapter的getView方法

java-安卓開發為何我點中EditText會調用BaseAdapter的getView方法

編輯:編程綜合問答
安卓開發為何我點中EditText會調用BaseAdapter的getView方法

public class MainActivity extends Activity {
List list=new ArrayList();
String date;
ListView lv=null;
MessageAdapter ma=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.listView1);
list.add(new Msg(Type.Receive,"主人你好"));
ma=new MessageAdapter(this,list);
lv.setAdapter(ma);
}
}

MessageAdapter類:
public class MessageAdapter extends BaseAdapter{
private LayoutInflater mInflater;
private List<Msg> mDatas;

public MessageAdapter(Context context, List<Msg> datas)
{
    mInflater = LayoutInflater.from(context);
    mDatas = datas;
}

@Override
public int getCount()
{
    return mDatas.size();
}

@Override
public Object getItem(int position)
{
    return mDatas.get(position);
}

@Override
public long getItemId(int position)
{
    return position;
}

/**
 * 接受到消息為1,發送消息為0
 */
@Override
public int getItemViewType(int position)
{
    Msg msg = mDatas.get(position);
    return msg.getType() == Type.Receive ? 1 : 0;
}

@Override
public int getViewTypeCount()
{
    return 2;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    ViewHolder vh=null; 

    Msg chatMessage = mDatas.get(position);

    if(convertView==null){

        vh=new ViewHolder();
        if(chatMessage.getType()==Type.Receive){
             convertView=mInflater.inflate(R.layout.left,parent, false);
            vh.createDate=(TextView) convertView.findViewById(R.id.receiveDate);
            vh.content=(TextView) convertView.findViewById(R.id.receiveContent);

            convertView.setTag(vh);
        }else{

            convertView=mInflater.inflate(R.layout.right,null);
            vh.createDate=(TextView) convertView.findViewById(R.id.sendDate);
            vh.content=(TextView) convertView.findViewById(R.id.sendContent);
            convertView.setTag(vh);

        }       
    }else{

        vh=(ViewHolder) convertView.getTag();   
    }

    vh.createDate.setText(chatMessage.getContent());
    vh.content.setText(chatMessage.getDateStr());
        return convertView;
}

private class ViewHolder
{
    public TextView createDate;
    public TextView content;
}

}
Msg類:
public class Msg {
public String content,name,DateStr;
private Date date;
public Type type;
enum Type{
send,Receive;
}
public Msg(){

}
public Msg(Type type,String content){
super();
this.type=type;
this.content=content;
setDate(new Date());
}
public String getContent(){
return content;
}
public Type getType(){
return type;
}
public String getDateStr(){
return DateStr;
}
public void setDate(Date date){

this.date=date;
DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateStr=df.format(date);

}
public void setType(Type t){
this.type=t;
}
public Date getDate(){
return date;
}
public void setMsg(String msg){
content=msg;
}
}
點擊editText欄的時候就會調用getView方法,我找了好三四天了始終沒有找到問題的根結,現在把所有java代碼都貼上來了求幫我看看是哪裡出的問題

最佳回答:


哪裡有個edittext...........,執行getView肯定是因為listview發生變化 比如移動了或者什麼的

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved