自定义ListView问题

时间:2011-08-14 11:58:06

标签: android listview listadapter

enter image description here

嗨,我试着写一个可以改变列大小的ListView。但是当我添加新行时,列表的第一行总是插入。你能帮我吗?

更新:

我的问题是当我通过点击“插入记录”添加新行时,第一行比其他行更长,为什么它们不一样?

这是我的代码:

MyListAdapter.java

package eric.android;

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

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

public class MyListAdapter extends BaseAdapter{
    Context context;
    List<Record> data = new ArrayList<Record>();

    public MyListAdapter(Context context){
        this.context = context;
    }

    public void setData(List<Record> d){
        this.data = d;
    }

    public List<Record> getData(){
        return this.data;
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int index) {
        return data.get(index);
    }

    public long getItemId(int arg0) {
        return arg0;
    }

    public void removeColumn(int index){
        for(Record record:data){
            record.removeData(index);
        }
    }

    public View getView(int arg0, View convertView, ViewGroup arg2) {
        if (convertView == null) {
//            LayoutInflater inflater = (LayoutInflater) context
//                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = new LinearLayout(context);
            ((LinearLayout)convertView).setOrientation(LinearLayout.HORIZONTAL);
        }
        Record record = data.get(arg0);
        for(String str: record.data()){
            TextView view = new TextView(context);
            view.setText(str);
            ((ViewGroup) convertView).addView(view);
        }
        return convertView;
    }

    public void addRecord(Record rec) {
        Log.d("record",rec.toString());
        data.add(rec);
    }

    public void removeRecord(int index) {
        data.remove(index);
    }

    public void addColumn(String d) {
        for(Record record:data){
            record.insertData(d);
        }
    }

}

MyListActivity.java

package eric.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MyListActivity extends Activity {
    MyListView list1;
    Button b1, b2, b3, b4;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        list1 = (MyListView)findViewById(R.id.list1);
        b1 = (Button)findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener(){
            public void onClick(View arg0) {
                list1.addColumn("new added");
            }
        });
        b2 = (Button)findViewById(R.id.button2);
        b2.setOnClickListener(new OnClickListener(){
            public void onClick(View arg0) {
                list1.removeColumn(0);
            }
        });
        b3 = (Button)findViewById(R.id.button3);
        b3.setOnClickListener(new OnClickListener(){
            public void onClick(View arg0) {
                list1.insertRecord("one,two,three");
            }
        });
        b4 = (Button)findViewById(R.id.button4);
        b4.setOnClickListener(new OnClickListener(){
            public void onClick(View arg0) {
                list1.removeRecord(0);
            }
        });
    }
}

MyListView.java

package eric.android;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

public class MyListView extends ListView {
    Context context;
    MyListAdapter adapter;

    public MyListView(Context context, AttributeSet as){
        super(context, as);
        this.context = context;
    }

    private MyListAdapter adapter(){
        if(adapter==null){
            adapter = new MyListAdapter(context);
        }
        return adapter;
    }

    public void insertRecord(Record rec){
        adapter().addRecord(rec);
        update();
    }

    public void insertRecord(String str){
        adapter().addRecord(new Record(str));
        update();
    }

    public void removeRecord(int index){
        adapter().removeRecord(index);
        update();
    }

    public void removeColumn(int index){
        adapter().removeColumn(index);
        update();
    }

    public void addColumn(String d){
        adapter().addColumn(d);
        update();
    }

    public void update(){
        setAdapter(adapter());
    }

}

Record.java

package eric.android;

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

public class Record {
    List<String> data = new ArrayList<String> ();

    public Record(List<String> d){
        data = d;
    }

    public Record(String str) {
        for(String item:str.split(",")){
            data.add(item);
        }
    }

    public void insertData(String str){
        data.add(str);
    }

    public void removeData(int x){
        data.remove(x);
    }

    public List<String> data() {
        return data;
    }

    public String toString(){
        return data.toString();
    }

}

1 个答案:

答案 0 :(得分:0)

我建议你为你的MyListActivity类使用ListActivity而不是Activity,你将会更轻松地实现你想要做的事情。看看这里的教程,了解一个想法List View on Android developers