在android中的listview中的同一行上显示两个按钮

时间:2014-12-10 11:22:29

标签: java android listview

大家好,我还是Android编程的新手,所以我想知道是否可以在listview中使用每个行中的两个按钮显示一个数组数据

array = {one,two,three,four etc}

下面的列表视图

[按钮一个] [按钮两个]

[按钮] [按钮]

示例xml文件我为此创建了

<?xml version="1.0" encoding="utf-8"?> 

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical">

          <TextView android:id="@+id/tv_string_data"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/sample_data"
                    android:textSize="15sp"
                    android:paddingBottom="5dp" />

<LinearLayout android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:orientation="horizontal">

         <Button android:id="@+id/btn_number_data"
                 android:layout_width="160sp"
                 android:layout_height="wrap_content"
                 android:text="@string/button_text"
                 android:paddingBottom="5dp" />

        <Button android:id="@+id/btn_number"
                android:layout_width="160sp"
                android:layout_height="wrap_content"
                android:text="@string/button_text"
                android:paddingBottom="5dp" />

</LinearLayout>

        <TextView android:id="@+id/tv_double_data"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:text="@string/sample_data_2"
                  android:textSize="15sp"
                  android:paddingBottom="5dp" />

</LinearLayout>

我编写的用于实现代码的java代码在

下面
public class CustomAdapter extends BaseAdapter {

private static final String TAG = CustomAdapter.class.getSimpleName();
    ArrayList<DataModel> listArray;

public CustomAdapter() {
        listArray = new ArrayList<DataModel>(5);
        listArray.add(new DataModel("name1", 5, 1.8, "Java"));
        listArray.add(new DataModel("name2", 10, 2.8, "Python"));
        listArray.add(new DataModel("name3", 15, 3.8, "Django"));
        listArray.add(new DataModel("name4", 20, 4.8, "Groovy"));
        listArray.add(new DataModel("name5", 25, 5.8, "Maven"));
    }

@Override
public int getCount() {
    return listArray.size();    // total number of elements in the list

}

@Override
public Object getItem(int i) {
    // TODO Auto-generated method stub
    return listArray.get(i);
}

@Override
public long getItemId(int i) {
    // TODO Auto-generated method stub
    return i;
}

 @Override
    public View getView(int index, View view, final ViewGroup parent) {

if (view == null) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            view = inflater.inflate(R.layout.single_list_item, parent, false);
        }

        final DataModel dataModel = listArray.get(index);

TextView textView = (TextView) view.findViewById(R.id.tv_string_data);
        textView.setText(dataModel.getName());

        Button button = (Button) view.findViewById(R.id.btn_number_data);
        button.setText("" + dataModel.getAnInt());

        Button buttontwo = (Button) view.findViewById(R.id.btn_number);
        buttontwo.setText("" + dataModel.getAnInt());

        textView = (TextView) view.findViewById(R.id.tv_double_data);
        textView.setText("" + dataModel.getaDouble());


button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "string: " + dataModel.getName());
                Log.d(TAG, "int: " + dataModel.getAnInt());
                Log.d(TAG, "double: " + dataModel.getaDouble());
                Log.d(TAG, "otherData: " + dataModel.getOtherData());

                Toast.makeText(parent.getContext(), "button clicked: " + dataModel.getAnInt(), Toast.LENGTH_SHORT).show();
            }
        });

        buttontwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "string: " + dataModel.getName());
                Log.d(TAG, "int: " + dataModel.getAnInt());
                Log.d(TAG, "double: " + dataModel.getaDouble());
                Log.d(TAG, "otherData: " + dataModel.getOtherData());

                Toast.makeText(parent.getContext(), "button clicked: " + dataModel.getAnInt(), Toast.LENGTH_SHORT).show();
            }
        });

view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "string: " + dataModel.getName());
                Log.d(TAG, "int: " + dataModel.getAnInt());
                Log.d(TAG, "double: " + dataModel.getaDouble());
                Log.d(TAG, "otherData: " + dataModel.getOtherData());

                Toast.makeText(parent.getContext(), "view clicked: " + dataModel.getOtherData(), Toast.LENGTH_SHORT).show();
            }
        });

        return view;
    }

}

但是我没有得到预期的结果......结果显示为

[按钮一个] [按钮一个]

[按钮两个] [按钮两个]

而不是

[按钮一个] [按钮两个]

[按钮] [按钮]

非常感谢帮助谢谢

2 个答案:

答案 0 :(得分:0)

您好要创建这种布局,请使用带有两个按钮行XML文件的自定义适配器

答案 1 :(得分:0)

因为您在两个按钮上都设置了相同的文字:更改第二个按钮的文字。

 Button button = (Button) view.findViewById(R.id.btn_number_data);
 button.setText("" + dataModel.getAnInt());

 Button buttontwo = (Button) view.findViewById(R.id.btn_number);
 buttontwo.setText("" + dataModel.getAnInt());