znl_12 2013-04-25 06:09 采纳率: 0%
浏览 2788
已采纳

如何获得列表视图的位置

现在 Listview 的 onListItemClick()方法中有问题,不能获取 list 的位置。这是我使用的代码,请大家帮忙。
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;
    }
}
  • 写回答

2条回答

  • 问答小助手 2013-04-25 10:26
    关注

    从你的代码看来,你忘记创建 list.setOnItemClickListener(this);

    Object obj = parent.getItemAtPosition(position);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Java中消息和缓存如何使用
  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路