0

我正在尝试创建一个 listView,它使我能够打开我之前创建的 2 个其他活动。我的意图有问题。我很确定它确实理解 Lactivity 是一项已经存在的活动。你能给我解释一下为什么吗?

public ListMenu extends Activity {

private ListView malistView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

    // create list of elements
    List<ElementOfConstruction> liste = new ArrayList<ElementOfConstruction>();

    String Columns = null;
    Class<?> CalculColumns = null;
    ElementOfConstruction columns = new ElementOfConstruction(Columns, 0, R.drawable.columns, CalculColumns, 100);
    liste.add(columns);
    String Beam1 = null;
    Class<?> CalculBeam = null;
    ElementOfConstruction beam1 = new ElementOfConstruction(Beam1, 0, R.drawable.beam1, CalculBeam, 200);

    //Récupération de la listview créée dans le fichier main.xml  
    malistView = (ListView) findViewById(R.id.malistview);

    ArrayAdapter<ElementOfConstruction> adapter = new ArrayAdapter<ElementOfConstruction>(ListMenu.this,
            R.layout.row_list, R.id.name, liste);

    // Affecter l'Adapter à notre ListView
    malistView.setAdapter(adapter);

    malistView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            //On récupère le titre de l'Item dans un ElementOfConstruction


            //start new activity
            //récupération de l’item sélectionné
            ElementOfConstruction element = (ElementOfConstruction)malistView.getAdapter().getItem(position);

            Class<?> Lactivity = element.getActivity();
            int numIdentification = element.getNumIdentification();

            //open a new activity
            Intent intent = new Intent(ListMenu.this, Lactivity.class);
            startActivityForResult(intent,numIdentification);
        }
    });
}}
4

2 回答 2

0

你可以试试这个:

`malistView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
       switch(position)
       {
          case 0:
             intentActivity = Lactivity.class
             break;
          case 1:
             intentActivity = Otheractivity.class
             break;
       }


        //open a new activity
        Intent intent = new Intent(ListMenu.this, Lactivity);
        startActivityForResult(intent,numIdentification);



    }
});`
于 2013-08-26T12:43:40.453 回答
0

您将 Activity 的类传递给 Intent,而不是 Activity 本身。

更改此行:

Class<?> Lactivity = element.getActivity();

至:

Activity Lactivity = element.getActivity();
于 2013-07-31T12:43:24.703 回答