我在SQLite数据库中有一个表,该表有两个字段,一个是“ b”,另一个是“ n”,“ b”代表书号,“ n”代表书名,我已成功获取列表中的所有“ n”字段现在查看我想要当任何用户单击特定列表项时显示书号b在我的数据库中,例如,当有人单击“创世纪”时,我有书n = Genesis,它显示书号,例如b = 1; 这里我的代码是数据库结构
*/
public class Bookname extends Fragment {
private ListView listView;
private ArrayList<String> stringArrayList;
private ArrayAdapter<String> adapter;
private DatabaseHelper mDBHelper;
private SQLiteDatabase mDb;
boolean book;
public Bookname() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_bookname, container, false);
setData();
listView = view.findViewById(R.id.list);
adapter = new ListViewAdapter((MainActivity) getContext(), R.layout.item_listview, stringArrayList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
return view;
}
private void setData() {
stringArrayList = new ArrayList<>();
mDBHelper = new DatabaseHelper(getContext());
mDb = mDBHelper.getReadableDatabase();
Cursor cursor = mDb.rawQuery("SELECT b, n FROM key_english;", new String[]{});
if (cursor.getCount() == 0) {
Toast.makeText(getContext(), "NO DATA FOUND", Toast.LENGTH_SHORT).show();
} else {
while (cursor.moveToNext()) {
stringArrayList.add(cursor.getString(1));
}
}
}
}
Myadopter class
package bible.swordof.God;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.amulyakhare.textdrawable.TextDrawable;
import com.amulyakhare.textdrawable.util.ColorGenerator;
import java.util.List;
public class ListViewAdapter extends ArrayAdapter<String> {
private MainActivity activity;
private List<String> friendList;
public ListViewAdapter(MainActivity context, int resource, List<String> objects) {
super(context, resource, objects);
this.activity = context;
this.friendList = objects;
}
@Override
public int getCount() {
return friendList.size();
}
@Override
public String 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;
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// If holder not exist then locate all view from UI file.
if (convertView == null) {
// inflate UI from XML file
convertView = inflater.inflate(R.layout.item_listview, parent, false);
// get all UI view
holder = new ViewHolder(convertView);
// set tag for holder
convertView.setTag(holder);
} else {
// if holder created, get tag from view
holder = (ViewHolder) convertView.getTag();
}
holder.friendName.setText(getItem(position));
//get first letter of each String item
String firstLetter = String.valueOf(getItem(position).charAt(0));
ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
// generate random color
int color = generator.getColor(getItem(position));
TextDrawable drawable = TextDrawable.builder()
.buildRound(firstLetter, color); // radius in px
holder.imageView.setImageDrawable(drawable);
return convertView;
}
private class ViewHolder {
private ImageView imageView;
private TextView friendName;
public ViewHolder(View v) {
imageView = (ImageView) v.findViewById(R.id.image_view);
friendName = (TextView) v.findViewById(R.id.text);
}
}
}
[My database structure][1]
[1]: https://i.stack.imgur.com/7aXut.png
答案 0 :(得分:0)
创建模型 喜欢:
public class BookData {
private String bookNo;
private String bookName;
BookData(String bookNo, String bookName){
this.bookNo = bookNo;
this.bookName = bookName;
}
public String getBookNo() {
return bookNo;
}
public void setBookNo(String bookNo) {
this.bookNo = bookNo;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
} }
创建ArrayList对象,例如:
ArrayList<BookData> stringArrayList = new ArrayList<>();
将数据存储在模型中
stringArrayList.add(new BookData(cursor.getString(0),cursor.getString(1)));
并使用以下方法检索数据:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String bookNo = stringArrayList.get(position).getBookNo();
Log.e("BookNo : ", bookNo);
}
});