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:
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="true"
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;
}
}