- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题:如何将 TextView 信息传递到变量中,以便在单击按钮时将该信息添加到创建的名为集合的 SQLite 数据库中。
遗漏的文件(因为我认为它们不相关):androidmanafest.xml、database.java(用于填充卡片数据库)和 cards.java(用于 setter getter)。
它的样子: enter image description here
主要 Activity :
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
SearchAdapter adapter;
MaterialSearchBar materialSearchBar;
List<String> suggestList = new ArrayList<>();
Database database;
//used for inserting card into collection database
DB_Controller controller;
TextView card_name, type, rarity, artist;
Button addCollectionButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//This is all for the controller in order to pass each card into collection database
controller = new DB_Controller(this,"collection.db",null,1);
setContentView(R.layout.layout_item);
card_name = (TextView)findViewById(R.id.card_name);
type = (TextView)findViewById(R.id.type);
rarity = (TextView)findViewById(R.id.rarity);
artist = (TextView)findViewById(R.id.artist);
//functionality of button "ADD TO COLLECTION"
addCollectionButton = (Button)findViewById(R.id.button_collection);
addCollectionButton.setOnClickListener(new View.OnClickListener() {
//when button clicked, insert into collection database
@Override
public void onClick(View view) {
String nameCard = card_name.getText().toString();
String typeCard = type.getText().toString();
String rarityCard = rarity.getText().toString();
String artistCard = artist.getText().toString();
//switch is used in case we need to add more buttons elsewhere in the app
//switch(view.getId()){
// case R.id.button_collection:
//controller.insert_card("drl","drl", "drl", "drl", "drl");
Toast.makeText(getBaseContext(), "ADDED TO COLLECTION", Toast.LENGTH_LONG).show(); //confirm card was added
//break;
//}
}
});
setContentView(R.layout.activity_main);
//init View
recyclerView = (RecyclerView)findViewById(R.id.recycler_search);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
materialSearchBar = (MaterialSearchBar)findViewById(R.id.search_bar);
//materialSearchBar.inflateMenu(R.layout.activity_main);
//init DB
database = new Database(this);
//setup search bar
//materialSearchBar.setHint("Search");
//materialSearchBar.setCardViewElevation(10);
loadSuggestList();
//This is used when typing in search bar suggesting words like those that have been typed
materialSearchBar.addTextChangeListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
}
// method for when user types in search bar, return suggested strings
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int after) {
List<String> suggest = new ArrayList<>();
for(String search : suggestList) {
if(search.toLowerCase().contains(materialSearchBar.getText().toLowerCase()))
suggest.add(search);
}
materialSearchBar.setLastSuggestions(suggest);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
//This is used when user has typed the word(s) needed to search and hits the search icon
materialSearchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener() {
@Override
public void onSearchStateChanged(boolean enabled) {
if(!enabled)
recyclerView.setAdapter(adapter);
}
@Override
public void onSearchConfirmed(CharSequence text) {
startSearch(text.toString());
}
@Override
public void onButtonClicked(int buttonCode) {
}
});
//init Adapter default set all result
//adapter = new SearchAdapter(this, database.getFriends());
adapter = new SearchAdapter(this, database.getCards());
recyclerView.setAdapter(adapter);
}
private void startSearch(String text) {
//adapter = new SearchAdapter(this, database.getFriendByName(text));
adapter = new SearchAdapter(this, database.getCardsByName(text));
recyclerView.setAdapter(adapter);
}
//for populating suggestion list when search bar has been activated
private void loadSuggestList() {
suggestList = database.getNames();
materialSearchBar.setLastSuggestions(suggestList);
}
}
layout_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="8dp"
android:layout_margin="8dp">
<!--Creation of overall look of searched results
Main design tile for each searched result-->
<LinearLayout
android:orientation="horizontal"
android:layout_margin="8dp"
android:background="@android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--default image for each tile-->
<ImageView
android:src="@drawable/ic_image_black_24dp"
android:layout_width="70dp"
android:layout_height="70dp" />
<!--design tile within the main tile to display the information pulled from database-->
<LinearLayout
android:orientation="vertical"
android:layout_weight="9"
android:layout_width="0dp"
android:layout_height="wrap_content">
<!--information pulled from database design: NAME
android:id="@+id/name"-->
<TextView
android:id="@+id/card_name"
android:layout_marginLeft="10dp"
android:gravity="center_vertical|start"
android:textAllCaps="true"
android:textStyle="bold"
android:text="Eddy Lee"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!--information pulled from database design: EMAIL
android:id="@+id/email"-->
<TextView
android:id="@+id/type"
android:layout_marginLeft="10dp"
android:gravity="center_vertical|start"
android:textStyle="italic"
android:text="eddy@gmail.com"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!--information pulled from database design: PHONE
android:id="@+id/phone"-->
<TextView
android:id="@+id/rarity"
android:layout_marginLeft="10dp"
android:gravity="center_vertical|start"
android:textAllCaps="true"
android:textStyle="bold"
android:text="(123)456-7890"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!--information pulled from database design: ADDRESS
android:id="@+id/address"-->
<TextView
android:id="@+id/artist"
android:layout_marginLeft="10dp"
android:gravity="center_vertical|start"
android:textAllCaps="true"
android:textStyle="normal"
android:text="123 Seseme Street"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button_collection"
android:layout_width="200dp"
android:layout_height="35dp"
android:text="Add to Collection"
/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.darrelbott.search.MainActivity">
<com.mancj.materialsearchbar.MaterialSearchBar
app:mt_speechMode="false"
app:mt_hint="Custom hint"
app:mt_maxSuggestionsCount="10"
app:mt_searchBarColor="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/search_bar" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_search"
android:layout_below="@+id/search_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
DB_Controller:
public class DB_Controller extends SQLiteOpenHelper {
private static final String TABLE_COLLECTION = "collection";
private static final String COLUMN_COLLECTION_ACCOUNT = "account";
private static final String COLUMN_COLLECTION_NAME = "card_name";
private static final String COLUMN_COLLECTION_TYPE = "type";
private static final String COLUMN_COLLECTION_RARITY = "rarity";
private static final String COLUMN_COLLECTION_ARTIST = "artist";
SQLiteDatabase db;
Context context;
int version = 1;
public DB_Controller(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, "collection.db", factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_COLLECTION + " (id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_COLLECTION_ACCOUNT + " TEXT, "
+ COLUMN_COLLECTION_NAME + " TEXT, "
+ COLUMN_COLLECTION_TYPE + " TEXT, "
+ COLUMN_COLLECTION_RARITY + " TEXT, "
+ COLUMN_COLLECTION_ARTIST + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
//sqLiteDatabase.execSQL("DROP TABLE IF EXISTS collection;");
//onCreate(sqLiteDatabase);
}
public void insert_card(String account, String card_name, String type, String rarity, String artist) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_COLLECTION_ACCOUNT,account);
contentValues.put(COLUMN_COLLECTION_NAME,card_name);
contentValues.put(COLUMN_COLLECTION_TYPE,type);
contentValues.put(COLUMN_COLLECTION_RARITY,rarity);
contentValues.put(COLUMN_COLLECTION_ARTIST,artist);
db.insert(TABLE_COLLECTION,null,contentValues);
//this.getWritableDatabase().insertOrThrow("collection.db","",contentValues);
}
}
适配器(包含 SearchAdapter 和 SearchViewHolder):
class SearchViewHolder extends RecyclerView.ViewHolder {
//public TextView name, address, email, phone;
public TextView card_name, type, rarity, artist;
public SearchViewHolder(@NonNull View itemView) {
super(itemView);
card_name = (TextView)itemView.findViewById(R.id.card_name);
type = (TextView)itemView.findViewById(R.id.type);
rarity = (TextView)itemView.findViewById(R.id.rarity);
artist = (TextView)itemView.findViewById(R.id.artist);
}
}
public class SearchAdapter extends RecyclerView.Adapter<SearchViewHolder> {
private Context context;
//private List<Friend> friends;
private List<Cards> cards;
public SearchAdapter(Context context, List<Cards> cards /*List<Friend> friends*/ ) {
this.context = context;
//this.friends = friends;
this.cards = cards;
}
@NonNull
@Override
public SearchViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.layout_item, parent, false);
return new SearchViewHolder(itemView);
}
Button addCollectionButton;
@Override
public void onBindViewHolder(@NonNull SearchViewHolder holder, int position) {
/*
holder.name.setText(friends.get(position).getName());
holder.address.setText(friends.get(position).getAddress());
holder.email.setText(friends.get(position).getEmail());
holder.phone.setText(friends.get(position).getPhone());
*/
holder.card_name.setText(cards.get(position).getName());
holder.type.setText(cards.get(position).getType());
holder.rarity.setText(cards.get(position).getRarity());
holder.artist.setText(cards.get(position).getArtist());
/*
//functionality of button "ADD TO COLLECTION"
final DB_Controller controller = new DB_Controller(this,"collection.db",null,1);
addCollectionButton.setOnClickListener(new View.OnClickListener() {
//when button clicked, insert into collection database
@Override
public void onClick(View view) {
//String card_name1 = card_name.setText(cards.get(position).getName());
//switch is used in case we need to add more buttons elsewhere in the app
//switch(view.getId()){
// case R.id.button_collection:
controller.insert_card("drl","drl", "drl", "drl", "drl");
//Toast.makeText(getApplicationContext(), "ADDED TO COLLECTION", Toast.LENGTH_LONG).show(); //confirm card was added
//break;
//}
}
});
*/
}
@Override
public int getItemCount() {
//return friends.size();
return cards.size();
}
}
最佳答案
您需要在按钮实际所在的适配器内安装 onClickListener。您所做的是让听众参与主要 Activity ,这是不正确的。将您的监听器放在 onBindViewHolder() 中,如果 sql 代码中没有问题,它应该可以工作
关于android - 单击按钮时将 TextView 文本插入 SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53694941/
我在选项卡中有我的网络应用程序的选项。 aaa bbb ccc ddd eee 当用户单击任何选项卡(在同一窗口中)时,我会使用此代码获得淡出效果,然后自动
如何获得一个简单的调用单击来实现如下功能:http://jsfiddle.net/ftwPS/6/我显然错过了一些基本的东西,因为当您单击“CLICK”时这不起作用。 $('tr').click(fu
我有一个列表,在左侧显示一个插入符号图标,单击它时应该更改图标。每当我单击一个图标时,所有图标都会更改。 detailedInfo = []; subordinateInfo; openSu
我有一个 div,我想对其进行设置,以便当我单击其他内容时,它会隐藏该 div。 我也是这样 $('body').click(function(){ if(loginOpened) {
我有很多装有不同内容的盒子。 如果单击第一个框,您可以看到一个包含更多详细信息的弹出框。如果单击第二个、第三个等框,您必须查看这些框的详细信息。 我的问题:如果我点击框号。 2 我看到了盒子号的详细信
当我点击 .add-btn 时,我将 .add-btn 的样式更改为 background:#cccccc 并将 val() 更改为“-”。 现在当我用 tr td .list 删除添加的文本时,它是
感谢 SO 的出色贡献者!当您开始了解 jQuery 时,它会变得更酷。 :) 所以我有一个 LI,单击时会显示/隐藏子 UL。我想做的是能够单击 LI 内的链接,打开一个空白窗口,但也不会关闭子 U
我有这份文档,它使我能够获得一份带有点击进度的多项选择选择调查问卷。 如何用图像代替此处的文字? (并且仍然有这样的机制,一旦单击[图像],就会提出下一个问题) 我已经尝试使用 UL/H1 设置背景图
我想在 JQGrid 的刷新按钮单击上编写代码。有什么事件吗? 最佳答案 如果您需要在开始刷新之前执行一些操作,您应该使用 beforeRefresh打回来: $("#grid_id").jqGrid
问题是将对象或多个参数从模板传递到组件,并使用它们将数据添加到 API。 任务.service.ts addTasks(task: Task): Observable{ let headers =
我有一个像这样的primefaces选项卡 View : This tab has static content. this t
我在 jquery 中有一个有效的 a.click() 函数...但是如果我单击一个 anchor ,我会打开一个新窗口...但是我怎样才能阻止浏览器本身打开一个新窗口? 示例: $('a')
有没有简单的方法来创建代码:如果 URL 更改或单击链接显示 div(例如加载 gif 3 秒),则显示页面?有点像空白的白色页面,加载 gif 旋转 3 秒然后显示页面? 谢谢! 最佳答案 给定 G
我需要知道此时按钮的状态是否被点击? 谢谢 最佳答案 if (myButton.state & UIControlStateHighlighted) { // Do your stuff the
我正在 NSImageView 上绘制一条 NSBezierPath 线。我正在创建 NSBezierPath 对象,设置 moveToPoint,设置 lineToPoint,设置 setLineW
我的 Selenium 代码存在问题,无法正确执行按键 + 单击操作。 测试应打开 jqueryui.com 链接并选择页面上的前 2 个 li 元素。 我正在使用 Selenium 2.23 和 F
单击时我将更改字符串一部分的样式。例如“TEXT”,然后单击“T”,之后它会将样式从黑色更改为红色,仅 T在我的代码中,当我单击文本时,我拆分文本并保留在“split”数组中,它将调用handleCl
我在网站上有一个 anchor 。当有人点击它时,我在 jquery 中执行一些操作并更改名称,但是当再次单击它时,事件被触发,尽管我已经更改了它的名称。代码在这里: $(".like_cont a[
我有一个下载链接Download我希望每次当有人点击“下载”时,我都可以将其插入数据库total_downloads+1。为了插入数据库,我通常使用 然后 if (isset($_POST['d
我是一名优秀的程序员,十分优秀!