在Searchbox.addTextChangedListener(new TextWatcher()中的@Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) 中,这段代码哪错了Cursor cursor=db2.rawQuery("select*from student_course where scname like '%?%'",new String[]{(String) charSequence});
时间: 2024-01-25 14:03:48 浏览: 76
这段代码的问题在于在 SQL 查询中使用了单引号来括住参数占位符 "?",这会导致参数占位符被视为字符串常量,而不是查询中的参数。因此,应该将单引号从参数占位符中移除,同时在查询参数字符串中包含百分号,如下所示:
```
Cursor cursor = db2.rawQuery("SELECT * FROM student_course WHERE scname LIKE ?", new String[]{"%" + charSequence + "%"});
```
这样,查询参数字符串将被正确地拼接到 SQL 查询中,而不会被视为字符串常量。
相关问题
android 好友功能,Android中使用ListView模拟微信好友功能代码
好的,以下是一个简单的Android使用ListView模拟微信好友功能的代码示例:
Friend.java(好友数据模型类):
```java
public class Friend {
private int avatarId;
private String nickname;
private String remark;
public Friend(int avatarId, String nickname, String remark) {
this.avatarId = avatarId;
this.nickname = nickname;
this.remark = remark;
}
public int getAvatarId() {
return avatarId;
}
public void setAvatarId(int avatarId) {
this.avatarId = avatarId;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
```
friend_item.xml(好友列表项布局文件):
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:background="@android:color/white">
<ImageView
android:id="@+id/avatar"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"/>
<TextView
android:id="@+id/nickname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@android:color/black"
android:layout_toRightOf="@+id/avatar"
android:layout_marginLeft="16dp"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/remark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="@android:color/darker_gray"
android:layout_toRightOf="@+id/avatar"
android:layout_below="@+id/nickname"
android:layout_marginLeft="16dp"/>
</RelativeLayout>
```
FriendAdapter.java(好友列表适配器):
```java
public class FriendAdapter extends BaseAdapter implements SectionIndexer {
private List<Friend> friendList;
private Context context;
private LayoutInflater inflater;
public FriendAdapter(List<Friend> friendList, Context context) {
this.friendList = friendList;
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return friendList.size();
}
@Override
public Object getItem(int position) {
return friendList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.friend_item, parent, false);
holder = new ViewHolder();
holder.avatar = convertView.findViewById(R.id.avatar);
holder.nickname = convertView.findViewById(R.id.nickname);
holder.remark = convertView.findViewById(R.id.remark);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Friend friend = friendList.get(position);
holder.avatar.setImageResource(friend.getAvatarId());
holder.nickname.setText(friend.getNickname());
holder.remark.setText(friend.getRemark());
return convertView;
}
@Override
public Object[] getSections() {
return new Object[0];
}
@Override
public int getPositionForSection(int sectionIndex) {
for (int i = 0; i < getCount(); i++) {
String nickname = friendList.get(i).getNickname();
String firstLetter = String.valueOf(nickname.charAt(0)).toUpperCase();
if (firstLetter.charAt(0) == sectionIndex) {
return i;
}
}
return -1;
}
@Override
public int getSectionForPosition(int position) {
String nickname = friendList.get(position).getNickname();
String firstLetter = String.valueOf(nickname.charAt(0)).toUpperCase();
return firstLetter.charAt(0);
}
private static class ViewHolder {
ImageView avatar;
TextView nickname;
TextView remark;
}
}
```
MainActivity.java(主界面代码):
```java
public class MainActivity extends AppCompatActivity implements TextWatcher {
private EditText searchBox;
private ListView friendListView;
private FriendAdapter friendAdapter;
private List<Friend> friendList;
private List<Friend> filteredFriendList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchBox = findViewById(R.id.search_box);
friendListView = findViewById(R.id.friend_list);
friendList = new ArrayList<>();
friendList.add(new Friend(R.drawable.avatar1, "张三", "三哥"));
friendList.add(new Friend(R.drawable.avatar2, "李四", "四哥"));
friendList.add(new Friend(R.drawable.avatar3, "王五", "五哥"));
friendList.add(new Friend(R.drawable.avatar4, "赵六", "六哥"));
friendList.add(new Friend(R.drawable.avatar5, "钱七", "七哥"));
friendList.add(new Friend(R.drawable.avatar6, "孙八", "八哥"));
friendList.add(new Friend(R.drawable.avatar7, "周九", "九哥"));
friendList.add(new Friend(R.drawable.avatar8, "吴十", "十哥"));
friendAdapter = new FriendAdapter(friendList, this);
friendListView.setAdapter(friendAdapter);
searchBox.addTextChangedListener(this);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
filteredFriendList = new ArrayList<>();
for (Friend friend : friendList) {
if (friend.getNickname().contains(s)) {
filteredFriendList.add(friend);
}
}
friendAdapter = new FriendAdapter(filteredFriendList, this);
friendListView.setAdapter(friendAdapter);
}
@Override
public void afterTextChanged(Editable s) {
}
}
```
以上代码仅供参考,具体实现还需要根据需要进行调整。
阅读全文