Monali
Monali

Reputation: 1966

How to get listview position?

I have problem in Listview onListItemClick() method. I am not getting the position of the list. Here is my code Pls help me

coverletterselection.xml

    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"> 
    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/bgg"     />
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:orientation="vertical"          
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="fill_parent"              
            android:background="@drawable/blackstrip"
            android:layout_height="wrap_content" />             
        <ImageView
            android:layout_width="fill_parent"  
            android:id="@+id/coverletterbgView"
            android:background="@drawable/imgselectcoverltrresume"
            android:layout_height="wrap_content" />
        <ListView 
            android:id="@+id/list" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"    />          
    </LinearLayout>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"      
        android:layout_height="wrap_content">
        <ImageButton
            android:layout_width="wrap_content"     
            android:layout_marginLeft="5sp"     
            android:background="@drawable/submission"
            android:layout_height="wrap_content" /> 
        <ImageButton
            android:layout_width="wrap_content"             
            android:background="@drawable/iconaddplus"
            android:layout_height="wrap_content" 
            android:layout_alignParentRight="true" 
            android:layout_marginRight="10sp"/>         
    </RelativeLayout>
</RelativeLayout>

Java File

 public class MainActivity extends ListActivity implements OnItemClickListener
{       
    int ht,wt;
ListView list;
    DataHelperCoverLetter dbcl;
    ArrayList<String> coverLetterTitle = new ArrayList<String>();

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

        dbcl = new DataHelperCoverLetter(this);
        DisplayMetrics displaymetrics = new DisplayMetrics();       
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);      
        ht = displaymetrics.heightPixels;      
        wt = displaymetrics.widthPixels;
        bg = (ImageView)findViewById(R.id.coverletterbgView);
        bg.setLayoutParams(new LinearLayout.LayoutParams(wt, ht/3));    

        coverLetterTitle = dbcl.fetchCoverLetter();

            //get the listView from your layout and define the listener on it
            list = (ListView)findViewById(R.id.list);
            list.setOnItemClickListener(this);

        addCoverLetterBtn = (ImageButton)findViewById(R.id.addCoverLetter);
        addCoverLetterBtn.setOnClickListener(this);
        setListAdapter(new CoverLetterAdaptor(this,ht,wt,coverLetterTitle));        
    }
    @Override
    public void onListItemClick(ListView parent, View view, int position, long id)
    {     
        Log.i("OnClick", "position = "+position);

    }   
}


public class CoverLetterAdaptor extends BaseAdapter
{
    private LayoutInflater mInflater;   
    ArrayList<String> coverLetterItems;
    Context context;    
    int ht,wt;  

    public CoverLetterAdaptor(Context context,int ht,int wt,ArrayList<String> coverLetterItems)
    {
        this.context = context;
        this.coverLetterItems = new ArrayList<String>();
        this.coverLetterItems = coverLetterItems ;          
        this.ht = ht;
        this.wt = wt;
        mInflater = LayoutInflater.from(context);   
    }

    public int getCount() {
        return coverLetterItems.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }


    public View getView(int position, View convertView, ViewGroup parent)
    {       
        ViewHolder holder;

        convertView = mInflater.inflate(R.layout.coverletteradaptor, null);     
        holder = new ViewHolder();

        holder.coverLetterTxt = (TextView) convertView.findViewById(R.id.coverLetterAdaptorTxt);
        holder.bgImageCCAdaptor = (ImageView)convertView.findViewById(R.id.bgimageCoverLetter);
        holder.bgImageCCAdaptor.setLayoutParams(new RelativeLayout.LayoutParams(wt, ht/7));     

        convertView.setTag(holder);
        holder = (ViewHolder) convertView.getTag();
        holder.coverLetterTxt.setText(coverLetterItems.get(position));                          
        return convertView;
    }

    static class ViewHolder 
    {
        TextView coverLetterTxt;        
        ImageView bgImageCCAdaptor;
    }
}

Upvotes: 3

Views: 15097

Answers (6)

Vika
Vika

Reputation: 1

I had the same problems, and this helped me

protected void populateView(View v, String s, int position) {
    ListUserNames.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TextView textView = (TextView) view.findViewById(R.id.discr_for_task);  
            String username = ListUserNames.getItemAtPosition(position).toString().trim();
        }
    }
}

Upvotes: 0

arjun
arjun

Reputation: 1

In a Activity Button onclick to get all listview item.

Listview lv = (ListView) findViewById(R.id.previewlist);

    final BaseAdapter adapter = new PreviewAdapter(this, name, age);

    confirm.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            View view = null;

            String value;
            for (int i = 0; i < adapter.getCount(); i++) {

                view = adapter.getView(i, view, lv);

                Textview et = (TextView) view.findViewById(R.id.passfare);

                value=et.getText().toString();

                 Toast.makeText(getApplicationContext(), value,Toast.LENGTH_SHORT).show();

            }

        }
    });

Upvotes: 0

Houcine
Houcine

Reputation: 24181

you forgot the setOnItemClickListener()

in order to listen the event on your List :)

and one more thing , i can't see in your code where you get your ListView ( findViewById ).

Look your code i've edited it to fixe the problem

Hope it works :)

Upvotes: 0

Zoombie
Zoombie

Reputation: 3610

Clearly seems from your code, u forgot to mention list.setOnItemClickListener(this);

Object obj = parent.getItemAtPosition(position);

in case, you want to get object in onItemClick

Upvotes: 1

zhen01.wang
zhen01.wang

Reputation: 98

in the onItemClick function,the third parameter is the position of item which you click.

Upvotes: 0

Kandha
Kandha

Reputation: 3689

i think You forget to setOnItemClickListener

ListView lst = (ListView) findViewById(R.id.list);
        lst.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {
                // TODO Auto-generated method stub
                Log.i("OnClick", "position = " + position);
            }
        });

add this line in your code...

Upvotes: 2

Related Questions