Show my Fragment in Dialog android - android

I have a class that extends a Fragment. I want when press a button from An Activity to opens this Fragment with its layout in dialog in addition to that the fragment opens normally in its place. Is this possible?
This is my Fragment (TipsFragment.java)
public class TipsFragment extends Fragment{
ListView list;
TipsAdapter tipsAdapter;
AlertDialog PageDialog;
DAO db;
Cursor c;
ArrayList<HashMap<String, String>> tipsList;
HashMap<String, String> map;
ArrayList<HashMap<String, String>> pages;
Integer tipType;
ImageButton page;
ImageButton add;
ImageButton search;
EditText inputAdd;
EditText inputSearch;
ImageView addImage;
RelativeLayout layoutAdd;
RelativeLayout layoutSearch;
int itemSelected;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
db = new DAO(activity);
db.open();
}
public TipsFragment(){}
public TipsFragment(int tipType, int itemSelected ){
this.tipType = tipType;
this.itemSelected = itemSelected;
}
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tips_fragement, container, false);
System.out.println("tipType: "+tipType);
System.out.println("itemSelected: "+itemSelected);
page = (ImageButton) rootView.findViewById(R.id.pager);
add = (ImageButton) rootView.findViewById(R.id.add);
search = (ImageButton) rootView.findViewById(R.id.search);
inputSearch = (EditText) rootView.findViewById(R.id.inputSearch);
inputAdd = (EditText) rootView.findViewById(R.id.inputAdd);
addImage = (ImageView) rootView.findViewById(R.id.imageAdd);
layoutAdd = (RelativeLayout) rootView.findViewById(R.id.layoutAdd);
layoutSearch = (RelativeLayout) rootView.findViewById(R.id.layoutSearch);
if (tipType != 0) {
switch (tipType) {
case 1:
System.out.println("All tips");
if (getActivity().getIntent().getStringExtra("startFrom") == null) {
c = db.getTips("0");
} else {
c = db.getTips(getActivity().getIntent().getStringExtra("startFrom"));
}
break;
case 2:
System.out.println("favorite tips");
c = db.getFavoriteTips();
page.setVisibility(View.GONE);
System.out.println("in favorite, count_records: "+c.getCount());
break;
}
}
System.out.println("count_records: "+c.getCount());
if (c.getCount() != 0) {
if (getActivity().getIntent().getStringExtra("startLabel") != null) {
page = (ImageButton) rootView.findViewById(R.id.pager);
}
tipsList = new ArrayList<HashMap<String, String>>();
list = (ListView) rootView.findViewById(R.id.tipList);
do {
map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(DAO.TIP_ID, c.getString(c.getColumnIndex(c.getColumnName(0))));
map.put(DAO.TIP_CONTENT, c.getString(c.getColumnIndex(c.getColumnName(1))));
// adding HashList to ArrayList
tipsList.add(map);
} while (c.moveToNext());
// Getting adapter by passing xml data ArrayList
tipsAdapter = new TipsAdapter(getActivity(), tipsList);
list.setAdapter(tipsAdapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
map = tipsList.get(position);
System.out.println("in list select item, tipType: "+tipType);
if (!MainActivity.tSlidingLayer.isOpened()) {
MainActivity.tSlidingLayer.openLayer(true);
}
}
});
page.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Strings to Show In Dialog with Radio Buttons
}
});
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
inputSearch.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
}
addImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "addImage pressed", Toast.LENGTH_LONG).show();
}
});
return rootView;
}
}
and this is layout tips_fragement.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/titleBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/add"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:scaleType="fitXY"
android:src="#drawable/add" />
<ImageButton
android:id="#+id/search"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:scaleType="fitXY"
android:src="#drawable/search" />
<ImageButton
android:id="#+id/pager"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:scaleType="fitXY"
android:src="#drawable/pager" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="5dip"
android:background="#drawable/shadow" >
</View>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" >
<EditText
android:id="#+id/inputAdd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Add" />
<ImageView
android:id="#+id/imageAdd"
android:padding="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/inputAdd"
android:layout_alignBottom="#+id/inputAdd"
android:layout_alignRight="#+id/inputAdd"
android:src="#android:drawable/ic_menu_add" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layoutSearch"
android:visibility="gone" >
<EditText
android:id="#+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search"/>
<ImageView
android:id="#+id/imageSearch"
android:padding="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/inputSearch"
android:layout_alignBottom="#+id/inputSearch"
android:layout_alignRight="#+id/inputSearch"
android:src="#android:drawable/ic_menu_search" />
</RelativeLayout>
<ListView
android:id="#+id/tipList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/buttons"
android:divider="#351802"
android:dividerHeight="2dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>

Since Android supports nesting Fragments (available in the support library) you could show your TipsFragment as a content of another DialogFragment. Avoid this solution whenever possible.
A better solution would be to make TipsFragment extend DialogFragment and that way you can either show the Fragment inline or in a dialog.
1 - Make TipsFragment extends DialogFragment
public class TipsFragment extends DialogFragment {...}
2 - On button click use FragmentTransaction.add or FragmentTransaction.replace to show the DialogFragment inline or include it in your layout and change its visibility.
3 - Show the TipsFragment as a dialog
TipsFragment fr = new TipsFragment();
fr.show(getSupportFragmentManager(), "dialog_fragment_tag");
Guide: Performing Fragment Transactions. More samples are available in the ApiDemos application from the SDK samples

Related

dynamic add-view with ID

i am creating an application, which i use to create a list of things that i have in SQLite database
i created a Fragment with a button that add a xml file to my LinearLayout , it works good , but i want to grab EditText and Spinner data and put them in a JSONobject but i don't know how , i don't have IDs of dynamically created views
My Fragment XML
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/itemsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:orientation="vertical"
android:paddingTop="20dp">
</LinearLayout>
</ScrollView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/addItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
app:srcCompat="#drawable/fab_add"
/>
</RelativeLayout>
My field (addView) XML
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:weightSum="3"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/add"
android:onClick="onIncrease"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="#drawable/ic_add_circle_black_24dp" />
<Button
android:id="#+id/remove"
android:onClick="onDecrease"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:background="#drawable/ic_remove_circle_black_24dp" />
</LinearLayout>
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:inputType="number"
android:hint="#string/number"
/>
<Spinner
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
Fragment.java File
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_newfactor,container,false);
fab = v.findViewById(R.id.addItem);
ln = v.findViewById(R.id.itemsContainer);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.field, null);
final EditText editText = rowView.findViewById(R.id.number);
Button add = rowView.findViewById(R.id.add);
Button remove = rowView.findViewById(R.id.remove);
Button clear = rowView.findViewById(R.id.clear);
Spinner spinner = rowView.findViewById(R.id.spinner);
list = new ArrayList<>();
dbConnector = new DbConnector(getContext(),null,null,1);
Cursor c = dbConnector.get().rawQuery("SELECT * FROM product",null);
while (c.moveToNext()){
int id = c.getInt(c.getColumnIndex("id"));
String name = c.getString(c.getColumnIndex("name"));
String desc = c.getString(c.getColumnIndex("description"));
String price = c.getString(c.getColumnIndex("price"));
list.add(new SpinnerObject(id,name,price));
}
spinnerAdapter = new ir.animelist.localshop.Adaptors.SpinnerAdapter(getContext(),android.R.layout.simple_list_item_1,list);
spinner.setAdapter(spinnerAdapter);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int num = Integer.parseInt(editText.getText().toString());
int num_num = num + 1;
editText.setText(num_num + "");
}
});
remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int num = Integer.parseInt(editText.getText().toString());
if(num > 0){
int num_num = num - 1;
editText.setText(num_num + "");
}
}
});
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ln.removeView((View) v.getParent());
}
});
ln.addView(rowView, ln.getChildCount() - 1);
}
});
return v;
}
You need to use the recyclerview to display your list items. Using recyclerview you can get view like -
View view = recyclerview.findViewHolderForAdapterPosition(pos).itemView;
Spinner spinner = view.findViewById(R.id.mySpinner);
This will return you a spinner of whichever position you have passed and afterwards get data from it.
Similarly, you can get view of edittext by using -
EditText edtTxt = view.findViewById(R.id.myEdtTxt);
To see how to get data from database and display that in recyclerview, check the example here - https://github.com/incipientinfo/db-queries

List item click listener inside a Fragment

I am trying to change the text and check box response of the clicked list item but I am not able to do so. As I not able to get the click list item.
The fragment is a part of view pager.
Here is my code,
public class ParticipantsFragment extends Fragment {
private ParticipantAdapter mAdapter;
SwipeRefreshLayout refreshLayout;
private String mParticipantId, mEventId, mGender;
ParticipantsAsyncTask task;
public ParticipantsFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mEventId = getArguments().getString(Config.BUNDLE_KEY_EVENT_ID);
mParticipantId = getArguments().getString(Config.BUNDLE_KEY_PARTICIPANT_ID);
mGender = getArguments().getString(Config.BUNDLE_KEY_GENDER);
Log.v("fragh", mEventId + " " + mParticipantId);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.participant_list, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.no_participant);
task = new ParticipantsAsyncTask();
task.execute();
ListView participantListView = (ListView) rootView.findViewById(R.id.participant_list);
refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout_participant_list);
participantListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.v("Clicked Participant",mAdapter.getItem(position).getParticipantName());
EditText notes = (EditText) rootView.findViewById(R.id.participant_list_item_notes);
String text = notes.getText().toString();
Log.v("Participant Text",text);
CheckBox checkBox = (CheckBox) rootView.findViewById(R.id.participant_list_item_checkbox);
String check;
if(checkBox.isChecked())
{
check = "1";
}
else
{
check = "0";
}
}
});
Button submit = (Button) rootView.findViewById(R.id.submit_participant);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
mAdapter = new ParticipantAdapter(getContext(), new ArrayList<Participant>());
participantListView.setAdapter(mAdapter);
participantListView.setEmptyView(textView);
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
task = new ParticipantsAsyncTask();
task.execute();
refreshLayout.setRefreshing(false);
}
});
return rootView;
}
private class ParticipantsAsyncTask extends AsyncTask<Void, Void, List<Participant>> {
private ProgressDialog progress;
#Override
protected void onPreExecute() {
progress = new ProgressDialog(getContext());
progress.setMessage("Gathering Data...");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.setCancelable(false);
progress.show();
}
#Override
protected void onPostExecute(List<Participant> data) {
// Clear the adapter of previous participant data
mAdapter.clear();
// If there is a valid list of {#link Event}s, then add them to the adapter's
// data set. This will trigger the ListView to update.
if (data != null && !data.isEmpty()) {
mAdapter.addAll(data);
}
}
#Override
protected List<Participant> doInBackground(Void... params) {
List<Participant> result;
if (!mGender.isEmpty()) {
HashMap<String, String> map = new HashMap<String, String>();
map.put(Config.KEY_EVENT_ID, mEventId);
map.put(Config.KEY_PARTICIPANT_ID, mParticipantId);
map.put(Config.KEY_GENDER, mGender);
result = QueryUtils.extractParticipantData(map, Config.FEVER_GENDER_FILTER_PARTICIPANT_URL);
} else {
HashMap<String, String> map = new HashMap<String, String>();
map.put(Config.KEY_EVENT_ID, mEventId);
map.put(Config.KEY_PARTICIPANT_ID, mParticipantId);
Log.v("law", mEventId + ", " + mParticipantId);
result = QueryUtils.extractParticipantData(map, Config.FEVER_ALL_PARTICIPANT_URL);
}
progress.dismiss();
return result;
}
}
#Override
public void onDestroyView() {
super.onDestroyView();
mAdapter.clear();
}
}
participant_list.xml
<?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="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/no_participant"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:gravity="center_horizontal"
android:text="#string/no_participant_found"
android:textColor="#color/primaryText"
android:textSize="24sp"
android:visibility="gone" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_layout_participant_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ListView
android:id="#+id/participant_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#color/divider"
android:dividerHeight="1dp"
android:descendantFocusability="beforeDescendants"
android:drawSelectorOnTop="true"
android:headerDividersEnabled="true" />
</android.support.v4.widget.SwipeRefreshLayout>
<Button
android:id="#+id/submit_participant"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:textSize="20sp"
android:background="#color/colorAccent"/>
</LinearLayout>
participant_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="80dp"
android:clickable="true"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="horizontal"
android:padding="8dp">
<TextView
android:id="#+id/participant_list_item_id"
android:layout_width="0dp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="2"
android:textSize="20sp"
tools:text="M78" />
<TextView
android:id="#+id/participant_list_item_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_weight="5"
android:textSize="20sp"
android:fontFamily="sans-serif"
tools:text="Billu Barber" />
<EditText
android:id="#+id/participant_list_item_notes"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_weight="6"
android:textSize="12sp"
android:background="#drawable/participant_list_notes"
android:hint="#string/participant_notes"
android:inputType="textMultiLine"
android:scrollbars="none"
android:imeOptions="actionDone"
android:maxLength="50"
android:padding="4dp" />
<CheckBox
android:id="#+id/participant_list_item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp" />
</LinearLayout>
My guess is that at the AdapterView.OnItemClickListener you are using rootView to access the cell properties. Like:
EditText notes = (EditText) rootView.findViewById(R.id.participant_list_item_notes);
...
CheckBox checkBox = (CheckBox) rootView.findViewById(R.id.participant_list_item_checkbox);
You should use the parameter View view from onItemClick like that:
EditText notes = (EditText) view.findViewById(R.id.participant_list_item_notes);
...
CheckBox checkBox = (CheckBox) view.findViewById(R.id.participant_list_item_checkbox);
Let me know if it solves your problem.
Edit:
If the problem is having the item clicked, check https://stackoverflow.com/a/20755698/2174489

setOnItemClickListener not working in Fragment

I am trying to change the text and check box response of the clicked list item but I am not able to do so. As I not able to get the clicked list item.
The fragment is a part of view pager.
Here is my code,
public class ParticipantsFragment extends Fragment {
private ParticipantAdapter mAdapter;
SwipeRefreshLayout refreshLayout;
private String mParticipantId, mEventId, mGender;
ParticipantsAsyncTask task;
public ParticipantsFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mEventId = getArguments().getString(Config.BUNDLE_KEY_EVENT_ID);
mParticipantId = getArguments().getString(Config.BUNDLE_KEY_PARTICIPANT_ID);
mGender = getArguments().getString(Config.BUNDLE_KEY_GENDER);
Log.v("fragh", mEventId + " " + mParticipantId);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.participant_list, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.no_participant);
task = new ParticipantsAsyncTask();
task.execute();
ListView participantListView = (ListView) rootView.findViewById(R.id.participant_list);
refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout_participant_list);
participantListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.v("Clicked Participant",mAdapter.getItem(position).getParticipantName());
EditText notes = (EditText) rootView.findViewById(R.id.participant_list_item_notes);
String text = notes.getText().toString();
Log.v("Participant Text",text);
CheckBox checkBox = (CheckBox) rootView.findViewById(R.id.participant_list_item_checkbox);
String check;
if(checkBox.isChecked())
{
check = "1";
}
else
{
check = "0";
}
}
});
Button submit = (Button) rootView.findViewById(R.id.submit_participant);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
mAdapter = new ParticipantAdapter(getContext(), new ArrayList<Participant>());
participantListView.setAdapter(mAdapter);
participantListView.setEmptyView(textView);
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
task = new ParticipantsAsyncTask();
task.execute();
refreshLayout.setRefreshing(false);
}
});
return rootView;
}
private class ParticipantsAsyncTask extends AsyncTask<Void, Void, List<Participant>> {
private ProgressDialog progress;
#Override
protected void onPreExecute() {
progress = new ProgressDialog(getContext());
progress.setMessage("Gathering Data...");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.setCancelable(false);
progress.show();
}
#Override
protected void onPostExecute(List<Participant> data) {
// Clear the adapter of previous participant data
mAdapter.clear();
// If there is a valid list of {#link Event}s, then add them to the adapter's
// data set. This will trigger the ListView to update.
if (data != null && !data.isEmpty()) {
mAdapter.addAll(data);
}
}
#Override
protected List<Participant> doInBackground(Void... params) {
List<Participant> result;
if (!mGender.isEmpty()) {
HashMap<String, String> map = new HashMap<String, String>();
map.put(Config.KEY_EVENT_ID, mEventId);
map.put(Config.KEY_PARTICIPANT_ID, mParticipantId);
map.put(Config.KEY_GENDER, mGender);
result = QueryUtils.extractParticipantData(map, Config.FEVER_GENDER_FILTER_PARTICIPANT_URL);
} else {
HashMap<String, String> map = new HashMap<String, String>();
map.put(Config.KEY_EVENT_ID, mEventId);
map.put(Config.KEY_PARTICIPANT_ID, mParticipantId);
Log.v("law", mEventId + ", " + mParticipantId);
result = QueryUtils.extractParticipantData(map, Config.FEVER_ALL_PARTICIPANT_URL);
}
progress.dismiss();
return result;
}
}
#Override
public void onDestroyView() {
super.onDestroyView();
mAdapter.clear();
}
}
participant_list.xml
<?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="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/no_participant"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:gravity="center_horizontal"
android:text="#string/no_participant_found"
android:textColor="#color/primaryText"
android:textSize="24sp"
android:visibility="gone" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_layout_participant_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ListView
android:id="#+id/participant_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#color/divider"
android:dividerHeight="1dp"
android:descendantFocusability="beforeDescendants"
android:drawSelectorOnTop="true"
android:headerDividersEnabled="true" />
</android.support.v4.widget.SwipeRefreshLayout>
<Button
android:id="#+id/submit_participant"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:textSize="20sp"
android:background="#color/colorAccent"/>
</LinearLayout>
participant_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="80dp"
android:clickable="true"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="horizontal"
android:padding="8dp">
<TextView
android:id="#+id/participant_list_item_id"
android:layout_width="0dp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="2"
android:textSize="20sp"
tools:text="M78" />
<TextView
android:id="#+id/participant_list_item_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_weight="5"
android:textSize="20sp"
android:fontFamily="sans-serif"
tools:text="Billu Barber" />
<EditText
android:id="#+id/participant_list_item_notes"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_weight="6"
android:textSize="12sp"
android:background="#drawable/participant_list_notes"
android:hint="#string/participant_notes"
android:inputType="textMultiLine"
android:scrollbars="none"
android:imeOptions="actionDone"
android:maxLength="50"
android:padding="4dp" />
<CheckBox
android:id="#+id/participant_list_item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp" />
</LinearLayout>
Found the answer atlast.
Don't use setOnItemClickListener for item click. Use item view click inside adapter method by using setOnClickListener(). Do things in onClick()
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View listItemView = convertView;
if (listItemView == null)
{
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.participant_list_item, parent, false);
}
listItemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(mContext, "click item",Toast.LENGTH_LONG).show();
}
});
Participant currentParticipant = getItem(position);
if(currentParticipant.getParticipantGender().equals("F"))
{
TextView idView = (TextView) listItemView.findViewById(R.id.participant_list_item_id);
idView.setTextColor(getColor(mContext, R.color.colorPrimary));
idView.setText(currentParticipant.getParticipantId());
}
else if(currentParticipant.getParticipantGender().equals("M"))
{
TextView idView = (TextView) listItemView.findViewById(R.id.participant_list_item_id);
idView.setTextColor(getColor(mContext, R.color.colorAccent));
idView.setText(currentParticipant.getParticipantId());
}
TextView nameView = (TextView) listItemView.findViewById(R.id.participant_list_item_name);
nameView.setText(currentParticipant.getParticipantName());
final EditText notesView = (EditText) listItemView.findViewById(R.id.participant_list_item_notes);
notesView.setText(currentParticipant.getParticipantNotes());
CheckBox checkedView = (CheckBox) listItemView.findViewById(R.id.participant_list_item_checkbox);
if (currentParticipant.getParticipantCheck().equals("1"))
{
checkedView.setChecked(true);
}
else
{
checkedView.setChecked(false);
}
return listItemView;
}
Remove this from participant_list_item.xml
android:descendantFocusability="blocksDescendants"
Thank you every one for the help.
<CheckBox
android:id="#+id/participant_list_item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:focusable="false"
android:clickable="false"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp" />
Try to replace your CheckBox and add this two line
android:focusable="false"
android:clickable="false"
Try this should work in participant_list_item.xml and remove from listview
android:descendantFocusability="blocksDescendants"
you have to do it like this
private EditText notes;
private CheckBox checkBox;
and put these above onclick
notes = (EditText) rootView.findViewById(R.id.participant_list_item_notes);
String text = notes.getText().toString();
Log.v("Participant Text",text);
checkBox = (CheckBox) rootView.findViewById(R.id.participant_list_item_checkbox);
edittext and check box are the view in the listview
hope this will help you
android:clickable=true
Make sure that you have added clickable property to true.
In participantslist.xml file:
<ListView
android:id="#+id/participant_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#color/divider"
android:dividerHeight="1dp"
android:descendantFocusability="beforeDescendants"
android:drawSelectorOnTop="true"
android:headerDividersEnabled="true"
android:clickable=true/>
And let me know if this suggestion was wrong?

Fragment doesn't respect match parent height when having listview row item has hidden view in it

I am having a weird issue in my app. In one of my app activity I am inflating three fragments in it with tabs. Everything works fine.
In the fragment in which I am having problem, I have listview which is inflated using adapter and data fro web service. This also works well. Now the problem is this the row which is inflated in adapter, has a hidden view which has visibility=gone in xml. On tap of imageview from that row I make that layout visible through java code. The problem is layout doesn't become visible on tap. I have even set breakpoint on the onClickListener of imageview and it does execute the line which changes the visibility from gone to visible. I am unable to understand what is causing this issue as I am using the same row xml with same data in other screen and there it is working perfectly.
UPDATE
I got to know what's causing this issue but don't know how to solve this. In my activity I am having three fragments. The view that I provided for fragment(in which fragment will be inflated) is causing main problem. I have set height width to match parent but it is not taking match parent height. If the fragment just includes normal views like textview, imageview then also fragment is shown properly.But the problem is if fragment consists of listview, then it only takes height of the custom row that is supplied to the listview. I am able to scroll complete listview in that space.
I don't understand what is causing this behaviour.
My Updated code.
Main layout xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<include layout="#layout/header_1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#000"
android:weightSum="3"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:weightSum="1"
android:orientation="horizontal"
android:id="#+id/lin_birds">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="#string/aves"
android:textColor="#ffffff"
android:gravity="center"
android:layout_gravity="center"
android:layout_weight="0.99"
android:id="#+id/fragment_aves"/>
<View
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="0.01"
android:background="#ffffff"
android:layout_marginTop="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:weightSum="1"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="#string/routes"
android:textColor="#ffffff"
android:gravity="center"
android:layout_gravity="center"
android:layout_weight="0.99"
android:id="#+id/fragment_routes"/>
<View
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="0.01"
android:background="#ffffff"
android:layout_marginTop="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/information"
android:textColor="#ffffff"
android:gravity="center"
android:layout_gravity="center"
android:id="#+id/fragment_information"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/frame_details" />
</LinearLayout>
</LinearLayout>
Main activity java code
public class ActivityRoutesDetails extends AppCompatActivity {
RelativeLayout rel_back;
TextView tv_title,tv_information,tv_routes,fragment_aves;
RoutesDataBean routesDataBean;
LinearLayout frame;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.routes_detail);
tv_title= (TextView)findViewById(R.id.tv_title);
tv_information= (TextView) findViewById(R.id.fragment_information);
tv_routes= (TextView) findViewById(R.id.fragment_routes);
fragment_aves= (TextView) findViewById(R.id.fragment_aves);
// frame= (LinearLayout) findViewById(R.id.frame_details);
routesDataBean= (RoutesDataBean)getIntent().getSerializableExtra("data");
tv_title.setText(routesDataBean.getDescrip1());
Fragment fragment=new FragmentRouteInside();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frame_details, fragment);
fragmentTransaction.commit();
fragment_aves.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragmentBirds=new FragmentRouteBirds();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_details, fragmentBirds);
fragmentTransaction.commit();
}
});
tv_information.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragmentRouteInformation = new FragmentRouteInformation();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_details, fragmentRouteInformation);
fragmentTransaction.commit();
}
});
tv_routes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment=new FragmentRouteInside();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_details, fragment);
fragmentTransaction.commit();
}
});
rel_back= (RelativeLayout) findViewById(R.id.rel_back);
rel_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
fragment bird xml
<?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="match_parent">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lv_bird"/>
</LinearLayout>
Fragment java code
public class FragmentRouteBirds extends Fragment {
AppSharedPreferences appSharedPreferences;
String REGISTER_URL="";
ListView lv_birds;
private ArrayList<BirdsDataBean> birdsUrlList;
boolean flag=false;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragments_birds, null);
appSharedPreferences=AppSharedPreferences.getsharedprefInstance(getActivity());
REGISTER_URL = "http://192.241.162.63/appvist/v1/routebird/"+appSharedPreferences.getRouteId();
birdsUrlList = new ArrayList<>();
lv_birds = (ListView) root.findViewById(R.id.lv_bird);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) lv_birds.getLayoutParams();
lp.height = LinearLayout.LayoutParams.MATCH_PARENT;
lv_birds.setLayoutParams(lp);
hitBirdsService();
return root;
}
private void hitBirdsService() {
class RegisterUser extends AsyncTask<String, Void, String> {
private ProgressDialog mDialog;
RequestClass ruc = new RequestClass();
String response = "";
#Override
protected void onPreExecute() {
super.onPreExecute();
mDialog = new ProgressDialog(getActivity());
mDialog.setMessage("Please Wait ...");
mDialog.setCancelable(false);
mDialog.show();
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
mDialog.dismiss();
parseBirdResponse(response);
//Toast.makeText(getActivity(), s, Toast.LENGTH_LONG).show();
}
#Override
protected String doInBackground(String[] params) {
response = RequestClass.GET(REGISTER_URL);
return response;
}
}
RegisterUser ru = new RegisterUser();
ru.execute();
}
public void parseBirdResponse(String response) {
//String descrip, String observaciones, String descrip_larga, String url_video, String url
try {
JSONObject jsonObject = new JSONObject(response);
Boolean error = jsonObject.getBoolean("error");
if (!error) {
JSONArray jsonArray = jsonObject.getJSONArray("birds");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonBirds = jsonArray.getJSONObject(i);
int idave=jsonBirds.getInt("idave");
String descrip = jsonBirds.getString("descrip");
String observaciones = jsonBirds.getString("observaciones");
String descrip_larga = jsonBirds.getString("descrip_larga");
String url_video = jsonBirds.getString("url_video");
String url = jsonBirds.getString("url");
String nombre_cientifico = jsonBirds.getString("nombre_cientifico");
int flag=jsonBirds.getInt("flag");
birdsUrlList.add(new BirdsDataBean(flag,idave,descrip, observaciones, descrip_larga, url_video, url, nombre_cientifico));
}
ScheduleTaskAdapter scheduleTaskAdapter = new ScheduleTaskAdapter(getActivity(), birdsUrlList);
lv_birds.setAdapter(scheduleTaskAdapter);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) lv_birds.getLayoutParams();
lp.height = 800;
lv_birds.setLayoutParams(lp);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public class ScheduleTaskAdapter extends BaseAdapter {
Context context;
LayoutInflater layoutInflater;
// List<InterestAndLanguageBean> interestAndLanguageBeans=new ArrayList<>();
List<BirdsDataBean> imageList = new ArrayList<>();
public ScheduleTaskAdapter(Context context, List<BirdsDataBean> imagesList) {
this.context = context;
this.imageList = imagesList;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return imageList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.routes_bird_row, parent, false);
holder.iv_birds = (ImageView) convertView.findViewById(R.id.iv_route_bird);
holder.frameLayout = (FrameLayout) convertView.findViewById(R.id.frame_route_bird);
holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.route_bird_detail_view);
holder.imageView = (ImageView) convertView.findViewById(R.id.iv_hide);
holder.iv_video = (ImageView) convertView.findViewById(R.id.iv_seen);
holder.iv_sound = (ImageView) convertView.findViewById(R.id.iv_video);
holder.tv_short_descript = (TextView) convertView.findViewById(R.id.tv_bird_name);
holder.tv_category = (TextView) convertView.findViewById(R.id.tv_scientific_name);
holder.tv_long_description = (TextView) convertView.findViewById(R.id.tv_description);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Uri myUri = Uri.parse(birdsUrlList.get(position).getUrl());
Glide.with(getActivity()).load(myUri).placeholder(R.drawable.birds).into(holder.iv_birds);
holder.tv_short_descript.setText(birdsUrlList.get(position).getDescrip());
holder.tv_long_description.setText(birdsUrlList.get(position).getDescrip_larga());
holder.tv_category.setText(birdsUrlList.get(position).getNombre_cientifico());
final ViewHolder finalHolder = holder;
holder.frameLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finalHolder.linearLayout.setVisibility(View.VISIBLE);
finalHolder.iv_sound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(birdsUrlList.get(position).getUrl_video())));
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
});
finalHolder.iv_video.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//hitBirdSeenService(birdsUrlList.get(position).getIdave());
// finalHolder.iv_video.setImageResource(R.drawable.eye_selected);
}
});
}
});
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finalHolder.linearLayout.setVisibility(View.GONE);
}
});
//Picasso.with(context).load(myUri).placeholder(R.drawable.image).into(holder.pic);
//malevich.load(helperTaskBeanList.get(position).getImage()).into(holder.pic);
return convertView;
}
}
static class ViewHolder {
ImageView iv_birds,imageView,iv_video,iv_sound;
FrameLayout frameLayout;
LinearLayout linearLayout;
TextView tv_short_descript,tv_category,tv_long_description;
}
}
And row layout for adapter
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/frame_route_bird"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:id="#+id/iv_route_bird"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="bottom"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="#string/name"
android:textSize="24sp"
android:textStyle="bold"
android:id="#+id/tv_bird_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#ffffff"
android:text="#string/bird_sub_category"
android:id="#+id/tv_scientific_name"/>
</LinearLayout>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone"
android:background="#ffffff"
android:id="#+id/route_bird_detail_view"
android:paddingBottom="120dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/bird_hidden_text"
android:textSize="20sp"
android:padding="20dp"
android:id="#+id/tv_description"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/eye110"
android:id="#+id/iv_seen"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/right"
android:layout_marginTop="25dp"
android:layout_marginLeft="15dp"
android:id="#+id/iv_arrow"/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ear"
android:layout_marginLeft="15dp"
android:id="#+id/iv_video"/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#D6D6D6"
android:layout_marginTop="20dp"
android:src="#drawable/up"
android:layout_gravity="center"
android:id="#+id/iv_hide"/>
</LinearLayout>
</LinearLayout>
Here are the screenshots of the problem
Comparing both images you can see that the hidden view appears in the space of the single row item and is scroll able completely in that space.
Better upshot were targeted at plus for android developers (Cristophe Beils):
"Your ListView must have its height set to match_parent or a fixed size, not wrap_content.
If you need to add another view fixed at the bottom of the ListView as a footer, you need to put both the ListView and the footer View in a vertical LinearLayout and set layout_height to 0dp and layout_weight to 1 on the ListView so that it takes the remaining vertical space."
Try fillViewPort=true on the list view.. But not sure what why yoi have a scroll view inside a list view.. Could be the issue too.
Try setting match_parent as the height of the listview
I suspect this will force the list view to have a defined height and fix your problem.

Unable to select an item in a ListView whose content is set by an Array Adapter

I couldn't select an item on ListView. I use Custom Adapter to set content of ListView. I have set OnItemClickListener of ListView. However, it didn't respond. I appriciate your help. Here is my code:
List Item (connections.xml):
<?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="wrap_content"
android:orientation="horizontal"
android:padding="6dip" >
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.90"
android:orientation="vertical" >
<TextView
android:id="#+id/connname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/acc1" />
<TextView
android:id="#+id/conntype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="9sp"
android:text="#string/acctype1" />
</LinearLayout>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
The screen which containt ListView (groupconnections.xml):
<?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:orientation="vertical">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
android:orientation="vertical"
android:id="#+id/textOperLayout">
<TextView
android:id="#+id/connectionsLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/connections"
android:textColor="#color/conn_text"
android:textSize="25sp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="15dp"
android:background="#color/conn_back"/>
<EditText
android:id="#+id/searchEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/searchhint"
android:layout_marginTop="10dp"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dip"
android:layout_marginBottom="50dip"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_above="#+id/textOperLayout"
android:id="#+id/listviewlayout">
<ListView
android:id="#+id/connectionlist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice" />
</LinearLayout>
<Button
android:id="#+id/addConnCommitButton"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="15dp"
android:text="#string/commitToAdd" />
</RelativeLayout>
The related activity (AddMoreConnections.xml):
public class AddMoreConnections extends Activity implements OnItemClickListener{
private ListView mainListView ;
private ArrayAdapter<AbstractHolder> listAdapter ;
private TextView searchConnTextView;
private Button commitButton;
private ArrayList<AbstractHolder> connlist;
private ArrayList<AbstractHolder> listNotOnView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Generate list View from ArrayLis
setContentView(R.layout.groupconnections);
addListenerOnSearchConnTextView();
//Initialize properties
mainListView = (ListView) findViewById( R.id.connectionlist );
mainListView.setOnItemClickListener(this);
// Create and populate a List of planet names.
listNotOnView = new ArrayList<AbstractHolder>();
connlist = new ArrayList<AbstractHolder>(5);
Iterator<AbstractHolder> iter = SocialRssModel.holders.values().iterator();
while(iter.hasNext())
connlist.add(iter.next());
// Create ArrayAdapter using the planet list.
listAdapter = new CustomAdapter(this,
R.layout.connlist, connlist);
mainListView.setAdapter( listAdapter );
}
public void addListenerToFinishButton(){
commitButton = (Button) findViewById(R.id.addConnCommitButton);
commitButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
public void addListenerOnSearchConnTextView(){
searchConnTextView = (TextView) findViewById(R.id.searchEditText);
searchConnTextView.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int listNotOnViewsize = listNotOnView.size();
int connlistsize = connlist.size();
for(int i= 0; i < connlistsize; i++){
if(!connlist.get(i).connNameContains(s.toString())){
listNotOnView.add(connlist.remove(i));
i--;
connlistsize--;
}
}
for(int i=0; i < listNotOnViewsize; i++){
if(listNotOnView.get(i).connNameContains(s.toString())){
connlist.add(listNotOnView.remove(i));
i--;
listNotOnViewsize--;
}
}
((CustomAdapter) listAdapter).updateList(connlist);
listAdapter.notifyDataSetChanged();
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
AbstractHolder temp = (AbstractHolder)mainListView.getItemAtPosition(arg2);
Intent i = new Intent(this, CategoryContentViewerController.class);
Bundle b = new Bundle();
b.putInt("AbstractHolderKey", temp.getId());
i.putExtras(b);
startActivity(i);
finish();
}
private class CustomAdapter extends ArrayAdapter<AbstractHolder> {
private ArrayList<AbstractHolder> connectionList;
public CustomAdapter(Context context, int textViewResourceId, ArrayList<AbstractHolder> connList) {
super(context, textViewResourceId, connList);
this.connectionList = new ArrayList<AbstractHolder>();
this.connectionList.addAll(connList);
}
public void updateList(ArrayList<AbstractHolder> connList){
connectionList.clear();
connectionList.addAll(connList);
}
private class ViewHolder {
TextView name;
TextView acctype;
CheckBox sel;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)(((Activity)this.getContext()).getSystemService(Context.LAYOUT_INFLATER_SERVICE));
convertView = vi.inflate(R.layout.connlist, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.connname);
holder.acctype = (TextView) convertView.findViewById(R.id.conntype);
holder.sel = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
AbstractHolder conn = connectionList.get(position);
holder.name.setText(conn.getName());
holder.acctype.setText(conn.getConntype());
holder.sel.setChecked(conn.isSelected());
holder.sel.setTag(conn);
return convertView;
}
}
}
It is related to checkbox item in connections.xml which is the list row item. If I remove checkbox, related listeners responses. I think, it may be thought there is no need a setItemOnClickListener(..) method since each row has a checkbox. Or it is a bug.

Categories

Resources