0

我是 Android 编程的初学者,但我对 Java 有所了解。我需要知道如何在 a 中插入乘法表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;
   }
}

提前致谢。

4

1 回答 1

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;
    }

}
于 2013-10-24T20:30:53.193 回答