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 が表示されているように表示したいと思います。各項目には、写真とタイトルがあります。プログラミングのスキルを始めたときから、これをどのように行うのが最善なのか疑問に思っています。それについて説明している良いチュートリアルを知っていれば教えてください。 イメージリスト ありがとう!

4

1 に答える 1

2

私があなたの質問を理解したら、カスタマイズしたアダプターを作成する必要があります。

文字列と画像を保持する、このような新しい単純なクラスを作成します

    Class ObjectHolder {
      int Image;
      String Title;
    }

この2つのゲッターとセッターを作成します

次に、カスタム 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);
于 2012-09-21T14:42:52.747 に答える