Android:自定义列表视图按钮 - 返回错误的位置

时间:2016-02-09 14:08:12

标签: button onclick

附上你可以找到我的问题,如果有人可以帮助我,我会很高兴。我做了几天的研究,发现我的错误。我想要一个带有可点击按钮的自定义列表视图,用于启动活动。活动始终相同,但我需要知道点击了哪个项目的按钮。为了能够看到点击了哪个按钮,我实现了Toa​​st,但Toast返回的唯一内容是0和1.

MainActivity.java

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{

    Spinner java_spinner;
    Spinner java_spinner2;

    String mytext;
    String mytext2;

    TextView choosen_text;
    TextView choosen_text2;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        java_spinner = (Spinner)findViewById(R.id. spinner);
        java_spinner.setOnItemSelectedListener(this);

        java_spinner2 = (Spinner)findViewById(R.id. spinner_number_of_rounds);
        java_spinner2.setOnItemSelectedListener(this);


        ListView lv = (ListView) findViewById(R.id.listView);
        generateListContent();
        lv.setAdapter(new MyListAdaper(this, R.layout.custom_layout, data));

    }

    private ArrayList<String> data = new ArrayList<String>();

    private void generateListContent() {
        for(int i = 0; i < 55; i++) {
            data.add("This is row number " + i);
        }
    }


    private class MyListAdaper extends ArrayAdapter<String> {
        private int layout;
        private List<String> mObjects;
        private MyListAdaper(Context context, int resource, List<String> objects) {
            super(context, resource, objects);
            mObjects = objects;
            layout = resource;
        }


        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder mainViewholder = null;
            if(convertView == null) {
                LayoutInflater inflater = LayoutInflater.from(getContext());
                convertView = inflater.inflate(layout, parent, false);
                ViewHolder viewHolder = new ViewHolder();
                viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.itemImage);
                viewHolder.title = (TextView) convertView.findViewById(R.id.listView);
                viewHolder.button = (Button) convertView.findViewById(R.id.playbutton);
                viewHolder.button.setTag(position);
                viewHolder.button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Toast.makeText(MainActivity.this, "Button of the following item was clicked at: " + position, Toast.LENGTH_LONG).show();

                    }
                });

                convertView.setTag(viewHolder);
            }


            mainViewholder = (ViewHolder) convertView.getTag();
            mainViewholder.title.setText(getItem(position));

            return convertView;
        }
    }
    public class ViewHolder {

        ImageView thumbnail;
        TextView title;
        Button button;
    }



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

        View test1 = java_spinner.getSelectedView();
        choosen_text = (TextView)test1 ;
        mytext = choosen_text.getText().toString();
        //Toast.makeText(this, "you selected" + mytext, Toast.LENGTH_SHORT).show();

        View test2 = java_spinner2.getSelectedView();
        choosen_text2 = (TextView)test2 ;
        mytext2 = choosen_text2.getText().toString();
        //Toast.makeText(this, "you selected" + mytext2, Toast.LENGTH_SHORT).show();


    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }


    @Override
    public void onBackPressed() {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle("Keinen Bock mehr...?");
        alertDialogBuilder
                .setMessage("Ja und tschüss...!")
                .setCancelable(false)
                .setPositiveButton("Ja",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                moveTaskToBack(true);
                                android.os.Process.killProcess(android.os.Process.myPid());
                                System.exit(1);
                            }
                        })

                .setNegativeButton("Nein", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        dialog.cancel();
                    }
                });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();




}}

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main"
    tools:context="com.example.dominic.spinner.MainActivity"
    android:background="#c0b7b7">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_weight="2"
            android:background="#368260">

        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
          android:layout_marginTop="12dp">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <Spinner
                    android:entries="@array/days"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/spinner"
                    android:spinnerMode="dropdown"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_alignRight="@+id/linearLayout"
                    android:layout_alignEnd="@+id/linearLayout" />

                <Spinner
                    android:entries="@array/pools"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/spinner2"
                    android:spinnerMode="dropdown"
                    android:layout_above="@+id/button4"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />

                <Spinner
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/spinner_number_of_rounds"
                    android:entries="@array/Auswahl_der_Rundenanzahl"
                    android:layout_above="@+id/spinner2"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />

                <ListView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:id="@+id/listView" />
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>


</RelativeLayout>

custom_layout.xml

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

    <ImageView
        android:layout_width="125dp"
        android:layout_height="90dp"
        android:id="@+id/itemImage"
        android:src="@mipmap/party"
        android:layout_margin="5dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/listView"
        android:layout_weight="2"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="250dp"
        android:layout_weight="1">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="PLAY"
            android:id="@+id/playbutton" />

    </LinearLayout>

</LinearLayout>

K Thx和Best, 星

0 个答案:

没有答案