2012-09-21 33 views
2

顯示圖形的不是字符串中的圖像列表目前我得到的項目我的數據庫,並將它們添加到字符串名爲結果是我回到並設置爲我的TextView:與標題

protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.level); 
    loadDataBase(); 

    int level = Integer.parseInt(getIntent().getExtras().getString("level")); 

    questions = new ArrayList<Question>(); 
    questions = myDbHelper.getQuestionsLevel(level); 

    tvQuestion = (TextView) findViewById(R.id.tvQuestion); 

    i = 0; 
     String data = getAllItems(); 
     tvQuestion.setText(data); 
} 
private String getAllItems() { 
    result = ""; 

    for (i = 0; i<9; i++){ 
     result = result + questions.get(i).getQuestion() + "\n"; 
    } 

    return result; 
    // TODO Auto-generated method stub 

} 

事情是,所有這些項目在數據庫中都有一個標題(字符串)和圖形縮略圖(字符串)。我想按照下面的圖片展示他們,每個人都有一個onclicklistener,而不是相互之下的無聊列表。每個項目都有一個圖片和標題。 自從我開始編程技巧,我想知道如何做到最好,如果你知道任何好的教程,它解釋得好嗎? imagelist 謝謝!

回答

2

如果我明白你的問題,你需要創建一個自定義的適配器。

創建一個新的簡單的類像這樣持有的字符串和圖片

Class ObjectHolder { 
     int Image; 
     String Title; 
    } 

,併爲這兩

getter和setter然後創建自定義ArrayAdapter

Class CustomArrayAdapter extends ArrayAdapter<ObjectHolder> { 


     public CustomArrayAdapter(Context C, ObjectHolder[] Arr) { 
     super(C, R.layout.caa_xml, Arr); 
     } 

    @Override 
    public View getView(int position, View v, ViewGroup parent) 
    { 
    View mView = v ; 
    if(mView == null){ 
     LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     mView = vi.inflate(R.layout.cpa_xml, null); 
    } 
    TextView text = (TextView) mView.findViewById(R.id.tv_caarow); 
    ImageView image = (ImageView) mView.findViewById(R.id.iv_caarow); 
    if(mView != null) 
    { text.setText(getItem(position).getText()); 
     image.setImageResource(getItem(position).getImage()); 
    return mView; 
    } 
    } 

和在res \ layout中創建caa_xml.xml \

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 
    <ImageView 
     android:id="@+id/iv_caarow" 
     android:src="@drawable/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <TextView 
     android:id="@+id/tv_caarow" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingBottom="15dip" 
     android:layout_BottomOf="@+id/iv_caarow" /> 
    </RelativeLayout> 

並像這樣使用它。

GridView GV= (GridView) findViewById(R.Id.gv); // reference to xml or create in java 
    ObjectHolder[] OHA; 
    // assign your array, any ways! 
    mAdapter CustomArrayAdapter= CustomArrayAdapter(this, OHA); 
    GridView.setAdapter(mAdapter); 
+0

你的意思是cpa_xml.xml是單元格嗎?我是否也需要創建一個獨立的caa_xml(如super中所述)?我該在哪裏申請這個customadapter呢? setListAdapter(new CustomArrayAdapter(this,Arr)); ?我有點困惑對不起.. –

+0

而第二件事情是不清楚的,我怎樣才能確保他們每兩行顯示? –

+0

嗨,傑克。我通過回答編輯。我爲caa_xml.xml添加了一個示例以及如何應用。 – SSKahani