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) ...
where the button 1 will make a call and the 2 button will send an email to person. detail, these buttons are clickable ImageView.
the Database is already pre-populated.
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();
}
}
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>
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.
please can anyone help with this?
For making the multiple clickables in your list row you have to include the below given code for the views/buttons in the XML:
and for doing operation for single row you need to use custom adapters instead using SimpleCursorAdapter, here the example goes