开发者

Custom ListView problem

开发者 https://www.devze.com 2023-03-28 04:38 出处:网络
Hi, I tried to write a ListView that could change column size. But the first line of the list is always get inserted when I add new line. Could you please help me?

Custom ListView problem

Hi, I tried to write a ListView that could change column size. But the first line of the list is always get inserted when I add new line. Could you please help me?

UPDATED:

My problem is when I add new line by clicking "insert record", the first line is longer then other lines, why they are not the same?

Here is my code:

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);开发者_C百科
        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();
    }

}


I suggest you use a ListActivity instead of Activity for your class MyListActivity, you'll achieve what you're trying to do more easily. Have a look at the tutorial here for an idea List View on Android developers

0

精彩评论

暂无评论...
验证码 换一张
取 消