列表视图行没有被点击?

时间:2017-11-22 01:45:00

标签: android listview

我正在创建一个包含项目列表的ListView,并尝试在单击ListView的每一行时打印一些吐司,但是当我点击ListView的标题部分但是在点击行的时候不会出现toast ListView。可能是什么问题。我无法理解为什么它没有识别ListView的行上的点击。

请帮帮我。 Thanx提前!!!

这是我的代码

主要Activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ankit.surya1.MainActivity">

    <HorizontalScrollView
        android:layout_marginTop="5dp"
        android:layout_width="1500dp"
        android:layout_height="match_parent">

        <ListView

            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp" />
    </HorizontalScrollView>




</RelativeLayout>

主要Activity.java

public class MainActivity extends AppCompatActivity {

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



        ListView listView = (ListView) findViewById(R.id.listview);
        ViewGroup headerView = (ViewGroup) getLayoutInflater().inflate(R.layout.header, listView, false);

        listView.addHeaderView(headerView);

        String[] items = getResources().getStringArray(R.array.list_items);

        LstViewAdapter adapter = new LstViewAdapter(MainActivity.this, R.layout.rowlayout, items);


        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/*
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Choose");
                builder.setIcon(R.drawable.checked);
                final CharSequence[] items = {"Call", "Send SMS"};
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        // t12.setText(items[item]);
                        if (items[item] == "call") {
// write the calll code here......................................

                        }
                        if (items[item] == "Send SMS") {
//write the sms code here..........................................


                        }
                    }
                });
                AlertDialog alert = builder.create();
                alert.show();*/
                switch(position)
                {
                    case 0:   {
                        Toast.makeText(getApplicationContext(), "hi ki", Toast.LENGTH_SHORT).show();
                    }
                    break;
                    case 1:    {
                        Toast.makeText(getApplicationContext(), "hi ki", Toast.LENGTH_SHORT).show();
                    }
                    break;
                    case 2:   {

                        Toast.makeText(getApplicationContext(), "hi ki", Toast.LENGTH_SHORT).show();
                    }
                    break;
                }


            }
        });

    }
}

header.xml

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

    <LinearLayout
        android:layout_width="1200dp"
        android:layout_height="wrap_content"

        android:background="#9E9E9E"
        >


        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:background="@drawable/table1"
            android:gravity="left"
            android:padding="10sp"
            android:text="Type"
            android:textColor="@color/white"
            android:textStyle="bold"></TextView>


        <TextView
            android:layout_width="100dp"
            android:layout_column="1"
            android:layout_height="wrap_content"
            android:padding="10sp"
            android:gravity="left"
            android:text="Loan #"
            android:textColor="@color/white"
            android:textStyle="bold"
            android:background="@drawable/table1"
            >
        </TextView>

        <TextView
            android:layout_width="100dp"
            android:layout_column="2"
            android:padding="10sp"
            android:gravity="left"
            android:textColor="@color/white"
            android:text="Name"
            android:textStyle="bold"
            android:background="@drawable/table1"
            android:layout_height="wrap_content"
            >
        </TextView>

        <TextView
            android:layout_width="100dp"
            android:layout_column="2"
            android:background="@drawable/table1"
            android:gravity="left"
            android:padding="10sp"
            android:text="Ph No"
            android:textColor="@color/white"
            android:textStyle="bold"
            android:layout_height="wrap_content"
            ></TextView>


        <TextView
            android:layout_height="wrap_content"
            android:layout_width="100dp"
            android:layout_column="4"
            android:padding="10sp"
            android:gravity="left"
            android:text="Address"
            android:textColor="@color/white"
            android:textStyle="bold"
            android:background="@drawable/table1"
            >
        </TextView>

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="100dp"
            android:layout_column="5"
            android:padding="10sp"
            android:gravity="left"
            android:textColor="@color/white"
            android:text="Area"
            android:textStyle="bold"
            android:background="@drawable/table1"
            >
        </TextView>

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="100dp"
            android:layout_column="6"
            android:padding="10sp"
            android:gravity="left"
            android:textColor="@color/white"
            android:text="Amount"
            android:textStyle="bold"
            android:background="@drawable/table1"
            >
        </TextView>

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="100dp"
            android:layout_column="7"
            android:padding="10sp"
            android:gravity="left"
            android:textColor="@color/white"
            android:text="ROI"
            android:textStyle="bold"
            android:background="@drawable/table1"
            >
        </TextView>

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="100dp"
            android:layout_column="8"
            android:padding="10sp"
            android:textColor="@color/white"
            android:gravity="left"
            android:text="Interest"
            android:textStyle="bold"
            android:background="@drawable/table1"
            >
        </TextView>

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="100dp"
            android:layout_column="9"
            android:padding="10sp"
            android:gravity="left"
            android:textColor="@color/white"
            android:text="Total"
            android:textStyle="bold"
            android:background="@drawable/table1"
            >
        </TextView>

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="100dp"
            android:layout_column="10"
            android:padding="10sp"
            android:gravity="left"
            android:text="DaysLeft"
            android:textColor="@color/white"
            android:textStyle="bold"
            android:background="@drawable/table1"
            >
        </TextView>

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="100dp"
            android:layout_column="11"
            android:padding="10sp"
            android:gravity="left"
            android:text="SMS Count"
            android:textColor="@color/white"
            android:textStyle="bold"
            android:background="@drawable/table1"
            >
        </TextView>

    </LinearLayout>
</LinearLayout>

rowlayout.xml

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

    <LinearLayout
        android:layout_width="1200dp"
        android:layout_height="wrap_content"

        android:background="#9E9E9E"
        >




        <TextView
            android:id="@+id/t1"
            android:layout_width="100dp"
            android:layout_column="0"
            android:layout_height="match_parent"
            android:gravity="left"
            android:padding="10sp"
            android:textColor="#000000"
            android:inputType="text"

            >


        </TextView>

        <TextView
            android:id="@+id/t2"
            android:layout_width="100dp"
            android:layout_column="1"
            android:layout_height="match_parent"
            android:padding="10sp"
            android:gravity="left"
            android:inputType="number"
            android:textColor="#000000"


            >
        </TextView>

        <TextView
            android:id="@+id/t3"
            android:layout_height="match_parent"
            android:layout_column="2"
            android:padding="10sp"
            android:gravity="left"
            android:textColor="#000000"
            android:inputType="textPersonName"
            android:layout_width="100dp"


            >
        </TextView>

        <TextView
            android:layout_height="match_parent"
            android:id="@+id/t4"
            android:layout_width="100dp"
            android:layout_column="3"
            android:padding="10sp"
            android:gravity="left"
            android:textColor="#000000"
            android:inputType="phone"
            >
        </TextView>


        <TextView
            android:layout_height="match_parent"
            android:id="@+id/t5"
            android:layout_width="100dp"
            android:layout_column="4"
            android:padding="10sp"
            android:gravity="left"
            android:textColor="#000000"
            android:inputType="text"

            >
        </TextView>

        <TextView
            android:layout_height="match_parent"
            android:id="@+id/t6"
            android:layout_width="100dp"
            android:layout_column="5"
            android:padding="10sp"
            android:gravity="left"
            android:textColor="#000000"
            android:inputType="text"

            >
        </TextView>

        <TextView
            android:layout_height="match_parent"
            android:id="@+id/t7"
            android:layout_width="100dp"
            android:layout_column="6"
            android:padding="10sp"
            android:gravity="left"
            android:textColor="#000000"
            android:inputType="number"
            >
        </TextView>

        <TextView
            android:layout_height="match_parent"
            android:id="@+id/t8"
            android:layout_width="100dp"
            android:layout_column="7"
            android:padding="10sp"
            android:gravity="left"
            android:textColor="#000000"
            android:inputType="number"


            >
        </TextView>

        <TextView
            android:layout_height="match_parent"
            android:id="@+id/t9"
            android:layout_width="100dp"
            android:layout_column="8"
            android:padding="10sp"
            android:textColor="#000000"
            android:gravity="left"
            android:inputType="number"



            >
        </TextView>

        <TextView
            android:layout_height="match_parent"
            android:id="@+id/t10"
            android:layout_width="100dp"
            android:layout_column="9"
            android:padding="10sp"
            android:gravity="left"
            android:textColor="#000000"
            android:inputType="number"

            >
        </TextView>

        <TextView
            android:layout_height="match_parent"
            android:id="@+id/t11"
            android:layout_width="100dp"
            android:layout_column="10"
            android:padding="10sp"
            android:gravity="left"
            android:textColor="#000000"
            android:inputType="number"


            >
        </TextView>

        <TextView
            android:layout_height="match_parent"
            android:id="@+id/t12"
            android:layout_width="100dp"
            android:layout_column="11"
            android:padding="10sp"
            android:gravity="left"
            android:textColor="#000000"
            android:inputType="number"

            >
        </TextView>

    </LinearLayout>
</LinearLayout>

的strings.xml

<string-array name="list_items">

        <item>Gold__4__Jahn Ibrahim__9556551456__LB Nagar__Hyd__95220__6__2920__22920__-26__0__</item>
        <item>Gold__3__Amit Kumar__9556325566__KpHB __Hyd__20000__3__2920__22580__-4__0__</item>
        <item>Gold__7__Mohan__9556458745__Hitecy__Hyd__95226__3__2920__23570__-5__0__</item>
        <item>Gold__8__Ravi__9556551456__LB Nagar__Hyd__95220__6__2920__278750__-26__0__</item>
        <item>Gold__3__Sunil__9556325566__KpHB __Hyd__20000__3__2920__27540__-4__0__</item>
        <item>Gold__9__Amit__9556458745__Hitecy__Hyd__95226__3__2920__27780__-5__0__</item>
        <item>Gold__5__Mohan__9556551456__LB Nagar__Hyd__95220__6__2920__278740__-26__0__</item>
        <item>Gold__3__Amit__9556325566__KpHB __Hyd__20000__3__2920__22920__-4__0__</item>
        <item>Gold__7__Jain__9556458745__Hitecy__Hyd__95226__3__2920__22920__-5__0__</item>
    </string-array>

0 个答案:

没有答案