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

时间:2014-09-08 03:39:03

标签: android sqlite android-listfragment

我是初学者。 我有一个ListFragment,列表中的每个元素都包含三个TextView和两个不同的Buttons。从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) ...

按钮1将拨打电话,2按钮将向个人发送电子邮件。 细节,这些按钮是可点击的ImageView。

数据库已预先填充。

我这样代理我的代码:

@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):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/itemlistPerson"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:paddingLeft="15dp"
   android:paddingRight="15dp"
   android:descendantFocusability="beforeDescendants">

   <LinearLayout
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:paddingLeft="15dp"
      android:paddingTop="5dp"
      android:paddingBottom="5dp"
      android:paddingRight="15dp"
      android:descendantFocusability="afterDescendants">

      <TextView
          android:id="@+id/text01person"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textColor="@color/blue_light2"
          android:textSize="18sp"
          android:text= "test"
          android:textStyle="bold|italic"
           />


      <TextView
          android:id="@+id/text02person"
          android:layout_width="wrap_content"
          android:text= "test"
          android:layout_height="wrap_content"
           />

      <TextView
          android:id="@+id/text03person"
          android:layout_width="wrap_content"
          android:text= "test"
          android:layout_height="wrap_content"
          android:textStyle="italic"
           />

      <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">

       <ImageView
        android:id="@+id/imgPhone"
        android:layout_width="?android:attr/listPreferredItemHeight"
        android:layout_height="fill_parent"
        android:src="@drawable/ic_action_call_tc_01"
        android:scaleType="center"
        android:clickable="true"
        android:focusable="false" 
        android:focusableInTouchMode="true"
         />

       <ImageView
        android:id="@+id/imgEmail"
        android:layout_width="?android:attr/listPreferredItemHeight"
        android:layout_height="fill_parent"
        android:src="@drawable/ic_action_new_email_tc"
        android:scaleType="center"
        android:clickable="true"
        android:focusable="false" 
        android:focusableInTouchMode="true"
         />


      </LinearLayout>

   </LinearLayout>
</FrameLayout>

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

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

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:background="@color/pink_very_light"
        android:cacheColorHint="@android:color/transparent"
        android:divider="@null"
        android:dividerHeight="10dp"
        android:footerDividersEnabled="true"
        android:headerDividersEnabled="true"
        android:listSelector="@android:color/transparent" >

    </ListView>

</RelativeLayout>

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

我读过各种材料,但都没有。

请有人帮忙吗?

1 个答案:

答案 0 :(得分:0)

要在列表行中创建多个可点击的内容,您必须在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;
            }
        }