如何获得listview的位置?

时间:2011-05-16 08:23:29

标签: android

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

6 个答案:

答案 0 :(得分:2)

我想你忘记了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);
            }
        });

在您的代码中添加此行...

答案 1 :(得分:1)

从您的代码中可以清楚地看出,您忘记提及list.setOnItemClickListener(this);

Object obj = parent.getItemAtPosition(position);

如果你想在onItemClick中获取对象

答案 2 :(得分:0)

在onItemClick函数中,第三个参数是您单击的项目的位置。

答案 3 :(得分:0)

您忘记了setOnItemClickListener()

为了听你的名单上的事件:)

还有一件事,我在你的代码中看不到你得到ListView(findViewById)。

查看我编写的代码以解决问题

希望它有效:)

答案 4 :(得分:0)

在活动按钮中点击以获取所有列表视图项目。

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

            }

        }
    });

答案 5 :(得分:0)

我遇到了同样的问题,这对我有帮助

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