當用戶點擊一個項目時,我需要獲取項目ID並將其發送到列表視圖活動。但是列表視圖是從預填充的SQLite數據庫填充的,項目應該根據網格視圖的項目標識進行回收。如何解析網格視圖項目ID到SQLite WHERE克勞斯?
例如:如果用戶在第一個活動中單擊項目1(銀行圖標),則列表視圖應該檢索銀行名稱的數據。
這是我的gridview onItem點擊代碼。
grid.setAdapter(new CustomAdapter(this));
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent=new Intent(MainActivity.this,ContactView.class);
intent.putExtra("catid", id);
startActivity(intent);
}
});
}
這是我的代碼來檢索數據庫訪問類的列表項。
public List<String> getQuotes() {
List<String> list = new ArrayList<>();
Long value;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
value = bundle.getLong("catid");
Cursor cursor = database.rawQuery("SELECT org_name FROM org_name WHERE category_id='value'", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
list.add(cursor.getString(0));
cursor.moveToNext();
cursor.close();
}
}
return list;
}
Custom Grid View Adapter的完整代碼。在MainActivity類別的GridView活動的
public class CustomAdapter extends BaseAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
class ViewHolder{
ImageView myCategories;
ViewHolder(View v){
myCategories = (ImageView)v.findViewById(R.id.myCat);
}
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View row =view;
ViewHolder holder=null;
if(row==null){
LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single_item, viewGroup, false);
holder=new ViewHolder(row);
row.setTag(holder);
}
else{
holder=(ViewHolder)row.getTag();
}
Categories temp= list.get(i);
holder.myCategories.setImageResource(temp.imgId);
return row; }
class Categories{
int imgId;
String categoryName;
Categories(int imgId,String categoryName){
this.imgId=imgId;
this.categoryName=categoryName;
}
}
Context context;
ArrayList<Categories> list;
CustomAdapter(Context context){
this.context=context;
list=new ArrayList<Categories>();
Resources res=context.getResources();
String[] tempCategoryNames= res.getStringArray(R.array.category_array);
int [] categoryImages={R.drawable.bank,R.drawable.education,R.drawable.fashion,R.drawable.hospitals,R.drawable.hotels,R.drawable.restaurant,R.drawable.retail,R.drawable.transport};
for(int i=0;i<8;i++){
Categories tempHello = new Categories(categoryImages[i],tempCategoryNames[i]);
list.add(tempHello);
}
// res.getStringArray(R.array.categories);
}
}
Fullcode
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
grid = (GridView) findViewById(R.id.grid);
toolbar=(Toolbar)findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
// CustomGrid adapter = new CustomGrid(MainActivity.this, categories, imageId);
grid.setAdapter(new CustomAdapter(this));
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent=new Intent(MainActivity.this,ContactView.class);
intent.putExtra("catid", id);
startActivity(intent);
}
});
}
而不是id,使用位置從db中獲取值:) –
請添加gridview acitivity和自定義適配器的完整代碼 – USKMobility
將'catid'的值傳遞給您的'Intent'是什麼? – pskink