在我的应用程序中有一个微调器,为用户提供选择单元,以便随机显示来自该特定单元的数据。我的微调器看起来像这样 单元1 单元2 。 。 。 单位-25 如果用户从微调器中选择单元-2,则单元-2中有12个单词,因此该单元中的单词应随机显示。 我创建了一个表并存储在数据库中的列是单位,单词 我已在表格中以下列方式存储
1 bank
1 stack
1 over
1 flow
. .
. .
. .
2 google
2 facebook
2 gmail
所以最多25个单位每个单位有12个单词。 现在我应该检索它们并随机显示它们 如果用户单击单元-12应该显示特定单位单词,如何为此编写逻辑。请帮助我解决此问题 提前致谢
答案 0 :(得分:0)
SQLite
部分,如果你不需要它 - 而不是传递一个数组在onActivityCreated
中调用此方法
setDataForCity();
<强> FunctionDefinition 强> ::
private void setDataForCity() {
DatabaseHandler mHelper;
SQLiteDatabase db = null;
Cursor mCursor = null;
ArrayList<LinkedHashMap<String,String>> listCollection=new ArrayList<LinkedHashMap<String,String>>();
LinkedHashMap<String, String> mapCollection;
try {
mHelper = new DatabaseHandler(getActivity());
db = mHelper.getReadableDatabase();
//QUERY:- select city_id,city_name,no_of_buffets,downloaded from city_mas
mCursor = db.rawQuery("select "
+""+city_mas.COLUMN_CITY_ID+","
+""+city_mas.COLUMN_CITY_NAME+","
+""+city_mas.COLUMN_DOWNLOADED+" "
+"from "
+""+city_mas.TABLE_NAME_CITY_MAS+"", null);
if(mCursor.moveToFirst()){
do{
mapCollection=new LinkedHashMap<String,String>();
mapCollection.put(city_mas.COLUMN_CITY_ID, mCursor.getString(mCursor.getColumnIndexOrThrow(city_mas.COLUMN_CITY_ID)));
mapCollection.put(city_mas.COLUMN_CITY_NAME, mCursor.getString(mCursor.getColumnIndexOrThrow(city_mas.COLUMN_CITY_NAME)));
mapCollection.put(city_mas.COLUMN_DOWNLOADED, mCursor.getString(mCursor.getColumnIndexOrThrow(city_mas.COLUMN_DOWNLOADED)));
listCollection.add(mapCollection);
}while(mCursor.moveToNext());
}
AdpSearchCitySpinner adapter=new AdpSearchCitySpinner(getActivity(),listCollection);
spnSearchByCity.setAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(db!=null){
if(db.isOpen()) db.close();
}
if(mCursor!=null){
if(!mCursor.isClosed())mCursor.close();
}
}
}
<强> AdpSearchCitySpinner.java 强>
public class AdpSearchCitySpinner extends BaseAdapter {
Context mContext;
ArrayList<LinkedHashMap<String, String>> city;
public AdpSearchCitySpinner(Context context, ArrayList<LinkedHashMap<String, String>> _listCollection) {
super();
this.mContext = context;
city=_listCollection;
}
@Override
public int getCount() {
return city.size();
}
@Override
public Object getItem(int position) {
return city.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
LinkedHashMap<String, String> mapCollection=city.get(position);
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
View row = inflater.inflate(R.layout.adp_search_city_spinner, parent, false);
TextView city_id = (TextView) row.findViewById(R.id.city_id);
TextView city_name = (TextView) row.findViewById(R.id.city_name);
TextView downloaded_id = (TextView) row.findViewById(R.id.downloaded_id);
city_id.setText(mapCollection.get(city_mas.COLUMN_CITY_ID));
city_name.setText(mapCollection.get(city_mas.COLUMN_CITY_NAME));
downloaded_id.setText(mapCollection.get(city_mas.COLUMN_DOWNLOADED));
return row;
}
}
希望这有帮助!