ListView中的按钮监听器

时间:2015-09-20 11:27:34

标签: java android listview

我使用AsyncTask来完成我的ListView。

public class SIPSettingsFragment extends ListFragment implements View.OnClickListener, AsyncResponse {

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_sipsettings, container, false);

new DownloadJSON().execute();

return rootView;
}

 public class DownloadJSON extends AsyncTask<Void, Void, Void> {

        /*
        some code
        */
        @Override
        protected void onPostExecute(Void result){
            super.onPostExecute(result);

            if (pDialog.isShowing())
                pDialog.dismiss();

            listView = (ListView) getActivity().findViewById(android.R.id.list);

            adapter = new SimpleAdapter(
                    getActivity(),
                    usersList,
                    R.layout.sipuser_list_item,
                    new String[] { TAG_USERNAME, TAG_ADDR, TAG_STATE },
                    new int[] { R.id.username, R.id.addr, R.id.state}
            );
            ((SimpleAdapter) adapter).notifyDataSetChanged();
            setListAdapter(adapter);

            Log.d("lab", "Done");
        }

我的xml

fragment_sipsettings.xml

 <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/userListLayout"
        android:layout_gravity="center_horizontal">
           <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/list" />

    </LinearLayout>

sipuser_list_item.xml

  <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Username: "
                android:layout_alignParentLeft="true"
                android:id="@+id/usernameSIP" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:id="@+id/username"
                android:layout_alignParentTop="true"
                android:layout_toEndOf="@+id/usernameSIP" />

            <Button
                android:id="@+id/deleteSIPUser"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Delete user"
                android:layout_alignParentRight="true"/>


        </RelativeLayout>

带按钮的列表正确显示。如何为sipuser_list_item.xml的按钮实现按钮OnClickListerer?

编辑:

我在SimpleAdapter @Krupal Shah和@cylon

中延伸getView()并覆盖AsyncTask来解决我的问题

2 个答案:

答案 0 :(得分:2)

尝试覆盖SimpleAdapter的getView()方法或编写扩展BaseAdapter类的自己的适配器。

adapter = new SimpleAdapter(...) {
     @Override
     public View getView (int position, View convertView, ViewGroup parent){
          View view = super.getView(position, convertView, parent);
          // get your button here
          Button button = (Button) view.findViewById(R.id.button);
          button.setOnClickListener(....); // set your onclick listener
          return view;
     }
 }

答案 1 :(得分:2)

您可以扩展SimpleAdapter或使用Anonymous内部类,如下所示。在扩展或匿名类中,您必须覆盖getView()方法。在getView方法中,您可以按ID找到按钮并在其上设置单击侦听器。

void Person_list::findAddr()
{
    Person *s;
    string addr;
    string temp_addr;
    s = head;
    cout << "Please enter student's address: ";
    getline(cin, addr);
    while ((s!=NULL) && (s-> Get_addr() != addr))
    {
        s = s -> next;
    }

    if (s != NULL)
    {
        s -> Show();
    }

    if (s == NULL)
    {
        cout << "Cant find. \n" << endl;
    }
}