我如何使用arrayadapter?

时间:2013-10-24 20:19:14

标签: android android-listview

我是Android编程的初学者,但我对Java有所了解。我需要知道如何在ListView中插入乘法表?我不知道该怎么做。

以下是我的尝试:

ArrayAdapter<String> adaptado = new ArrayAdapter<String>(Tablas_multDiego.this,android.R.layout.simple_list_item_1,tablas(1));
cmb_tabla.setAdapter(adaptado);

public String[] tablas(int tablas) {
   String[] res = new String[10];
   for (int i<=10;i++) {
      res[i]= tablas + "x" + i + "" + tablas * i;
   }
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

快速做到这一点:

res / layout / list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="20sp" />

MainActivity.java

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int yourNumber = 2;
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
                tablas(yourNumber)));

        ListView listView = getListView();
        listView.setTextFilterEnabled(true);

        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // When clicked, show a toast with the TextView text
                Toast.makeText(getApplicationContext(),
                        ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    public String[] tablas(int tablas) {
        String[] res = new String[10];
        for (int i = 1; i <= 10; i++) {
            res[i - 1] = tablas + " x " + i + " = " + tablas * i;
        }

        return res;
    }

}