2012-09-21 36 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