简体   繁体   中英

ListView Item not clickable

im trying to make an activity that searches a DB based on an input string and then returns the results in a list view. All this is working however, My listview items are not clickable. I have tried changing the focus and clickable attributes but nothing has worked so far.

Main XML (add_friend_layout.xml):

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="match_parent" >


 <LinearLayout android:id="@+id/AddFriendHeaderLayout"
        android:layout_marginTop="3dip"
        android:layout_height="45dip"
        android:orientation="horizontal" 
        android:layout_width="fill_parent" 
        android:gravity="center">
        <EditText
            android:id="@+id/et_SearchFriend"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:ems="10"
            android:inputType="text" />

        <Button
            android:id="@+id/bt_SearchFriend"
            android:layout_width="wrap_content"
            android:layout_height="40dip"
            android:focusable="false"
            android:icon="@android:drawable/ic_search_category_default"
            android:text="Search" >

        </Button>   

    </LinearLayout>
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="60dip"  
        android:layout_marginBottom="5dip"
        android:layout_alignParentBottom="true"
        android:layout_above="@+id/AddFriendHeaderLayout"
        android:id="@+id/searchFriendFooterLayout">

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:clickable="true"
            android:layout_height="wrap_content">

        </ListView>
    </LinearLayout>

</RelativeLayout>

ListView XML (single_listview_layout.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="wrap_content"
android:clickable="true" >

<TextView 
    android:id="@+id/tv_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="20sp" >


</TextView>
</LinearLayout>

Main Java Function (SearchFriendActivity.java):

public class SearchFriendActivity extends ListActivity {

//Progress Dialog
private ProgressDialog pDialog;
private Button bt_searchFriend;
private EditText et_Search_String;
private ListView tv_listview;

String[] arrFriends;
String[] arrUserId;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try{
        setContentView(R.layout.add_friend_layout);


        bt_searchFriend= (Button) findViewById(R.id.bt_SearchFriend);
        et_Search_String = (EditText) findViewById(R.id.et_SearchFriend);
        tv_listview = (ListView) findViewById(R.id.tv_list);


        String searchString="";

        //searchString = String.valueOf(et_Search_String.getText());
        Log.i ("log_search","value of searchString: " + searchString);

        bt_searchFriend.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                try{



                    searchFriends aTask = new searchFriends();

                             aTask.execute(String.valueOf(et_Search_String.getText()));
                    String rResult = aTask.get();

                    // Convert aResult to array
                    arrFriends = convertJSONData(rResult);
                    arrUserId = convertJSONDataToUserArray(rResult);


                    setListAdapter(new ArrayAdapter<String>
                                                    (getBaseContext(),       
                                                    R.layout.single_listview_layout, 
                            R.id.tv_list,
                            arrFriends));

                }catch(Exception e){
                    Log.e ("log_search_load",e.toString());
                }
            }
        });


        tv_listview = getListView();


        //handler for List Item click
        tv_listview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
                // When clicked, show a toast with the TextView text
                Toast.makeText(getApplicationContext(),
                        "Friend request sent for " + 
                                            ((TextView) view).getText(), 
                        Toast.LENGTH_SHORT).show();

                Log.i("log_search_listclick", "Listview clicked");
            }
        });

    }catch(Exception e){
        Log.e ("log_search_load2",e.toString());
    }
} 

Firstly, don't use focusable and clickable. When you use setOnclick or another they are auto creating.

Secondly, remove try cache blog then start application. Because when the problem created program will contining and you can't understand where is the problem.

===>Thirdly<==== you are used ( tv_listview= ...) second twice

  1. tv_listview = (ListView) findViewById(R.id.tv_list);
  2. tv_listview = getListView();

then you are trying tv_listview.setOnItemClickListener.... . If second view is not first view seton not working on "R.id.tv_list".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM