i have a ListView in my application, i fetch contacts from user phonebook then displays them into ListView, now what i want to do is to add different child views into ListView item based on some condition like my application check each phonebook contact for it's availability on server database and what i want to achieve is to add voice/video call buttons for that particular contact and add invite button for all other contacts that are not available in database.
My condition applies on these three child's of ListView item, voice call button, video call button and invite button and my condition is if user is available in database then i only want to display voice/video call button and hide invite button but if the user is not available then i wan to display invite button and hide audio/video call buttons
ListView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/contacts_listview"/>
</RelativeLayout>
ListView Item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="16dp">
<ImageView
android:id="#+id/contact_item_icon"
android:layout_width="50dp"
android:layout_height="50dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="#+id/contact_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Feel The Light" />
<TextView
android:id="#+id/contact_item_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home" />
</LinearLayout>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_audio_call_dark"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_video_call_dark"/>
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="Invite"
android:background="#drawable/selector_button_green_oval"
android:textColor="#color/white"/>
</LinearLayout>
ContactsFragment
public class ContactsAdapter extends ArrayAdapter<ContactItem> {
private static final String LOG_TAG = ContactsAdapter.class.getSimpleName();
public ContactsAdapter(Activity context, List<ContactItem> contactItems) {
super(context, 0, contactItems);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ContactItem contactItem = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.contact_list_item, parent, false);
}
ImageView contactImage = (ImageView) convertView.findViewById(R.id.contact_item_icon);
contactImage.setImageResource(contactItem.contactImage);
TextView contactName = (TextView) convertView.findViewById(R.id.contact_item_name);
contactName.setText(contactItem.contactName);
TextView contactNumber = (TextView) convertView.findViewById(R.id.contact_item_number);
contactNumber.setText(contactItem.contactNumber);
return convertView;
}
}
ContactsFragment
public class ContactsListFragment extends Fragment {
private ContactsAdapter contactsAdapter;
public static ContactsListFragment newInstance() {
return new ContactsListFragment();
}
public ContactsListFragment() {
}
ArrayList<String> contactName = new ArrayList<>();
ArrayList<String> contactNumber = new ArrayList<>();
ArrayList<ContactItem> contactsAvailable = new ArrayList<>();
ArrayList<ContactItem> contactsInvite = new ArrayList<>();
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor cursor = getActivity().getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null
);
String currentNumber = "";
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
number = number.replaceAll(" ", "");
char c = number.charAt(0);
char c1 = number.charAt(1);
String s = Character.toString(c)+Character.toString(c1);
if (s.equals("00")) {
number = number.replaceAll("00", "+");
}
if (number.length() == 12) {
number = "+" + number;
}
if (number.length() == 13) {
if (!currentNumber.equals(number)) {
contactName.add(name);
contactNumber.add(number);
}
}
currentNumber = number;
}
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_contacts_list, container, false);
QBPagedRequestBuilder pagedRequestBuilder = new QBPagedRequestBuilder();
pagedRequestBuilder.setPage(1);
pagedRequestBuilder.setPerPage(50);
QBUsers.getUsersByLogins(contactNumber, pagedRequestBuilder, new QBEntityCallback<ArrayList<QBUser>>() {
#Override
public void onSuccess(ArrayList<QBUser> qbUsers, Bundle bundle) {
for (int i = 0; i < qbUsers.size(); i++) {
ContactItem contactItem = new ContactItem(qbUsers.get(i).getFullName(), qbUsers.get(i).getLogin(), R.drawable.ic_launcher);
contactsAvailable.add(contactItem);
}
contactsAdapter = new ContactsAdapter(getActivity(), contactsAvailable);
ListView listView = (ListView) rootView.findViewById(R.id.contacts_listview_available);
listView.setAdapter(contactsAdapter);
}
#Override
public void onError(QBResponseException e) {
Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG).show();
}
});
return rootView;
}
}
Try this:
Add a boolean flag isAvailableInDB in your ContactItem.java class.
Get contact list from your phonebook and check one by one with your server database.
If a contactItem exist in your database just set flag isAvailableInDB as true oterwise false for that contactItem.
After that add contactItem to ArrayList
Create ContactsAdapter and set it to ListView
Finally, add a condition in your adapter's getView() method to check the contactItem flag isAvailableInDB. If true, show voice/video call button & hide invite button and if false, show invite button & hide voice/video call button.
Give an id to XML button's:
<ImageButton
android:id="#+id/button_voice_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_audio_call_dark"/>
<ImageButton
android:id="#+id/button_video_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_video_call_dark"/>
<Button
android:id="#+id/button_invite"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="Invite"
android:background="#drawable/selector_button_green_oval"
android:textColor="#color/white"/>
In your adapter's getView() method:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ContactItem contactItem = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.contact_list_item, parent, false);
}
ImageView contactImage = (ImageView) convertView.findViewById(R.id.contact_item_icon);
contactImage.setImageResource(contactItem.contactImage);
TextView contactName = (TextView) convertView.findViewById(R.id.contact_item_name);
contactName.setText(contactItem.contactName);
TextView contactNumber = (TextView) convertView.findViewById(R.id.contact_item_number);
contactNumber.setText(contactItem.contactNumber);
ImageButton buttonVoiceCall= (ImageButton) convertView.findViewById(R.id.button_voice_call);
ImageButton buttonVideoCall= (ImageButton) convertView.findViewById(R.id.button_video_call);
Button buttonVoiceCall= (Button) convertView.findViewById(R.id.button_invite);
if(contactItem.getIsAvailableInDB())
{
buttonVoiceCall.setVisibility(View.VISIBLE);
buttonVideoCall.setVisibility(View.VISIBLE);
buttonInvite.setVisibility(View.GONE);
}
else
{
buttonVoiceCall.setVisibility(View.GONE);
buttonVideoCall.setVisibility(View.GONE);
buttonInvite.setVisibility(View.VISIBLE);
}
return convertView;
}
Hope this will help you~
You could add a flag on the ContactItem class and set it true or false, if it is available in the Database and then set the visibility of the buttons in the adapter according to this flag.
Related
Am developing an app that take student attendance, i want to get the list of checked the names on "TakeAttendance" when i click a "ViewAttendance" on same custom listview . Please how can i do that?
here is my codes...
The custom view
enter code here
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_firstname"
android:text="Firstname"
android:textSize="11dp"
android:ellipsize="start"
android:lines="3"
android:textColor="#000"
android:textStyle="bold"
android:gravity="center"
android:paddingLeft="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lastname"
android:lines="3"
android:textSize="11dp"
android:textColor="#000"
android:textStyle="bold"
android:gravity="center"
android:paddingLeft="10dp"
android:id="#+id/tv_lastname" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_surname"
android:text="Surname"
android:lines="3"
android:textSize="11sp"
android:textColor="#000"
android:textStyle="bold"
android:gravity="center"
android:paddingRight="80dp"
android:paddingLeft="10dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="20dp"
android:id="#+id/checkBox"
android:checked="false"
android:focusable="false"
android:background="#color/white" />
The Listview
enter code here
The ListAdapter
enter code herepublic class StudentListAdapter1 extends BaseAdapter {
private Context mContext;
private List mStudentList;
//Constructor
public StudentListAdapter1(Context mContext, List<StudentList> mStudentList) {
this.mContext = mContext;
this.mStudentList = mStudentList;
}
#Override
public int getCount() {
return mStudentList.size();
}
#Override
public Object getItem(int position) {
return mStudentList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = View.inflate(mContext, R.layout.student_take, null);
TextView tvReg_no = (TextView) v.findViewById(R.id.tv_reg_no);
TextView tvFirstname = (TextView) v.findViewById(R.id.tv_firstname);
TextView tvLastname = (TextView) v.findViewById(R.id.tv_lastname);
TextView tvSurname = (TextView) v.findViewById(R.id.tv_surname);
//Set text for TextView
tvReg_no.setText(mStudentList.get(position).getReg_no());
tvFirstname.setText(mStudentList.get(position).getFirstname());
tvLastname.setText(mStudentList.get(position).getLasttname());
tvSurname.setText(mStudentList.get(position).getSurname());
//Save product id to tag
v.setTag(mStudentList.get(position).getId());
return v;
}
}
The Attendance Activity
enter code here protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_take100);
lvStudentlist= (ListView) findViewById(R.id.listview_studentlist);
mStudentList = new ArrayList<>();
//Add sample data for list
//We can get data from DB, webservice here
mStudentList.add(new StudentList(1, "U11EE1001", "Bargo","S.","Mayafi"));
mStudentList.add(new StudentList(2, "U11EE1002", "Barnsbas","Snake.","Maciji"));
mStudentList.add(new StudentList(3, "U11EE1004", "Adamu","Tanko.","Sadau"));
mStudentList.add(new StudentList(4, "U11EE1005", "Munzali","","Cire Tallafi"));
//Init adapter
adapter = new StudentListAdapter1(getApplicationContext(), mStudentList);
lvStudentlist.setAdapter(adapter);
checkBox = (CheckBox) findViewById(R.id.checkBox);
lvStudentlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Do something
SparseBooleanArray checked = lvStudentlist.getCheckedItemPositions();
ArrayList<String> selectedItems = new ArrayList<>();
for (int i = 0; i < checked.size(); i++) {
// Item position in adapter
position = checked.keyAt(i);
// Add names if it is checked i.e.) == TRUE!
if (checked.valueAt(i))
selectedItems.add((String) adapter.getItem(position));
}
String[] outputStrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
outputStrArr[i] = selectedItems.get(i);
}
Intent intent = new Intent(getApplicationContext(),
ViewAttendanceActivity.class);
// Create a bundle object
Bundle b = new Bundle();
b.putStringArray("selectedItems", outputStrArr);
// Add the bundle to the intent.
intent.putExtras(b);
// start the ResultActivity
startActivity(intent);
}
});
}
}
The View Attendance Activity
enter code here protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_attendance);
Bundle b = getIntent().getExtras();
String[] resultArr = b.getStringArray("selectedItems");
lvStudentlist= (ListView) findViewById(R.id.listview_studentlist);
adapter = new StudentListAdapter(getApplicationContext(), mStudentList);
lvStudentlist.setAdapter(adapter);
}
}
At first I think you should reuse your items in listview
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView != null) {
v = convertView;
} else {
v = View.inflate(mContext, R.layout.student_take, null);
}
.......
now answer for your question, I think you need to add your selected items to array in lvStudentlist.setOnItemClickListener method and then send it as Intent extra to your next activity. You can send String[] as Intent extra so you have no need to use Bundle.
I'm using a ListView with ArrayAdapter. There is a Button along with TextView in the listview.xml. I want to retrieve data from that TextView by Clicking over corresponding buttons.
My listview.xml is as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="90dp"
android:id="#+id/btConfirmfriend"
android:text="Confirm Friend"
/>
</RelativeLayout>
My PendingRequest Class is as follows:
public class PendingRequest extends AddFriend {
TextView tvPending;
Button btConfirmfriend;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pendingrequest);
final String[] s= getIntent().getExtras().getStringArray("pending");
System.out.println(s[1]);
final ListAdapter adapter = new ArrayAdapter<String>(this,R.layout.listview,R.id.label,s);
final ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
final View inflatedView = getLayoutInflater().inflate(R.layout.listview, null);
btConfirmfriend = (Button) inflatedView.findViewById(R.id.btConfirmfriend);
btConfirmfriend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ServerConnection sc = new ServerConnection();
//here I want to retrieve the value from List
//System.out.println(s[adapter.]);
int status = sc.confirmRequest(s[listView.getSelectedItemPosition()]);
AlertDialog.Builder alert = new AlertDialog.Builder(PendingRequest.this);
if (status == 1) {
alert.setMessage("Friend has been Added");
alert.show();
} else {
alert.setMessage("Sorry for inconvinient!!Try Again");
alert.show();
}
}
});
}
}
My pendingrequest.xml file is:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#drawable/background"
android:layout_height="match_parent">
<Button
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#e9abab"
android:id="#+id/btSend"
android:text="Pending Requests"
/>
<ListView
android:id="#+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
On clicking over the button nothing happening.onClick() method is not executing.Please help.
Since you need a click listener on a view inside your list item, you need to write your own adapter for that -
class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, String[] list) {
super(context, 0, list);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Get the data item for this position
String item = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
// Lookup view for data population
TextView label = (TextView) convertView.findViewById(R.id.label);
Button btConfirmfriend = (Button) convertView.findViewById(R.id.btConfirmfriend);
// Populate the data into the template view using the data object
label.setText(item);
btConfirmfriend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getItem(position); //Get your selected text
}
});
return convertView;
}
}
And this is how you will set the adapter -
final CustomAdapter adapter = new CustomAdapter(this,s);
final ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
In my fragment, I want each custom item in my listview to look as follows:
Name
SetsXReps
[][][][][] ---> array of textviews
[][][][][] ---> array of checkboxes
The number of textviews and checkboxes depends upon the item in the adapter, so I need to create that part dynamically. I am having trouble adding these elements dynamically to my relativelayout that I partially define in my xml. Here is my fragment class followed by my xml for the custom list item. The id for the layout is list_item_exercise_in_workout.
public class WorkoutFragment extends Fragment
{
public static final String EXTRA_ALREADYCREATED_ID = "shake2lift.nickdelnano.com";
private ExAdapter adapter;
private ListView listView;
private ArrayList<Set> sets;
private Workout w;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UUID workoutId = (UUID) getArguments().getSerializable(EXTRA_ALREADYCREATED_ID);
w = WorkoutMASTER.get(getActivity()).getWorkout(workoutId);
}
public View onCreateView(LayoutInflater inflater, ViewGroup parent,Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_workout, parent, false);
adapter = new ExAdapter(getActivity(), R.layout.fragment_create_workout, R.id.list, sets);
listView.setAdapter(adapter);
return v;
}
public static WorkoutFragment newInstance(UUID workoutId)
{
Bundle args = new Bundle();
args.putSerializable(EXTRA_ALREADYCREATED_ID, workoutId);
WorkoutFragment fragment = new WorkoutFragment();
fragment.setArguments(args);
return fragment;
}
private class ExAdapter extends ArrayAdapter<Set>
{
public ExAdapter(Context c, int resource, int textViewResourceId, ArrayList<Set> sets ) {
super(c, 0, sets);
}
public View getView(final int position, View convertView, ViewGroup parent)
{
//if we werent given a view, inflate one
if(convertView == null)
{
convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_exercise_in_workout, null);
}
Set s = getItem(position);
//RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
// RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT );
TextView name = (TextView) convertView.findViewById(R.id.exName);
name.setText(s.getName());
TextView setsReps = (TextView) convertView.findViewById(R.id.setsXreps);
setsReps.setText(s.getSets() + "X" + s.getReps());
CheckBox[] checkBoxes = new CheckBox[s.getSetsInt()];
EditText[] weightBoxes = new EditText[s.getSetsInt()];
//initialize weightBoxes's text to the inputted weight
for(int i = 0; i < weightBoxes.length; i++)
{
//code to add dynamically here
}
return convertView;
}
}
}
and my XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/relativeLayoutEx"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name"
android:id="#+id/exName"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="SetsXReps"
android:id="#+id/setsXreps"
android:layout_below="#id/exName"
android:layout_alignParentLeft="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2"
android:layout_below="#+id/setsXreps"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="112dp" />
I appreciate any help or advice.
You need to first get a pointer to your RelativeLayout like this
RelativeLayout container = (RelativeLayout) view.findViewById(R.id.relativeLayoutEx);
Then in your for loop, create the Text Boxes (and Check Boxes) dynamically and add them to the layout:
TextView text = new TextView(getActivity());
text.setText("Hello");
container.addView(text);
When I run my program I get the error E/AndroidRuntime(27275): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. where is the problem??
My list fragment class
package com.zaa.yhgj;
public class HomeFragment extends ListFragment {
private List<ListViewItem> mItems; // ListView items list
View myFragmentView;
ArrayList<View> chart_view = new ArrayList<View>();
List<Integer> onedimen = new ArrayList<Integer>();
List<List> aa = new ArrayList<List>();
///
List<List> statuslistoflist = new ArrayList<List>();
List<String> statusonedimen = new ArrayList<String>();
private List<DialogInterface.OnClickListener> nItems;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.dashboardlist, container, false);
HashMap<String,Integer> myhash=new HashMap<String, Integer>() ;
myhash.put("Pending",0);
myhash.put("In Progress",0);
myhash.put("Limitation",0);
myhash.put("Needs Research",0);
myhash.put("In Testing",0);
myhash.put("Issue Not Clear",0);
myhash.put("Unassigned",0);
int mainclasssize = Constants.listuserprojectMain.getMainListClass().size();
int[] listsize = new int[mainclasssize];
String[] projectname = new String[mainclasssize];
int[] openissuecount = new int[mainclasssize];
for (int k = 0; k < mainclasssize; k++) {
listsize[k] = Constants.listuserprojectMain.getMainListClass().get(k).getProjectIssue().size();
projectname[k] = Constants.listuserprojectMain.getMainListClass().get(k).getName();
openissuecount[k] = Constants.listuserprojectMain.getMainListClass().get(k).getOpenIssueCount();
}
for (int w = 0; w < mainclasssize; w++) {
onedimen = new ArrayList<Integer>();
for (int x = 0; x < listsize[w]; x++) {
myhash.put(Constants.listuserprojectMain.getMainListClass().get(w).getProjectIssue().get(x).getStatus(),Constants.listuserprojectMain.getMainListClass().get(w).getProjectIssue().get(x).getIssueCount());
}
Set keys = myhash.keySet();
Iterator itr = keys.iterator();
String key;
int value;
while(itr.hasNext())
{
key = (String)itr.next();
onedimen.add((Integer)myhash.get(key));
value = (Integer)myhash.get(key);
// System.out.println(key + " - "+ value);
}
aa.add(onedimen);
statuslistoflist.add(statusonedimen);
}
for (int v = 0; v < aa.size(); v++) {
chart_view.add(openChart(aa.get(v)));
}
mItems = new ArrayList<ListViewItem>();
nItems = new ArrayList<DialogInterface.OnClickListener>();
Resources resources = getResources();
for (int f = 0; f < aa.size(); f++) {
mItems.add(new ListViewItem(projectname[f], openissuecount[f], chart_view.get(f)));
}
nItems.add(new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
// initialize and set the list adapter
setListAdapter(new ListViewDemoAdapter(getActivity(), mItems));
return myFragmentView;
}
private View openChart(List<Integer> chartvaluedd) {
return v;
}
}
my adapter class is
public class ListViewDemoAdapter extends ArrayAdapter {
public ListViewDemoAdapter(Context context, List<ListViewItem> items) {
super(context, R.layout.dashboardlistview_item, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder= new ViewHolder();
if(convertView == null) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.dashboardlistview_item, parent, false);
// initialize the view holder
// viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
viewHolder.graphView = (LinearLayout) convertView.findViewById(R.id.ll_chart);
viewHolder.tvCreateIssue = (TextView) convertView.findViewById(R.id.tvCreateissue);
viewHolder.tvDetails = (TextView) convertView.findViewById(R.id.tv_details);
viewHolder.tvIssue = (TextView) convertView.findViewById(R.id.tv_issues);
viewHolder.tvProjectTitle = (TextView) convertView.findViewById(R.id.tv_projectTitle);
viewHolder.tvOpenIssueCount = (TextView) convertView.findViewById(R.id.tv_openissues);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
// recycle the already inflated view
///error when scrolling graph
viewHolder.graphView.removeAllViews();
}
// update the item view
final ListViewItem item = getItem(position);
viewHolder.tvCreateIssue.setText("Create Issue");
viewHolder.tvProjectTitle.setText(item.title);
viewHolder.graphView.addView(item.chartview);
viewHolder.tvOpenIssueCount.setText("Open Issues "+item.openissuecount);
viewHolder.tvCreateIssue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), item.title, Toast.LENGTH_SHORT).show();
}
});
/////
viewHolder.tvIssue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getContext(),"issue",Toast.LENGTH_LONG).show();
}
});
return convertView;
}
private static class ViewHolder {
// ImageView ivIcon;
LinearLayout graphView;
TextView tvCreateIssue;
TextView tvDetails;
TextView tvProjectTitle;
TextView tvOpenIssueCount;
TextView tvIssue;
}
}
my xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include layout="#layout/top" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none" >
</ListView>
</LinearLayout>
my listview item is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center_vertical"
android:background="#color/list_background"
android:padding="5dp"
android:id="#+id/ww"
>
<!-- the innner view - provides the white rectangle -->
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#drawable/frame"
android:id="#+id/jhhh">
<!-- the icon view -->
<TextView
android:id="#+id/tv_projectTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project name"
android:textSize="16dp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
/>
<LinearLayout android:id="#+id/ll_chart"
android:orientation="vertical"
android:layout_below="#+id/tv_projectTitle"
android:layout_width="wrap_content"
android:layout_height="230dp"
android:background="#ffffff"
android:padding="5dp"
android:scaleType="fitXY" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ll_chart"
android:id="#+id/tv_openissues"
android:text="Open Issues 24"/>
<LinearLayout
android:layout_width="wrap_content"
android:id="#+id/ll_textview"
android:padding="5dp"
android:layout_alignParentRight="true"
android:layout_below="#id/tv_openissues"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView android:id="#+id/tvCreateissue"
android:drawableLeft="#drawable/bug6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Issue"
/>
<!-- the description view -->
<TextView android:id="#+id/tv_details"
android:layout_below="#id/tvCreateissue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/bars64"
android:layout_marginLeft="5dp"
android:text="Details"
/>
<TextView android:id="#+id/tv_issues"
android:layout_below="#id/tvCreateissue"
android:layout_marginLeft="5dp"
android:drawableLeft="#drawable/bug6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Issues"
/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
I might be wrong, but the error seems to reside here:
viewHolder.graphView.addView(item.chartview);
Once the view item.chartview is added to graphView, it has a parent. However ListView is known to call some getView on some positions multiple times for caching.
It would seem that the same view is being added to multiple parents which in return throws the error you get. Before adding the chartview you could try removing it from its parent if it exists:
// Remove from parent (if exists)
ViewGroup parent = item.chartview.getParent();
if (parent != null) {
parent.removeView(item.chartview);
}
// Add to another parent
viewHolder.graphView.addView(item.chartview);
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.dashboardlist, container, false);
instead of container use null
(I'm not an android geek but faced the problem before)
I am using a ListView with a custom adapter for displaying items as pairs of title/subtitle TextViews.
Unfortunately, I can select an item just by clicking on its upper half, the one occupied by the title
Here is the code I am using:
journal_list.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:background="#fff"
android:cacheColorHint="#fff"
android:paddingBottom="6dp"
/>
journal_list_item.xml for the list item's layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
>
<TextView
android:id="#+id/journal_entry_date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>
<TextView
android:id="#+id/journal_content"
android:paddingLeft="28dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="16sp"
android:textColor="#555"
android:inputType="textMultiLine"
android:maxLines="2"
android:minLines="1"
android:layout_below="#id/journal_entry_date"
android:layout_alignParentLeft="true"
/>
Also the code for the adapter:
private class JournalAdapter extends ArrayAdapter<JournalEntry> {
Context ctxt;
public JournalAdapter(Context ctxt) {
super(ctxt, R.layout.journal_list_item);
this.ctxt = ctxt;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
JournalHolder holder;
if (row == null) {
row = ((LayoutInflater)ctxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).
inflate(R.layout.journal_list_item, parent, false);
holder = new JournalHolder(row);
row.setTag(holder);
} else {
holder = (JournalHolder) row.getTag();
}
JournalEntry crt = getItem(position);
holder.getDate().setText(crt.dateWritten);
holder.getContent().setText(crt.content);
return row;
}
}
private static class JournalHolder {
private TextView date;
private TextView content;
private View base;
public JournalHolder(View base) {
this.base = base;
}
public TextView getDate() {
if (date == null)
date = (TextView) base.findViewById(R.id.journal_entry_date);
return date;
}
public TextView getContent() {
if (content == null)
content = (TextView) base.findViewById(R.id.journal_content);
return content;
}
}
Code from the ListActivity's onCreate() method:
private JournalAdapter adapter;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.journal_list);
adapter = new JournalAdapter(this);
setListAdapter(adapter);
registerForContextMenu(getListView());
}
Also, I am calling a method called updateList() in onResume()
private void updateList() {
adapter.clear();
Cursor cursor = helper.listJournal(start, end);
cursor.moveToFirst();
JournalEntry crt;
while (!cursor.isAfterLast()) {
crt = new JournalEntry();
crt.dateWritten = cursor.getString(cursor.getColumnIndex("date_written"));
crt.content = cursor.getString(cursor.getColumnIndex("content"));
crt.id = cursor.getInt(cursor.getColumnIndex("entry_id"));
adapter.add(crt);
cursor.moveToNext();
}
cursor.close();
adapter.notifyDataSetChanged();
}
List item clicking is handled in the onListItemClick of my ListActivity, as follows:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent(this, JournalEditor.class);
intent.setAction(Intent.ACTION_EDIT);
intent.putExtra("entry_id", adapter.getItem(position).id);
startActivity(intent);
}
Apparently, I am forgetting something important, and that's why this whole weird thing happens.
I have found a similar discussion here: http://groups.google.com/group/android-developers/browse_thread/thread/5636c8ea74033657 but it wasn't very helpful.
The problem may be from the clicking of the textviews. When you set the views of the listview items as clickable, they override the click of the listview item and the click will only work when the specific view of the item is clicked.
If this is the case, do the following for your two textviews in the getView() function:
TextView tv;
tv.setFocusable(false);
tv.setClickable(false);
Then set the listview to clickable: listView1.setClickable(true);