Java 从 customadapter listview Android 中获取所选项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19680964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:21:21  来源:igfitidea点击:

Get selected item from customadapter listview Android

javaandroidlistviewandroid-listview

提问by Dunkey

what I've been doing is about customadapter. Now I want to get the selected item from my listview, but it's adapter is custom. In my listview, I have an image, title and a caption, I want to get the selected item, in my case just the title. How do I do that?

我一直在做的是关于customadapter。现在我想从我的列表视图中获取所选项目,但它的适配器是自定义的。在我的列表视图中,我有一个图像、标题和说明,我想获取所选项目,在我的情况下只是标题。我怎么做?

Here's what I've tried so far:

这是我迄今为止尝试过的:

    @Override
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
    String strHouseName = "house_name";
    String strHousePrice = "house_price";
    //String selectedFromList =(String) (lv.getItemAtPosition(pos));
    Intent i;

    switch(pos){
    case 0:
        i = new Intent(this, ViewHouse.class);
        i.putExtra(strHouseName, "5 Dina Retreat, Carrum Downs");
        i.putExtra(strHousePrice, "0,000.00");
        startActivity(i);
        break;
    case 1:
        i = new Intent(this, ViewHouse.class);
        i.putExtra(strHouseName, "Lot 941 Cob Terrace, Clyde North");
        i.putExtra(strHousePrice, "0,000.00");
        startActivity(i);
        break;
    case 2:
        i = new Intent(this, ViewHouse.class);
        i.putExtra(strHouseName, "Lot 932 Ventasso Street, Clyde");
        i.putExtra(strHousePrice, ",000,000.00");
        startActivity(i);
        break;
    case 3:
        i = new Intent(this, ViewHouse.class);
        i.putExtra(strHouseName, "Lot 515 Summerhill Blvd, Drouin");
        i.putExtra(strHousePrice, "0,000.00");
        startActivity(i);
        break;
    case 4:
        i = new Intent(this, ViewHouse.class);
        i.putExtra(strHouseName, "Lot 17 Todman Street, Drouin (Option 2)"); 
        i.putExtra(strHousePrice, "0,000.00");
        startActivity(i);
        break;
    case 5:
        i = new Intent(this, ViewHouse.class);
        i.putExtra(strHouseName, "Lot 32 Aqueduct Road, Langwarrin");
        i.putExtra(strHousePrice, "0,000.00");
        startActivity(i);
        break;
    case 6:
        i = new Intent(this, ViewHouse.class);
        i.putExtra(strHouseName, "Units 2-7, 269 North Road, Langwarin");
        i.putExtra(strHousePrice, "0,000.00");
        startActivity(i);
        break;
    case 7:
        i = new Intent(this, ViewHouse.class);
        i.putExtra(strHouseName, "U 39, 40-60 Potts Road, Langwarrin");
        i.putExtra(strHousePrice, ",300,000.00");
        startActivity(i);
        break;
    case 8:
        i = new Intent(this, ViewHouse.class);
        i.putExtra(strHouseName, "Lot 1 McClenaghan Place, Pakenham");
        i.putExtra(strHousePrice, ",000,000.00");
        startActivity(i);
        break;
    case 9:
        i = new Intent(this, ViewHouse.class);
        i.putExtra(strHouseName, "Lot 2 McClenaghan Place, Pakenham");
        i.putExtra(strHousePrice, "0,000.00");
        startActivity(i);
        break;
    case 10:
        i = new Intent(this, ViewHouse.class);
        i.putExtra(strHouseName, "Lot 3 McClenaghan Place, Pakenham");
        i.putExtra(strHousePrice, "0,000.00");
        startActivity(i);
        break;
    case 11:
        i = new Intent(this, ViewHouse.class);
        i.putExtra(strHouseName, "Lot 4 McClenaghan Place, Pakenham");
        i.putExtra(strHousePrice, "0,000.00");
        startActivity(i);
        break;
    case 12:
        i = new Intent(this, ViewHouse.class);
        i.putExtra(strHouseName, "Lot 5 McClenaghan Place, Pakenham");
        i.putExtra(strHousePrice, ",200,000.00");
        startActivity(i);
        break;
    case 13:
        i = new Intent(this, ViewHouse.class);
        i.putExtra(strHouseName, "Lot 7 McClenaghan Place, Pakenham");
        i.putExtra(strHousePrice, "0,000.00");
        startActivity(i);
        break;
    case 14:
        i = new Intent(this, ViewHouse.class);
        i.putExtra(strHouseName, "Lot 11 McClenaghan Place, Pakenham");
        i.putExtra(strHousePrice, "0,000.00");
        startActivity(i);
        break;
    case 15:
        i = new Intent(this, ViewHouse.class);
        i.putExtra(strHouseName, "Lot 12 McClenaghan Place, Pakenham");
        i.putExtra(strHousePrice, ",000,000.00");
        startActivity(i);
        break;
    case 16:
        i = new Intent(this, ViewHouse.class);
        i.putExtra(strHouseName, "Lot 532 Summerhill Blvd, Drouin");
        i.putExtra(strHousePrice, "0,000.00");
        startActivity(i);
        break;
    case 17:
        i = new Intent(this, ViewHouse.class);
        i.putExtra(strHouseName, "Lot 17 Ajax Street, Drouin");
        i.putExtra(strHousePrice, "0,000.00");
        startActivity(i);
        break;
    case 18:
        i = new Intent(this, ViewHouse.class);
        i.putExtra(strHouseName, "Lot 133 Mountainview Blvd, Cranbourne North");
        i.putExtra(strHousePrice, "0,000.00");
        startActivity(i);
        break;
    case 19:
        i = new Intent(this, ViewHouse.class);
        i.putExtra(strHouseName, "Lot 531 Summerhill Blvd, Drouin");
        i.putExtra(strHousePrice, "0,000.00");
        startActivity(i);
        break;
    default: Log.d(strHouseName, "No such house name available");
    } 

}


    class MyCustomAdapter extends BaseAdapter
{
    String[] data_text1;
    String[] data_text2;
    int[] data_image;

MyCustomAdapter() {
    data_text1 = null;
    data_text2 = null;
    data_image = null;
}

MyCustomAdapter(int[] image, String[] house, String[] price) {
    data_text1 = house;
    data_text2 = price;
    data_image = image;
}

public int getCount() {
    return data_text1.length;
}

public String getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = getLayoutInflater();
    View row;

    row = inflater.inflate(R.layout.listrow, null);

    TextView textview1 = (TextView) row.findViewById(R.id.text1);
    TextView textview2 = (TextView) row.findViewById(R.id.text2);
    ImageView imageview = (ImageView) row.findViewById(R.id.image);

    imageview.setScaleType(ImageView.ScaleType.FIT_XY);
    textview1.setText(data_text1[position]);
    textview2.setText("$" + (new DecimalFormat("#,###.00")).format(Double.parseDouble(data_text2[position])) );
    imageview.setImageResource(data_image[position]);

    return (row);

    }
}

For the mean time, I just get via its position, but the issue is, what if the list is sorted? How can get the correct selected item? Any ideas? I'd gladly appreciate it. Thanks.

同时,我只是通过它的位置,但问题是,如果列表已排序怎么办?如何获得正确的选定项目?有任何想法吗?我很乐意欣赏它。谢谢。

采纳答案by Hariharan

Try this..

尝试这个..

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

    TextView textview1 = (TextView) v.findViewById(R.id.text1);
    TextView textview2 = (TextView) v.findViewById(R.id.text2);

    Log.v("textview1",textview1.getText().toString().trim());
    Log.v("textview2",textview2.getText().toString().trim());

}

回答by Piyush_Android

Try to get view. Parse it to your textview by id. Get the text and continue

尝试获取视图。通过 id 将其解析为您的 textview。获取文本并继续

回答by Jagadesh Seeram

Try this..

尝试这个..

public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
    // Type cast to what the layout you used in the xml i.e, for the list item
    // For example i took the RelativeLayout
    if(v instanceOf RelativeLayout){
        RelativeLayout layout = (RelativeLayout) v;
        TextView title = (TextView) layout.findViewById(R.id.conFormat_Name);
        System.out.println(title.getText());
    } 

}

回答by umair.ali

You can create a member to hold selected index in your custom adapter like...

您可以创建一个成员来保存自定义适配器中的选定索引,例如...

private int selectedIndex;

Then initialize it to selectedIndex = -1in constructor of custom adapter and its getter and setters...

然后selectedIndex = -1在自定义适配器的构造函数及其 getter 和 setter 中将其初始化...

public void setSelectedIndex(int index) {
    this.selectedIndex = index;
}

public int getSelectedIndex() {
    return selectedIndex;
}

And finally, you just need to pass positionparameter of you adapter onItemClickin the setter and get selected item anywhere you want using getter.

最后,您只需要在 setter 中传递position适配器的参数,onItemClick并使用 getter 在您想要的任何地方获取所选项目。

And once you maintain the selectedIndex, you can easily get selected item like..

并且一旦您维护了 selectedIndex,您就可以轻松获得所选项目,例如..

public BookInfo getSelectedItem() {
    if (selectedIndex <= -1 || selectedIndex >= items.size()) {
        return null;
    } else {
        return items.get(selectedIndex);
    }
}

hope this helps...:)

希望这可以帮助...:)

回答by Ahmad Dwaik 'Warlock'

On item click get the current view, get it's ui elements ex TextView title, and send them to intent

在项目上单击获取当前视图,获取它的 ui 元素 ex TextView 标题,并将它们发送到意图

@Override
public void onItemClick(AdapterView<?> parent, View v, int pos, long id)
{
    if (v== null)
        v= inflater.inflate(R.layout.list_row_layout, null); // reached in case of listview bug in android which is a little chance but to make sure not to crash on NPE    
    String title = ((TextView) v.findViewById(R.id.list_row_title)).getText();
    String price = ((TextView) v.findViewById(R.id.list_row_price)).getText();

    i = new Intent(this, ViewHouse.class);
    i.putExtra(strHouseName, title );
    i.putExtra(strHousePrice, price );
    startActivity(i);
}

回答by Praveen Sharma

Sure for an example

当然是一个例子

//DatasetClass

//数据集类

public class HouseData{

    //private String HouseName= "";
    private String HousePrice = "";

    /*public String getHouseName() {
        return HouseName;
    }
    public void setHouseName(String HouseName) {
        this.HouseName= HouseName;
    }*/
    public String getHousePrice () {
        return HousePrice ;
    }
    public void setHousePrice (String HousePrice ) {
        this.HousePrice = HousePrice ;
    }

//viewholder class is as:-

//viewholder类是这样的:-

 public class ViewHolder
        {
            public TextView tv_HousePrice,tv_HouseName

        }

//and here is the getview method here.

//这里是getview方法。

public View getView(final int position, View convertView,
                ViewGroup parent) {

            final ViewHolder holder;
            //holder=new ViewHolder();

            View listrow = convertView;

            activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

            if (listrow == null) {

                LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                listrow = vi.inflate(R.layout.your_listview_item_layout, null);

                holder=new ViewHolder();
                holder.tv_HouseName=(TextView)listrow.findViewById(R.id.tv_name);


                holder.tv_HousePrice=(EditText)listrow.findViewById(R.id.tv_price);
                listrow.setTag(holder);

            }
            else
                holder=(ViewHolder)listrow.getTag();
            listrow.setBackgroundColor(((position % 2)!=0) ? Color.rgb(229,229,229) : Color.WHITE);

            try {
                houseData= getItem(position);



                if (houseData!= null) { 

                    holder.tv_HouseName.setText(""+houseData.getHouseName());
                    holder.tv_HousePrice.setText(""+houseData.getHousePrice());

                }
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }

            listrow.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
//here you can pass complete data object to next activity
}
            });


            return listrow;
        }