android-如何获取列表视图位置?

android-如何获取列表视图位置?,第1张

概述我在ListviewonListItemClick()方法中遇到问题.我没有得到名单的位置.这是我的代码请帮助我coverletterselection.xml<RelativeLayoutxmlns:android="http://schemas.android.com/apkes/android"android:layout_width="wrap_content"android:layout_hei

我在ListvIEw onListItemClick()方法中遇到问题.我没有得到名单的位置.
这是我的代码请帮助我

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文件

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

解决方法:

从您的代码中显然可以看出,您忘了提到List.setonItemClickListener(this);.

Object obj = parent.getItemAtposition(position);

如果您想在onItemClick中获取对象

总结

以上是内存溢出为你收集整理的android-如何获取列表视图位置?全部内容,希望文章能够帮你解决android-如何获取列表视图位置?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1071315.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存