2 znl 12 znl_12 于 2013.04.25 14:09 提问

如何获得列表视图的位置

现在 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个回答

CSDN_Ask
CSDN_Ask   2013.04.25 18:26
已采纳

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

Object obj = parent.getItemAtPosition(position);
xiaoyan_12
xiaoyan_12   2013.04.26 10:31

我觉得你忘记设置 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);
            }
        });
Csdn user default icon
上传中...
上传图片
插入图片