如何通过单击按钮获取列表视图中的选定项目

时间:2013-06-23 13:47:21

标签: android listview button

我有一个带有textView的列表视图和每行中的一个按钮,我试图通过单击按钮来获取文本,而不是通过单击整行而是使用adapterView方法:(AdapterView arg0,View v,int position, long arg3)不适用于按钮点击。

public View getView(int position, View convertView, ViewGroup parent) {
        //Set the view for each item in the list view
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.employeeitem, null);
        }
        //Get the Textviews from the row view and set the appropriate values for them
        TextView labelName=(TextView) v.findViewById(R.id.labelName);
        TextView labelAddress=(TextView)v.findViewById(R.id.labelAddress);
        TextView labelImmat=(TextView)v.findViewById(R.id.labelImmat);
        labelName.setText(array[position].marque);
        labelAddress.setText(array[position].categorie);
       labelImmat.setText(array[position].Prix_Achats);
        return v;
    }

这是我通过点击列表视图的行来选择项目的方式但是,我想通过单击按钮而不是整行来选择项目:

listEmployeeDetails.setOnItemClickListener(new OnItemClickListener(){

           public void onItemClick(AdapterView<?> arg0, View v,int position, long arg3)
     {

             TextView tv=(TextView)v.findViewById(R.id.labelName);
             String name=tv.getText().toString();


             Toast.makeText(getApplicationContext(), "Contact Selected "+name, Toast.LENGTH_LONG).show();
     }
    });

6 个答案:

答案 0 :(得分:2)

将onClickListener设置为按钮:

Button button = (Button) getActivity().findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
       // button was clicked
    }

});

答案 1 :(得分:1)

您可以将标签设置为按钮,以便从getView()方法访问该按钮。在标记中,当您以编程方式或通过xml声明项目时,可以存储项目的文本。

public View getView(int position, View convertView, ViewGroup parent) {
    //Set the view for each item in the list view
    View v = convertView;
    //Do your view stuff here
    Button btn = (Button)v.findViewById(R.id.your_button_id);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String str = (String)v.getTag();
            //Do whatever you want with your str here
        }
    });
    return v;
}

答案 2 :(得分:1)

点击按钮

从textview获取文本
    btn.setTag(textView.getText().toString());
btn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub


        String s =(String)v.getTag();


    }
});

答案 3 :(得分:0)

我使用游标适配器做了类似的事情。

在适配器内部定义onClickListener。在此之前,您必须使用getTagsetTag函数发送和检索要在函数内恢复的信息。

myButton.setTag("Value I want to store");

    myButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String value = (String) v.getTag();
            //Whatever you want to do
        }
    });

希望有所帮助:)

答案 4 :(得分:0)

listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Toast.makeText(getApplicationContext(), "Clicked "+parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();

        }

    });

parent.getItemAtPosition(position).toString()

此方法返回该位置的当前所选项目

答案 5 :(得分:-1)

我花了三天时间来解决这个问题,但解决方案非常明显。我的列表使用SQLJSONArray服务器中膨胀,我在getView()方法中所做的只是将TextView传递到按钮的setTag()之后我用jsonOjbect给它充气了。然后在按钮的onClick()方法中,我必须得到标签。看看代码,希望我能帮忙。

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

   ListCell cell;//ListCell is my widgets class holder

    //set up the convertView

    if(convertView==null){

        convertView=inflater.inflate(R.layout.menu_custom_layout, null);
        cell=new ListCell();
        cell.Diet=(TextView)convertView.findViewById(R.id.dietEat); //Diet is the custom list layout textview
        cell.eatButton=(Button)convertView.findViewById(R.id.eatButtton); //eatButton is the button to be clicked
        cell.eatButton.setOnClickListener(this);

    }else{
        cell=(ListCell)convertView.getTag();

    }

    //Change data of each cell
    try {

        JSONObject jsonObject=this.dataArray.getJSONObject(position);
        cell.Diet.setText(jsonObject.getString("food_name"));
        cell.eatButton.setTag(cell.Diet.getText()); //Tag for button's onclick Listener

    } catch (JSONException e) {

        e.printStackTrace();

    }

    return convertView;
}

                @Override
public void onClick(View v) {

String fud=(String) v.getTag(); //Getting the tag and parsing it to string

}