动态按钮文字?

时间:2011-10-02 04:59:24

标签: android

三行按钮的顶行显示数据库的前三个值,但是从下一行再次显示三个按钮中的前三个值?

public class ButtonTest extends Activity {
/** Called when the activity is first created. */

NoteHelper helper = null;
Cursor ourCursor = null;
NoteAdapter adapter = null;


    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        setContentView(R.layout.main);

        ListView list = (ListView) findViewById(R.id.list);

        helper = new NoteHelper(this);
        ourCursor = helper.getAll();

        startManagingCursor(ourCursor);

        adapter = new NoteAdapter(ourCursor);
        list.setAdapter(adapter);

    }

    catch (Exception e) {
        Log.e("ERROR", "ERROR IN CODE :" + e.toString());
        e.printStackTrace();
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    helper.close();
}


class NoteAdapter extends CursorAdapter {
    NoteAdapter(Cursor c) {
        super(ButtonTest.this, c);
    }

    @Override
    public void bindView(View row, Context ctxt, Cursor c) {

        NoteHolder holder = (NoteHolder) row.getTag();
        holder.populateFrom(c, helper);
    }

    @Override
    public View newView(Context ctxt, Cursor c, ViewGroup parent) {

        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.row, parent, false);
        NoteHolder holder = new NoteHolder(row);
        row.setTag(holder);
        return (row);
    }
}

我的NoteHolder类是

static class NoteHolder {
    private Button b1 = null;
    private Button b2 = null;
    private Button b3 = null;



    NoteHolder(View row) {
        b1 = (Button) row.findViewById(R.id.one);
        b2 = (Button) row.findViewById(R.id.two);
        b3 = (Button) row.findViewById(R.id.three);

    }

    void populateFrom(Cursor c, NoteHelper helper) {

        if (!c.moveToFirst()) return;
        b1.setText(helper.getNote(c));
        c.moveToNext();
        b2.setText(helper.getNote(c));
        c.moveToNext();
        b3.setText(helper.getNote(c));

    }
}

我的SQLiteHelper类是

class NoteHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "note.db";
    private static final int SCHEMA_VERSION = 1;
    public SQLiteDatabase dbSqlite;

    public NoteHelper(Context context) {
        super(context, DATABASE_NAME, null, SCHEMA_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE Notes (_id INTEGER PRIMARY KEY AUTOINCREMENT,note TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }


    public Cursor getAll() {
        return (getReadableDatabase().rawQuery("SELECT _id,note From Notes",
                null));

    }

    public String getNote(Cursor c) {           
        return(c.getString(1));
        }

    public Cursor getById(String id) {

        return (getReadableDatabase().rawQuery(
                "SELECT _id,note From Notes", null));
    }
}

}

我的main.xml是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="6dip"
  android:background="#1F8B26">

<ListView android:layout_height="wrap_content" android:id="@+id/list" 
android:layout_width="fill_parent"></ListView>

1 个答案:

答案 0 :(得分:1)

首先我要说的是Gridview将更适合您的情况 第二,你得到多行,因为游标适配器将创建那些有行的视图,并且每行都会在populateFrom() method中读取行,以便重复行。

而不是Cursor适配器使用Base adapter

<强>解决方案

1)首先从光标创建arraylist

2)获取arraylist后,在

下创建NoteAdapter的对象

3)在gridview中设置适配器

private class NoteAdapter extends BaseAdapter implements OnClickListener {

ArrayList<Note> arrNote;
LayoutInflater inflater;
public NoteAdapter(ArrayList<Note> arr) {
    this.arrNote = arr;
    inflater = ExploreDestination.this.getLayoutInflater();
}
@Override
public int getCount() {
    return arrNote.size();
}

@Override
public Object getItem(int index) {
    return arrNote.get(index);
}

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

@Override
public View getView(int position, View v, ViewGroup arg2) {
    v = inflater.inflate(R.layout.Notebutton, null);
    v.setTag(arrNote.get(position));
    ((Button)v).setText(arrNote.get(position).getTitle());
    v.setOnClickListener(this);
    return v;
}
@Override
public void onClick(View v) {
    //do your work 
}
}