1

我有一个带有 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();
     }
    });
4

6 回答 6

2

为您的按钮设置一个 onClickListener:

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

button.setOnClickListener(new View.OnClickListener() {

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

});
于 2013-06-23T14:09:23.717 回答
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();


    }
});
于 2014-05-20T06:58:39.040 回答
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;
}
于 2013-07-29T16:45:57.810 回答
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()

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

于 2013-10-29T11:30:19.580 回答
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
        }
    });

希望能帮助到你 :)

于 2013-06-23T14:33:37.330 回答
-1

我花了三天时间解决这个问题,但解决方案非常明显。我的列表是从SQL服务器使用膨胀的,JSONArray我在该getView()方法中要做的只是在我用 jsonOjbect 膨胀它之后将它传递TextView到按钮中。setTag()然后在按钮的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

}
于 2015-03-20T05:42:21.890 回答