开发者

How to get listview position?

开发者 https://www.devze.com 2023-03-06 16:30 出处:网络
I have problem in Listview onListItemClick() m开发者_Go百科ethod. I am not getting the position of the list.

I have problem in Listview onListItemClick() m开发者_Go百科ethod. 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;
    }
}


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...


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


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


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


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

            }

        }
    });


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();
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消