2016-04-24 43 views
1

當用戶點擊一個項目時,我需要獲取項目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); 

     } 

    }); 


} 
+0

而不是id,使用位置從db中獲取值:) –

+0

請添加gridview acitivity和自定義適配器的完整代碼 – USKMobility

+0

將'catid'的值傳遞給您的'Intent'是什麼? – pskink

回答

0

參見:https://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html

長ID:被點擊的項目的行ID。

還有:SELECT org_name FROM org_name WHERE category_id='value'無效。代碼中的值是字符串「value」,它不是catid的值。

+0

非常感謝。我編輯了代碼。但仍然無法正常工作。 – venura

+0

你想把catelogy id傳給第二個屏幕嗎? –

+0

您必須確保2件事:1:通過提取catid,2:在第二個屏幕中獲得此catid。之後,閱讀本指南以瞭解如何查詢數據:http://developer.android.com/intl/vi/reference/android/database/sqlite/SQLiteDatabase.html#query(java.lang.String,java.lang .String [],java.lang.String,java.lang.String [],java.lang.String,java.lang.String,java.lang.String,java.lang.String) –