gpt4 book ai didi

android - 动态按钮文本?

转载 作者:行者123 更新时间:2023-11-29 02:10:39 24 4
gpt4 key购买 nike

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

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>

最佳答案

首先我想说的是Gridview更适合你的情况其次,您将获得多行,因为游标适配器将创建它具有行的许多 View ,并且对于每一行,您将再次读取 populateFrom() 方法中的行,以便重复您的行。

使用 Base 适配器代替 Cursor 适配器

解决方案

1) 首先从你的游标创建数组列表

2) 获取arraylist后创建一个NoteAdapter对象在下面

3) 在gridview中设置adapter

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

关于android - 动态按钮文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7624703/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com