用不同的动作多可点击的项目在ListFragment从SQLite的执行 [英] Multiple clickable items with different actions to perform in ListFragment from SQLite

查看:135
本文介绍了用不同的动作多可点击的项目在ListFragment从SQLite的执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm a beginner. I've a ListFragment where every element in the list contains three TextView and two different Buttons. Reading data from SQLite Database. Something like this:

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。

where the button 1 will make a call and the 2 button will send an email to person. detail, these buttons are clickable ImageView.

数据库已在$ P $填充页。

the Database is already pre-populated.

我iniciate我的code是这样的:

I iniciate my code like this:

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

this is my 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):

this is my 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>

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

I can display the list. but I don't know how to implement the buttons!

我读过的各种材料,但没有工作。

I've read various materials, but none works.

请任何人都可以在这方面帮助?

推荐答案

有关在列表行做多clickables你必须包含如下code代表的意见/在XML按钮:

For making the multiple clickables in your list row you have to include the below given code for the views/buttons in the XML:

android:clickable="true" 
android:focusable="false" 
android:focusableInTouchMode="true"

和做操作单列你需要使用而不是使用SimpleCursorAdapter自定义适配器,这里的例子去

and for doing operation for single row you need to use custom adapters instead using SimpleCursorAdapter, here the example goes

     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;
            }
        }

这篇关于用不同的动作多可点击的项目在ListFragment从SQLite的执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆