从customlistview

时间:2016-03-20 18:05:54

标签: android

我想在自定义列表视图中点击每个项目的其他活动..我使用了switch case。我没有得到任何错误,但是当我跑步时它不起作用..帮助我...这是我的代码......!

package com.example.chennaihangouts;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
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.Toast;

public class FamilyActivity extends Activity{
Integer itemPosition;

ListView list;
String[] itemname ={
        "AMUSEMENT PARKS",
        "BEACH",
        "CHURCH",
        "MALLS",
        "MOSQUES",
        "PARKS",
        "RESTAURANTS",
        "TEMPLES",
        "THEATRES",
        "OTHERS",

    };

Integer[] imgid={
        R.drawable.themepark,
        R.drawable.beach,
        R.drawable.church,
        R.drawable.malls,
        R.drawable.mosque,
        R.drawable.parks,
        R.drawable.restaurants,
        R.drawable.temple,
        R.drawable.movie,
        R.drawable.others,

};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.family);

    FamilyListAdapter adapter=new FamilyListAdapter(this, itemname, imgid);
    list=(ListView)findViewById(R.id.list1);
    list.setAdapter(adapter);

    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            //String Slecteditem= itemname[+position];
            //Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();

             switch(itemPosition)   
             {

                case 0 :Intent appInfo = new Intent(FamilyActivity.this, ThemeActivity.class);
                    startActivity(appInfo);
                 break;
                case 1 :Intent  i= new Intent(FamilyActivity.this, BeachActivity.class);
                    startActivity(i);
                 break;
                 case 2 :Intent h = new Intent(FamilyActivity.this, ChurchActivity.class);
                    startActivity(h);
                 break;
                 case 3 :Intent g = new Intent(FamilyActivity.this, MallsActivity.class);
                    startActivity(g);
                 break;
                case 4 :Intent f = new Intent(FamilyActivity.this, MosqueActivity.class);
                    startActivity(f);
                 break;
                 case 5 :Intent e = new Intent(FamilyActivity.this, ParkActivity.class);
                    startActivity(e);
                 break;
                 case 6 :Intent d = new Intent(FamilyActivity.this, HotelActivity.class);
                    startActivity(d);
                 break;
                case 7 :Intent c = new Intent(FamilyActivity.this, TempleActivity.class);
                    startActivity(c);
                 break;
                 case 8 :Intent b = new Intent(FamilyActivity.this, MovieActivity.class);
                    startActivity(b);
                 case 9 :Intent a = new Intent(FamilyActivity.this, OtherActivity.class);
                    startActivity(a);
                 break;
        }
        }
    });
}

}

1 个答案:

答案 0 :(得分:1)

您可能需要点击项目的位置。

试试这个。

更改此内容。

 switch(itemPosition)  

对此。

 switch(position) 

希望它会对你有所帮助。