当按下行上的按钮时,如何获取ListView的行位置

时间:2015-04-30 14:09:13

标签: android listview

我有一个包含一个按钮的3个项目的简单列表。我想得到按钮的行号。我无法让它发挥作用。专家请帮帮忙。

这是我的活动

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class MyListActivity extends ActionBarActivity implements AdapterView.OnItemClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_layout);

        ListView lv = (ListView) findViewById(R.id.listView);
        lv.setOnItemClickListener(this);

        String[] countries = new MyCountries().getCountries();

        ListAdapter la = new MyListAdapter(this, countries);
        lv.setAdapter(la);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.list_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Toast.makeText(this, "Clicked Position " + String.valueOf(position), Toast.LENGTH_SHORT).show();
    }

    public void onClick(View v)
    {
        View myView = v;
    }
}

我的列表行

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true">

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_weight="1" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView"
        android:layout_weight="1" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="ClickMe"
        android:id="@+id/button"
        android:layout_gravity="right"
        android:layout_weight="1"
        android:onClick="onClick" />
</LinearLayout>

国家/地区列表

package com.example.ananth.listviewexample;

import java.util.Locale;

public class MyCountries {

    private String[] countries;

    public String[] getCountries() {
        String [] locales  = Locale.getISOCountries();
        countries = new String[locales.length];
        for (int i = 0; i < locales.length; i ++)  {

            Locale obj = new Locale("", locales[i]);

            countries[i] = obj.getDisplayCountry();

        }        return countries;
    }


    public MyCountries()
    {

    }
}

我的列表适配器

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;


public class MyListAdapter extends ArrayAdapter <String> {
    private final Context context;
    private final String[] values;

    public MyListAdapter(Context context, String[] values) {
        super(context, R.layout.list_row, values);
        this.context = context;
        this.values = values;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_row, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.editText);
        textView.setText(values[position]);

        textView = (TextView) rowView.findViewById(R.id.textView);
        textView.setText(values[position]);

        return rowView;
    }
}

我需要在onItemClick()或onClick()

中获取该位置

提前致谢。

在充气时为每个按钮添加OnClickListener()会有点过分,因为我会有数百行。

2 个答案:

答案 0 :(得分:1)

我修改了onClick()以包含

ListView lv = (ListView) findViewById(R.id.listView);
final int position = lv.getPositionForView((LinearLayout)v.getParent());

得到了这篇文章的提示 - how to get a list item position by clicking the button inside it?

答案 1 :(得分:0)

创建视图时,在按钮上添加按钮单击侦听器

public MyListAdapter(Context context, String[] values) {
    super(context, R.layout.list_row, values);
    this.context = context;
    this.values = values;
}

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.list_row, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.editText);
    textView.setText(values[position]);

    textView = (TextView) rowView.findViewById(R.id.textView);
    textView.setText(values[position]);

    ***button = (Button) rowView.findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener(){})***

    return rowView;
}