开发者

Dynamically Button Text?

Top rows of three buttons shows the top three values of the database but from the next rows again top three values were shown in the three buttons ?

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);
    }
}

My NoteHolder Class Is

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));

    }
}

My SQLiteHelper Class is

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 AUTOINCRE开发者_开发百科MENT,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));
    }
}

}

My main.xml is

<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>


First I want to say is that Gridview will be more suitable in your case second you are getting multiple rows because cursor adapter will create those many view that it has rows and for each row you are again reading rows in populateFrom() method so that your rows are being repeated.

Instead of Cursor adapter use Base adapter

Solution

1) first create arraylist from your cursor

2) after getting arraylist create a object of NoteAdapter which is below

3) set adapter in 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 
}
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜