关于BaseAdapter的用法


注意:本文代码做了两次测试,所以bean类和myadapter类不对称,但本文可提供语法格式,代码均亲测
package com.example.administrator.daer_end;

/**
 * Created by Administrator on 2017/2/25.
 */

public class bean {
    private String name;
//    private int imagId;
    private int type;
    public static final int received=0;
    public static final int send=1;

    public bean(String name,int type) {
        this.name = name;
//        this.imagId = imagId;
        this.type=type;
    }

    public String getName() {
        return name;
    }

//    public int getImagId() {
//        return imagId;
//    }
    public int getType(){
        return type;
    }

}
package com.example.administrator.daer_end;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2017/2/25.
 */

public class myadapter extends BaseAdapter {
    private LayoutInflater mlayoutInflater;
    private Listlist;

    public myadapter(Context context, List list) {
        this.mlayoutInflater = LayoutInflater.from(context);
        this.list = list;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewHoder viewhoder=null;
        if (convertView==null)
        {
            viewhoder=new viewHoder();
            convertView=mlayoutInflater.inflate(R.layout.news,null);
            viewhoder.leftLayout= (LinearLayout) convertView.findViewById(R.id.layout_left);
            viewhoder.rightLayout= (LinearLayout) convertView.findViewById(R.id.layout_right);
            viewhoder.right_news= (TextView) convertView.findViewById(R.id.right);
            viewhoder.left_news= (TextView) convertView.findViewById(R.id.left);
//            viewhoder.imageId= (ImageView) convertView.findViewById(R.id.image1);
            convertView.setTag(viewhoder);
        }
        else {
            viewhoder= (viewHoder) convertView.getTag();
        }

        bean go=list.get(position);
        if (go.getType()==bean.received){
            viewhoder.leftLayout.setVisibility(convertView.VISIBLE);
            viewhoder.rightLayout.setVisibility(convertView.GONE);
            viewhoder.left_news.setText(go.getName());
        }

//        viewhoder.imageId.setImageResource(go.getImagId());
        else if (go.getType()==bean.send){
            viewhoder.rightLayout.setVisibility(convertView.VISIBLE);
            viewhoder.leftLayout.setVisibility(convertView.GONE);
            viewhoder.right_news.setText(go.getName());
        }
        return convertView;
    }
    class viewHoder{
        TextView left_news;
        TextView right_news;
//        ImageView imageId;
        LinearLayout leftLayout;
        LinearLayout rightLayout;
    }
}

package com.example.administrator.daer_end;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private List list=new ArrayList();
    private EditText inputText;
    private myadapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView= (ListView) findViewById(R.id.listview);
        inputText= (EditText) findViewById(R.id.input_text);
        Button button= (Button) findViewById(R.id.button);
        adapter=new myadapter(MainActivity.this,list);
        init();
        listView.setAdapter(adapter);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content=inputText.getText().toString();
                if (!"".equals(content)){
                    bean bea=new bean(content,bean.send);
                    list.add(bea);
                    adapter.notifyDataSetChanged();
                    listView.setSelection(list.size());
                    inputText.setText("");
                }
            }
        });
    }

    private void init() {
        bean one=new bean("你好啊",bean.received);
        list.add(one);
        bean two=new bean("你好,很高兴见到你",bean.send);
        list.add(two);
        bean three=new bean("最近过的怎么样",bean.received);
        list.add(three);
    }
}


你可能感兴趣的:(Android)