Android 在SQLite的ListFragment中执行多个具有不同操作的可单击项

Android 在SQLite的ListFragment中执行多个具有不同操作的可单击项,android,sqlite,android-listfragment,Android,Sqlite,Android Listfragment,我是初学者。 我有一个ListFragment,其中列表中的每个元素都包含三个TextView和两个不同的按钮。从SQLite数据库读取数据。大概是这样的: ListFragment -------------------- [Person Name] [Person Phone] [Person e-mail] [Button 1][Button 2] -------------------- [Person Name] [Person Phone] [Person e-mail] [Butt

我是初学者。 我有一个ListFragment,其中列表中的每个元素都包含三个TextView和两个不同的按钮。从SQLite数据库读取数据。大概是这样的:

ListFragment
--------------------
[Person Name]
[Person Phone]
[Person e-mail]
[Button 1][Button 2]
--------------------
[Person Name]
[Person Phone]
[Person e-mail]
[Button 1][Button 2]
--------------------
... (and so on) ...
@SuppressWarnings("deprecation")
public class Fragment01person extends ListFragment {


    SQLiteDatabase dataBase = null;
    Cursor crs;
    SimpleCursorAdapter dataAdapter;
    ListView listView;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment01person, container,false);


    dataBase = getActivity().openOrCreateDatabase("DBperson.db", 
            android.content.Context.MODE_PRIVATE, null);

    crs = dataBase.rawQuery("SELECT * FROM person", null);


         String[] columns = new String[] {
            "person_name",
            "person_phone",
            "person_email"
          };

          // the XML defined views which the data will be bound to
          int[] to = new int[] { 
            R.id.text01person,
            R.id.text02person,
            R.id.text03person
          };

          // create the adapter using the cursor pointing to the desired data 
          //as well as the layout information
          dataAdapter = new SimpleCursorAdapter(
                  getActivity(), R.layout.fragment01itemlist, 
            crs, 
            columns, 
            to);

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

          View v = new View(getActivity());
          listView.addHeaderView(v);
          listView.addFooterView(v);

          // Assign adapter to ListView
          listView.setAdapter(dataAdapter);  

    rootView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT ));       
    return rootView;

    }

    public void onListItemClick(ListView l, View v, int position, long id) 
    {
        super.onListItemClick(l, v, position, id); 

        Cursor cursorLocal = (Cursor) l.getItemAtPosition(position);

        String nameperson = cursorLocal.getString(cursorLocal.getColumnIndex("person_name"));
        String endperson = cursorLocal.getString(cursorLocal.getColumnIndex("person_endereco"));
        String phoneperson = cursorLocal.getString(cursorLocal.getColumnIndex("person_phone"));
        String emailperson = cursorLocal.getString(cursorLocal.getColumnIndex("person_email"));

        showMessage("test",nameperson+" "+endperson+" "+phoneperson+" "+emailperson);


    }




    public void showMessage (String title, String text){
        AlertDialog.Builder message = new AlertDialog.Builder(getActivity());
        message.setTitle(title);
        message.setMessage(text);
        message.setNeutralButton("Ok", null);
        message.show();
    }

}
其中按钮1将拨打电话,按钮2将向此人发送电子邮件。 详细信息,这些按钮是可单击的图像视图

数据库已预填充

我将我的代码如下所示:

ListFragment
--------------------
[Person Name]
[Person Phone]
[Person e-mail]
[Button 1][Button 2]
--------------------
[Person Name]
[Person Phone]
[Person e-mail]
[Button 1][Button 2]
--------------------
... (and so on) ...
@SuppressWarnings("deprecation")
public class Fragment01person extends ListFragment {


    SQLiteDatabase dataBase = null;
    Cursor crs;
    SimpleCursorAdapter dataAdapter;
    ListView listView;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment01person, container,false);


    dataBase = getActivity().openOrCreateDatabase("DBperson.db", 
            android.content.Context.MODE_PRIVATE, null);

    crs = dataBase.rawQuery("SELECT * FROM person", null);


         String[] columns = new String[] {
            "person_name",
            "person_phone",
            "person_email"
          };

          // the XML defined views which the data will be bound to
          int[] to = new int[] { 
            R.id.text01person,
            R.id.text02person,
            R.id.text03person
          };

          // create the adapter using the cursor pointing to the desired data 
          //as well as the layout information
          dataAdapter = new SimpleCursorAdapter(
                  getActivity(), R.layout.fragment01itemlist, 
            crs, 
            columns, 
            to);

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

          View v = new View(getActivity());
          listView.addHeaderView(v);
          listView.addFooterView(v);

          // Assign adapter to ListView
          listView.setAdapter(dataAdapter);  

    rootView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT ));       
    return rootView;

    }

    public void onListItemClick(ListView l, View v, int position, long id) 
    {
        super.onListItemClick(l, v, position, id); 

        Cursor cursorLocal = (Cursor) l.getItemAtPosition(position);

        String nameperson = cursorLocal.getString(cursorLocal.getColumnIndex("person_name"));
        String endperson = cursorLocal.getString(cursorLocal.getColumnIndex("person_endereco"));
        String phoneperson = cursorLocal.getString(cursorLocal.getColumnIndex("person_phone"));
        String emailperson = cursorLocal.getString(cursorLocal.getColumnIndex("person_email"));

        showMessage("test",nameperson+" "+endperson+" "+phoneperson+" "+emailperson);


    }




    public void showMessage (String title, String text){
        AlertDialog.Builder message = new AlertDialog.Builder(getActivity());
        message.setTitle(title);
        message.setMessage(text);
        message.setNeutralButton("Ok", null);
        message.show();
    }

}
这是我的itemlist xml(fragment01itemlist.xml):


这是我的listview xml(fragment01person.xml):


我可以显示列表。 但我不知道如何实现这些按钮

我读过各种材料,但都不管用


有人能帮忙吗?

要在列表行中创建多个可单击项,您必须在XML中包含以下视图/按钮的给定代码:

android:clickable="true" 
android:focusable="false" 
android:focusableInTouchMode="true"
对于单行的操作,您需要使用自定义适配器,而不是使用SimpleCursorAdapter,下面是示例

     public class CustomListAdapter  extends BaseAdapter
        {

            private Context mContext;
            String[] cursor;
            public SMSListAdapter(Context context,String[] cur)
            {
                    super();
                    mContext=context;
                    cursor=cur;

            }

            public int getCount()
            {
                // return the number of records in cursor
                return cursor.length;
            }

            // getView method is called for each item of ListView
            public View getView(int position,  View view, ViewGroup parent)
            {
                            // inflate the layout for each item of listView
                            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                            view = inflater.inflate(R.layout.listview_each_item, null);



                            // get the reference of textViews
                            TextView textViewConatctNumber=(TextView)view.findViewById(R.id.textViewSMSSender);
                            TextView textViewSMSBody=(TextView)view.findViewById(R.id.textViewMessageBody);
    Button bt1=(Button)view.findViewById(R.id.btn1);

    bt1.setOnClickListner(new OnClickListener() {

                @Override
                public void onClick(View view) {
}
});

                            // Set the Sender number and smsBody to respective TextViews
                            textViewConatctNumber.setText(senderNumber);
                            textViewSMSBody.setText(smsBody);


                            return view;
            }

            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return position;
            }

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

是否要使按钮可单击?是!我在xmlandroid:clickable=“true”上设置了android:clickable=“true”android:focusable=“false”android:focusableInTouchMode=“true”这项工作只是不知道如何在代码中实现操作将此代码放在listview项的xml文件中我尝试过这样做。我改变了变量。但它没有起作用。我不知道我做错了什么。我从我的项目列表中发布了xml,以向您展示我想要做的事情。请遵循此链接-您甚至可以使用此链接-非常感谢。我将学习并应用这些教程的示例。