ListView within ListView in android application - android

I am doing a project that currently requires a list and when a particular item from the list is clicked another list activity appears. Is there any solution as to how to move from one listview activity to another. I am currently using http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/ as a reference for my ListView.

One solution would be:
Create the second ListView activity and implement the first ListActivity with a OnItemClickListener that opens the second ListViewActivity by using an regular Intent.
listView = (ListView) findViewById(R.id.mylistview);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long arg3) {
Intent intent = new Intent(FirstListActivity.this, SecondListActivity.class);
Bundle bundle = new Bundle();
bundle.putString("pos", position);
intent.putExtras(bundle);
startActivity(intent);
}
});
UPDATE:
I have wrote an simple example list application. You may use it for insperation. The code that could open the Second list activity is included, but commented out. If you get this example to run, you are getting closer. Then you can try comment out the Intent code.
package com.adpog.listviewexample;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the ListView resource.
ListView mainListView = (ListView) findViewById( R.id.my_list );
// Set the Adapter as the ListView's adapter.
mainListView.setAdapter( new BaseAdapter(){
// Create and populate a List of planet names.
String[] planets = new String[] {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };
#Override
public int getCount() {
return planets.length;
}
#Override
public Object getItem(int pos) {
return planets[pos-1];
}
#Override
public long getItemId(int pos) {
return 0;
}
#Override
public View getView(int pos, View view, ViewGroup viewgroup) {
if(view == null){
/**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
/>
<TextView
android:id="#+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text not set" />
</LinearLayout>
*/
view = View.inflate(getApplicationContext(), R.layout.row, null);
}
return view;
}
});
mainListView.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView< ? > arg0, View arg1, int arg2, long arg3) {
Log.d("ListView", "Pos: " + arg2 + ", long : "+arg3);
Toast.makeText(getApplicationContext(), "Test " + arg2, Toast.LENGTH_SHORT).show();
/* Alternative way; opens a new Activity
Intent intent = new Intent(this, SecondListViewActivity.class);
intent.putExtra("position", pos);
startActivity(intent);
*/
}
#Override
public void onNothingSelected(AdapterView< ? > arg0) {
}
});
/**
* Implement an action for each item click.
*/
mainListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView< ? > arg0, View arg1, int arg2, long arg3) {
Log.d("ListView", "OnClickPos: " + arg2 + ", long : "+arg3);
Toast.makeText(getApplicationContext(), "Test " + arg2, Toast.LENGTH_SHORT).show();
}
});
}
}

api provides ExpandableListView . your requirement looks very close to it

Related

Adding onClickListener to specific items in ListView

please I'm trying to add understand how to add onItemClickListener to the followng code such that when "Smartphone Plans" is clicked, its activity starts and so on. I've seen other questions on StackOverflow relating to this question but do not understand how to go about them.
I've already added an onItemClickListener but do not understand how to set it to specific list items.
here is the code
package devchuks.com.rechargeit;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.app.ListActivity;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class EtisalatData extends AppCompatActivity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_etisalat_data);
listView = (ListView) findViewById(R.id.list);
String[] values = new String[] {
"Smartphone Plans",
"Internet Bundles",
"Weekend Plans",
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
}
You can define your listView.setOnItemClickListener like this to go to different activity for clicking different element.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String item = listView.getItemAtPosition(position);
Toast.makeText(this,"You selected : " + item,Toast.LENGTH_SHORT).show();
if(position==0) {
// Do your code for clicking "Smartphone Plans" example
// startActivity(new Intent(getApplicationContext(),SmartphonePlans.class));
}
else if(position==1) {
// Do your code for clicking "Internet Bundles". example
// startActivity(new Intent(getApplicationContext(),InternetBundles.class));
}
else if(position==2) {
// Do your code for clicking "Weekend Plans". example
//startActivity(new Intent(getApplicationContext(),WeekendPlans.class));*/
}
});
onItemClick will be called whenever any of the list items are clicked. The position will be the position of the view in the Adapter.
Please refer -
How to handle the click event in Listview in android?
Thanks
Sriram
try this:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String text = values[position];
if(text.equals("Smartphone Plans")){ //your specific list item text
Intent i = new Intent(MainActivity.this, AnotherActivity.class);
i.putExtra("TEXT", text);
startActivity(i);
}
}
}
If this helps and is your concern
`
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String data = values[position];
switch(data){
case "Smartphone Plans":
// do somwthing
break;
// similarly for other two values.
}
}
});`
Below method give you a position of a clicked row. Take advantage of that and value from your array.
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Move your values array to member array or make it final to use
// in Anonymous class
final String selectedValue = values[position];
Intent intent = null
// Now add case and start activity
if("Smartphone Plans".equals(selectedValue)) {
intent = new Intent(EtisalatData.this, SmartPhonePlan.class);
}
else if("other Plans".equals(selectedValue)){
// other action
}
//... more cases and at the end start your activity if its not null
startActivity(intent);
}

Extracting 2 strings from 2 listviews in adroidstudio

I have 2 listviews in 1 activity (1 on the left 1 on the right of screen). I want 2 strings extracted from both list views and use them else where in the code (either in the same activity but different class or different activity). I have tried assigning clicked items to a public variable and then posting them on a text view bit I see nothing. Please help or suggest another better way. Here is a sample of my code:
public class Tabs extends Activity {
String CF =""; //Convert To
String CT =""; //Convert From
populateListView(); //function that populates my listview (not shown here)
ListView listMassFrom = (ListView) findViewById(R.id.ListViewMassFrom);
ListView listMassTo = (ListView) findViewById(R.id.ListViewMassTo);
listMassFrom.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
String convFrom = ((TextView) viewClicked).getText().toString();
CF = convFrom;
}
});
listMassTo.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
String convTo = ((TextView) viewClicked).getText().toString();
CT = convTo;
}
});
//Test to see if the two string were extracted from the onItemClick method:
TextView t1 = (TextView) findViewById(R.id.test1);
t1.setText(CT);
TextView t2 = (TextView) findViewById(R.id.test2);
t2.setText(CF);
}
}
You should access items via adapters using the parameter position.
Supposing your adapter contains Strings (Otherwise adapt it to your data model):
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
CF = <listMassFrom_adapter>.getItem(position);
}
EDIT:
Example tested and working. You can see a toast when items are selected.
package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.example.testbutton.R;
public class Tabs extends Activity {
final static String[] MassUnits = { "kg", "g", "mg", "lb", "lbm", "slug" };
String CF = ""; // Convert To
String CT = ""; // Convert From
ListView listMassFrom, listMassTo;
ListAdapter adapterFrom, adapterTo;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
// Find views
listMassFrom = (ListView) findViewById(R.id.lvFrom);
listMassTo = (ListView) findViewById(R.id.lvTo);
// Create list of items
adapterFrom = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, MassUnits);
listMassFrom.setAdapter(adapterFrom);
adapterTo = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, MassUnits);
listMassTo.setAdapter(adapterTo);
listMassFrom
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent,
View viewClicked, int position, long id) {
String convFrom = (String) adapterFrom
.getItem(position);
Toast.makeText(getApplicationContext(),
"From: " + convFrom, Toast.LENGTH_SHORT).show();
}
});
listMassTo
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent,
View viewClicked, int position, long id) {
String convTo = (String) adapterTo.getItem(position);
Toast.makeText(getApplicationContext(),
"To: " + convTo, Toast.LENGTH_SHORT).show();
}
});
}
}
Layout tabs.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
android:id="#+id/lvFrom"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ListView
android:id="#+id/lvTo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
I think it is better to use spinners, but you can adapt the code if that's the case.

Simple list item multiple choice not selecting items

I've created an array list and display it in a list view with simple list item multiple choice but i cannot check or tick the items on the list, when i click on the items nothing happens. Please check my code below and tell me what i am doing wrong.
package com.example.arrays;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;
public class MainActivity extends Activity {
ListView showList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView show = (TextView)findViewById(R.id.txtShow);
final Random generate = new Random();
showList = (ListView)findViewById(R.id.listView1);
final String[] myAttraction = new String[4];
myAttraction[0]= "Walter Sisulu National Botanical Garden ";
myAttraction[1]= "Coca-Cola Dome";
myAttraction[2]= "Promusica Theatre";
myAttraction[3]= "Unisa Science Campus";
Button arrays = (Button)findViewById(R.id.button1);
arrays.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
/*int random = generate.nextInt(4);
String display = myAttraction[random];
show.setText(display);*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, myAttraction);
showList.setAdapter(adapter);
}
});
showList.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "long clicked pos: " + pos, Toast.LENGTH_LONG).show();
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Add a OnItemClickListener like this to check/uncheck the CheckedTextView when user click on an item
showList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// change the checkbox state
CheckedTextView checkedTextView = ((CheckedTextView)view);
checkedTextView.setChecked(!checkedTextView.isChecked());
}
});
change your code as following....
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, myAttraction);
showList.setAdapter(adapter);
arrays.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
/*int random = generate.nextInt(4);
String display = myAttraction[random];
show.setText(display);*/
}
});
showList.setOnItemClickListener(new OnItemClickListener() {
public boolean onItemClick(AdapterView<?> arg0, View arg1, int pos, long id) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "long clicked pos: " + pos, Toast.LENGTH_LONG).show();
return true;
}
});
Set choiceMode property of your list to multipleChoice. I'm implementing multiple choice list in such a way in my applications, and it surely works.
Change this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_multiple_choice, myAttraction);
showList.setAdapter(adapter);
To this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_multiple_choice, myAttraction);
showList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
showList.setAdapter(adapter);
Just Add this line if you have a ListView and have selected simple_list_item_multiple_choice and you are unable to interact with the checkbox.
YourListViewName.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

Android ListView - how to react to item clicks?

Let's say that I have a ListView with some items. I want to make it so when the user clicks on an item, the app pops up a Toast containing the name of the item. For example, when the user clicks "Apple", they are presented with the toast: "You ate Apple." How can I do so?
Standard way it to use .setOnItemClickListener().
you can set Item Click Listener on List View i.e.
listvw=(ListView)findViewById(R.id.listviewid);
listvw.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
Toast.makeText(ActivityName.this, "Text message", Toast.LENGTH_SHORT).show();
}
});
use like that
ActivityListView .java
package com.sunil;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ActivityListView extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an array of Strings, that will be put to our ListActivity
String[] namesArray = new String[] { "Linux", "Windows7", "Eclipse",
"Suse", "Ubuntu", "Solaris", "Android", "iPhone" };
/* Create an ArrayAdapter, that will actually make the Strings above
appear in the ListView */
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, namesArray));
}
#Override
protected void onListItemClick(ListView l, View v,
int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "You selected: " + keyword,
Toast.LENGTH_SHORT).show();
}
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
Try this code
listview=(ListView)findViewById(R.id.listid);
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,long arg3) {
// to set your list item or name
//here list is your list that you set in your adapter
list.get(pos);
}
});

How to refresh the ListView in Drag-Sort ListView?

I have implement Drag-Sort ListView(DSLV) and LazyList together in my Project, I download the Demo LazyList and Drag-Sort ListView from github then integrate and modify as per my requirement ,
I use DSLV for drag and sort the items of ListView and LazyList for Displaying Image from URL,
i just implement "Basic usage playground" from DSLV for drag and sort,
I have implement search in TestBedDSLV.java, but the problem is that when I search the Content from the list, but i can't update the list, i have tried notifyDataSetChanged method but it not works, generally we create new adapter and pass it to listview like lv.setAdapter(adapter) , but here they just set ListAdapter, so we cant do lv.setAdapter(adapter)
TestBedDSLV.java
package com.mobeta.android.demodslv;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.Button;
import android.widget.EditText;
import com.mobeta.android.dslv.DragSortController;
import com.mobeta.android.dslv.DragSortListView;
public class TestBedDSLV extends FragmentActivity {
private int mNumHeaders = 0;
private int mNumFooters = 0;
private int mDragStartMode = DragSortController.ON_DOWN;
private boolean mRemoveEnabled = false;
private int mRemoveMode = DragSortController.FLING_RIGHT_REMOVE;
private boolean mSortEnabled = true;
private boolean mDragEnabled = true;
private String mTag = "dslvTag";
Button search;
EditText search_customer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_bed_main);
search = (Button) findViewById(R.id.btn_search);
search_customer = (EditText) findViewById(R.id.search_product);
search_customer.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// here is logic for refresh the list View
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.test_bed, getNewDslvFragment(), mTag).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mode_menu, menu);
return true;
}
private Fragment getNewDslvFragment() {
DSLVFragmentClicks f = DSLVFragmentClicks.newInstance(mNumHeaders,
mNumFooters);
f.removeMode = mRemoveMode;
f.removeEnabled = mRemoveEnabled;
f.dragStartMode = mDragStartMode;
f.sortEnabled = mSortEnabled;
f.dragEnabled = mDragEnabled;
return f;
}
}
DSLVFragmentClicks.java
package com.mobeta.android.demodslv;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.os.Bundle;
import android.widget.Toast;
public class DSLVFragmentClicks extends DSLVFragment {
public static DSLVFragmentClicks newInstance(int headers, int footers) {
DSLVFragmentClicks f = new DSLVFragmentClicks();
Bundle args = new Bundle();
args.putInt("headers", headers);
args.putInt("footers", footers);
f.setArguments(args);
return f;
}
AdapterView.OnItemLongClickListener mLongClickListener =
new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String message = String.format("Long-clicked item %d", arg2);
Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
return true;
}
};
#Override
public void onActivityCreated(Bundle savedState) {
super.onActivityCreated(savedState);
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String message = String.format("Clicked item %d", arg2);
Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
}
});
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String message = String.format("Long-clicked item %d", arg2);
Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
DSLVFragment.java
package com.mobeta.android.demodslv;
import java.util.ArrayList;
import java.util.Arrays;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import com.mobeta.android.dslv.DragSortController;
import com.mobeta.android.dslv.DragSortListView;
public class DSLVFragment extends ListFragment {
ArrayAdapter<String> adapter;
private String[] array;
public static ArrayList<String> list;
private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() {
#Override
public void drop(int from, int to) {
if (from != to) {
String item = adapter.getItem(from);
adapter.remove(item);
adapter.insert(item, to);
}
}
};
private DragSortListView.RemoveListener onRemove = new DragSortListView.RemoveListener() {
#Override
public void remove(int which) {
adapter.remove(adapter.getItem(which));
}
};
protected int getLayout() {
return R.layout.dslv_fragment_main;
}
/**
* Return list item layout resource passed to the ArrayAdapter.
*/
protected int getItemLayout() {
return R.layout.list_item_handle_right;
}
private DragSortListView mDslv;
private DragSortController mController;
public int dragStartMode = DragSortController.ON_DOWN;
public boolean removeEnabled = false;
public int removeMode = DragSortController.FLING_RIGHT_REMOVE;
public boolean sortEnabled = true;
public boolean dragEnabled = true;
public static DSLVFragment newInstance(int headers, int footers) {
DSLVFragment f = new DSLVFragment();
Bundle args = new Bundle();
args.putInt("headers", headers);
args.putInt("footers", footers);
f.setArguments(args);
return f;
}
public DragSortController getController() {
return mController;
}
/**
* Called from DSLVFragment.onActivityCreated(). Override to set a different
* adapter.
*/
public void setListAdapter() {
array = getResources().getStringArray(R.array.jazz_artist_names);
list = new ArrayList<String>(Arrays.asList(array));
adapter = new ArrayAdapter<String>(getActivity(), getItemLayout(),
R.id.text, list);
setListAdapter(adapter);
}
/**
* Called in onCreateView. Override this to provide a custom
* DragSortController.
*/
public DragSortController buildController(DragSortListView dslv) {
DragSortController controller = new DragSortController(dslv);
controller.setDragHandleId(R.id.drag_handle);
controller.setClickRemoveId(R.id.click_remove);
controller.setRemoveEnabled(removeEnabled);
controller.setSortEnabled(sortEnabled);
controller.setDragInitMode(dragStartMode);
controller.setRemoveMode(removeMode);
return controller;
}
/** Called when the activity is first created. */
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDslv = (DragSortListView) inflater.inflate(getLayout(), container,
false);
mController = buildController(mDslv);
mDslv.setFloatViewManager(mController);
mDslv.setOnTouchListener(mController);
mDslv.setDragEnabled(dragEnabled);
return mDslv;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mDslv = (DragSortListView) getListView();
mDslv.setDropListener(onDrop);
mDslv.setRemoveListener(onRemove);
Bundle args = getArguments();
int headers = 0;
int footers = 0;
if (args != null) {
headers = args.getInt("headers", 0);
footers = args.getInt("footers", 0);
}
setListAdapter();
}
}
test_bed_main.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="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/search_lay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal" >
<EditText
android:id="#+id/search_product"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:background="#drawable/search_back"
android:hint="Enter Firstname"
android:imeOptions="actionSearch"
android:inputType="text"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:paddingTop="2dp"
android:visibility="visible" />
<Button
android:id="#+id/btn_search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="2.5"
android:text="CANCEL"
android:textColor="#155280"
android:textSize="14sp"
android:textStyle="bold"
android:visibility="visible" />
</LinearLayout>
<FrameLayout
android:id="#+id/test_bed"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<!-- We will add the DSLVFragment inside the FrameLayout in code -->
</LinearLayout>
and other require class can be download from github link that i given above.....
Actually if you see your code closly there's a method that you have mention
public void setListAdapter() {
array = getResources().getStringArray(R.array.jazz_artist_names);
list = new ArrayList<String>(Arrays.asList(array));
adapter = new ArrayAdapter<String>(getActivity(), getItemLayout(),
R.id.text, list);
setListAdapter(adapter);
}
So as you said :
"generally we create new adapter and pass it to listview like lv.setAdapter(adapter) , but here they just set ListAdapter, so we cant do lv.setAdapter(adapter)"
Can't you easily do this by easily generating an array based on your search and set it like the code ??
list = new ArrayList<String>(Arrays.asList(array));
adapter = new ArrayAdapter<String>(getActivity(), getItemLayout(),
R.id.text, list);
setListAdapter(adapter);
EDIT: I think you are asking the wrong question. Your main concern should be how to communicate between activity and fragments...since your search functionality is in activity and list is in fragment. So you need to setup an interface that basically passes on the search string to your fragment and there you can make an array and set it the same way you are doing right now.
Read this SO question and answer. In the answer you'll find one similar approach on passing data between activity and fragment.
Edit 2: Your main concern is to communicate from activity to fragment for this..declare an interface in your activity like this:
public interface FragmentCommunicator{
public void passDataToFragment(String someValue);
}
then call the interface at your text change listener..for example
FragmentCommunicator mfragmentCommunicator;
//your onCreate function
{
//your textchangelistenr
{
onTextChanged call
if(mfragmentCommunicator != null)
mfragmentCommunicator.passDataToFragment(Pass your string here)
}
then make your fragement implement this interface like
public class someFragment extends Fragment implements FragmentCommunicator
{
//this is your rest of the fragment
#Override
public void passDataToFragment(String somevalue)
{
//This function will get fired each time your text is changed since in your activity you are calling this same function in your textchange listener. the String somevalue will be the string that you passed from your activity
}
//rest of the code
.
.
.
//Don't forget to initialize your interface in fragment itself. Do this in your onAttach
like this
#Override
public void onAttach(Activity activity){
super.onAttach(activity);
context = getActivity();
((MainActivity)context).fragmentCommunicator = this;
}
}
}
You can refer this link for any further clarifications. Check only the implementation of FragmentCommunicator
If you are using array as the base arraylist for adapter, just update this array and call adapater.notifyDataSetChanged(). This will update DSLV for sure as I have used same approach in one of my project.

Categories

Resources