0

我已经使用 baseadapter 创建了列表视图。当点击不同的项目但无法获得项目价值时,我想去不同的活动。所以任何人都可以帮助我如何做到这一点。例如点击项目 xi 想要活动 1 点击项目说 z,我想去 avctivity2 谢谢。

这是代码:

    public class homeScreen extends Activity{
ListView list1; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homescreen);

        list1=(ListView)findViewById(R.id.spacelist);
        list1.setAdapter(new MySimpleAdapter(this));
        list1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position,long id) {
                // TODO Auto-generated method stub      
                Intent i=new Intent(homeScreen.this, privateSpaceList.class);
                startActivity(i);
            }
        });
    }
}

class SingleRow{

    String title;
    int image;
    public SingleRow(String title,int image) {
        // TODO Auto-generated constructor stub
        this.title=title;
        this.image=image;
    }
}
 class MySimpleAdapter extends BaseAdapter{

    ArrayList<SingleRow> list;
    Context context;
    public MySimpleAdapter(Context c) {
        // TODO Auto-generated constructor stub
        context=c;
        list=new ArrayList<SingleRow>();
        //putting actual values in array
        Resources res=c.getResources();
        String[] titles=res.getStringArray(R.array.titles);
        int[] images={R.drawable.error,R.drawable.ic_launcher,R.drawable.ic_launcher};

        //putting single row in arraylist
        for(int i=0;i<3;i++)
        {
            list.add(new SingleRow(titles[i], images[i]));
        }
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }
    @Override
    public Object getItem(int i) {
        // TODO Auto-generated method stub
        return list.get(i);
    }
    @Override
    public long getItemId(int i) {
        // TODO Auto-generated method stub
        return i;
    }

    //called when want to display row
    @Override
    public View getView(int i, View view, ViewGroup viewgroup) {
        // TODO Auto-generated method stub
        LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row=inflater.inflate(R.layout.single_row,viewgroup,false);
        TextView title=(TextView)row.findViewById(R.id.label);
        ImageView image=(ImageView)row.findViewById(R.id.imageView);
        SingleRow temp=list.get(i);
        title.setText(temp.title);
        image.setImageResource(temp.image);
        return row;
    }
}
4

4 回答 4

1

我认为在 Adapter 类的getView上实现ClickListener的简单方法。或者,在您的情况下,您需要 store List<..>,还需要将此列表添加到适配器,此时:

@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,long id) {   
  Intent intent = new Intent(homeScreen.this, privateSpaceList.class);
  yourList.get(position) // your data
  startActivity(intent);
}

你得到你的对象。

或像这样:

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

    switch(position) {

      case 1:
          Intent intent = new Intent(homeScreen.this, privateSpaceList.class);
          startActivity(intent);
          break;

      }

    }
于 2013-11-01T07:44:48.983 回答
0

在您的项目上单击将您的代码更改为

list1.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int position,long id) {
        // TODO Auto-generated method stub  
       String[] titles=res.getStringArray(R.array.titles); 
         if((titles[position]).equalsIgnoreCase("Itemx"))
         {
              Intent i=new Intent(homeScreen.this, privateSpaceList.class);
              startActivity(i);
         }
         else if((titles[position]).equalsIgnoreCase.equals("Itemy"))
         {
              Intent i=new Intent(homeScreen.this, activity2.class);
              startActivity(i);
         }
         else if((titles[position]).equalsIgnoreCase.equals("Itemz"))
         {
              Intent i=new Intent(homeScreen.this, activity3.class);
              startActivity(i);
         }
    }
});
于 2013-11-01T07:56:04.917 回答
0

像这样试试

这是一种非常简单的方法,无需太多条件。

定义一个包含所有 Activity 类名称的数组。但是所有活动都应该在同一个包中。

String[] Classtitles=new String[]{"Activity1","Activity2","Activity3"};

获取包名

final String packageName = this.getClass().getPackage().getName();

并在 onItemClick() 内部进行更改

list1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position,long id) {
               try {
                        Class c = Class.forName(packageName + "." + Classtitles[position]);
                        startActivity(new Intent(homeScreen.this, c));
                    } catch (ClassNotFoundException e) {
                        Toast.makeText(homeScreen.this, String.valueOf(e), 5000).show();
                    }
            }
        });

希望这会帮助你。

于 2013-11-01T08:07:34.407 回答
0

尝试这个..

list1.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int position,long id) {
            // TODO Auto-generated method stub  
            TextView title=(TextView)v.findViewById(R.id.label);    
             if(title.getText().toString().trim().equals("Item x"))
             {
                  Intent i=new Intent(homeScreen.this, activity1.class);
                  startActivity(i);
             }
             else if(title.getText().toString().trim().equals("Item y"))
             {
                  Intent i=new Intent(homeScreen.this, activity2.class);
                  startActivity(i);
             }
             else if(title.getText().toString().trim().equals("Item z"))
             {
                  Intent i=new Intent(homeScreen.this, activity3.class);
                  startActivity(i);
             }
        }
    });
于 2013-11-01T07:48:33.577 回答