在点击列表视图项目上启动新活动

时间:2012-06-15 22:43:33

标签: android listview

我需要一些帮助才能在列表视图项中进行简单的单击以打开新的活动。我在这里看到了很多这样的问题,但没有人帮助过我。

public class CustomListView extends ListActivity {

    private EfficientAdapter adap;
    ...

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        adap = new EfficientAdapter(this);
        setListAdapter(adap);


    }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);


        startActivity(new Intent(CustomListView.this, next.class));
    }


    public static class EfficientAdapter extends BaseAdapter implements Filterable {
        private LayoutInflater mInflater;
        private Bitmap mIcon1;
        private Context context;

        public EfficientAdapter(Context context) {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mInflater = LayoutInflater.from(context);
            this.context = context;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {

            ViewHolder holder;


            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.adaptor_content, null);


                convertView.setOnClickListener(new OnClickListener() {
                    private int pos = position;
                    @Override
                    public void onClick(View v) {


                    }
                });



            convertView.setTag(holder);
            }else{
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
            }


            return convertView;
        }

        ...
    }
}

我还尝试在CustomListView类的onCreate方法中添加下一个代码,但它不起作用

ListView lv = getListView();

// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View view,
      int position, long id) {

        startActivity(new Intent(CustomListView.this, next.class));

  }
});

4 个答案:

答案 0 :(得分:3)

onListItemClick()课程中移除CustomListView,并将startActivity()方法放在convertView.setOnClickListener()内。

convertView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(getApplicationContext(), two.class));
    }
});

答案 1 :(得分:1)

试试这段代码。我相信它会帮助你,不要将类设置为像这样的xml文件;

setContentView(R.layout.second);

确保您确实已将该代码放在您的课​​程中,否则您在开启新活动时会收到错误。

package com.exampled.list;

import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity {

    String [] names ={
        "Iphone",
        "Samsung",
        "Nokia",
        "Ericsson",
        "BlackBerry",
        "Benq"
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,names));
    }

    public void onListItemClick(ListView Parent, View v, int position,long id){
        startActivity(new Intent(MainActivity.this, Second.class)); 
        //Toast.makeText(this, "Clicked on : " + names[position], Toast.LENGTH_LONG).show();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.main, menu);
         return true;
    }

}

答案 2 :(得分:0)

尝试将startActivity(new Intent(one.this, two.class)); - 代码放入

convertView.setOnClickListener(new OnClickListener() {
    private int pos = position;
    @Override
    public void onClick(View v) {...}
});

这是什么意思?它应该是Context

答案 3 :(得分:0)

在您的代码one.this中引用Context

由于您的活动名称为CustomListView,您应该写CustomListView.this而不是one.this

您也可以使用getApplicationContext()