Start a new activity in the click list item

I need help to make a simple click on a listview element to open a new action. I saw many such problems here, but no one helped me.

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;
        }

        ...
    }
}

I also tried adding the following code inside the onCreate method from the CustomListView class, but it does not work either

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));

  }
});
+5
source share
4 answers

Remove onListItemClick()from the class CustomListViewand place the method startActivity()inside convertView.setOnClickListener().

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

Try this code. I am sure that it will help you and does not put the class in an xml file, for example:

setContentView(R.layout.second);

make sure you put this code above in your class, otherwise you will receive an error message when opening a new action.

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;
    }

}
+1
source

startActivity(new Intent(one.this, two.class));

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

? Context

0

The code one.thisis specified Context.

Because the name of your activity CustomListView, you should write CustomListView.thisinstead one.this.

You can also use getApplicationContext().

0
source

All Articles