将一个类投射到一个活动

时间:2013-07-31 12:36:36

标签: android listview android-intent android-activity

我正在尝试创建一个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);



        }
    });

}}

2 个答案:

答案 0 :(得分:0)

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

更改此行:

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

要:

Activity Lactivity = element.getActivity();

答案 1 :(得分: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);



    }
});`