开发者社区> 问答> 正文

关于安卓开发为什么我点中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代码都贴上来了求帮我看看是哪里出的问题

展开
收起
爵霸 2016-03-20 08:19:14 2858 0
1 条回答
写回答
取消 提交回答
  • 不管多少个edittext,最好在适配器中来处理。适配器的getView方法来处理。 public View getView(int positon, View convertView, ViewGroup parent){ //省略代码 convertView = inflate.inflate(R.layout.bindxml,null);//bindxml.xml就是帮顶的xml,通过EditText edittext1 = (EditText)convertView.findViewById(R.id.edittext1); EditText edittext2 = (EditText)convertView.findViewById(R.id.edittext2); EditText edittext3 = (EditText)convertView.findViewById(R.id.edittext3); ..... //省略代码 }

    2019-09-18 15:00:27
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载