I've been trying to set my background colour on this list view and with following multiple tutorials it just doesn't come up, yet no errors are shown.
What I've got is an xml file called colours in the values folder:
<resources>
<drawable name="red_colour">#6D0E0E</drawable>
</resources>
Which links to an xml in the drawable folder:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_activated="true"
android:drawable="#drawable/red_colour">
</item>
</selector>
I've tried placing the following code android:background:#id"colourhighlight.xml"
literally everywhere based on various tutorials yet no luck?
Any advice? Please help...
I've got one main class with a context menu:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_contacts);
// The contacts from the contacts content provider is stored in this cursor
mMatrixCursor = new MatrixCursor(new String[] { "_id","name","photo","details"} );
// Adapter to set data in the listview
mAdapter = new SimpleCursorAdapter(getBaseContext(),
R.layout.lv_layout,
null,
new String[] { "name","photo","details"},
new int[] { R.id.tv_name,R.id.iv_photo,R.id.tv_details}, 0);
// Getting reference to listview
final ListView lstContacts = (ListView) findViewById(R.id.lst_contacts);
// Setting the adapter to listview
lstContacts.setAdapter(mAdapter);
// Creating an AsyncTask object to retrieve and load listview with contacts
ListViewContactsLoader listViewContactsLoader = new ListViewContactsLoader();
// Starting the AsyncTask process to retrieve and load listview with contacts
listViewContactsLoader.execute();
//Selecting and highlighting the elements in the listview
lstContacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setActivated(true);
}
});
//Creating the context menu and the options for it
listview = (ListView) findViewById(R.id.lst_contacts);
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listview.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
count = count+1;
mode.setTitle(count + " Contacts Selected");
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.contact_context_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch(item.getItemId()){
case R.id.delete_id:
Toast.makeText(getBaseContext(), count + " Contacts Deselected", Toast.LENGTH_SHORT).show();
count = 0;
mode.finish();
return true;
//break;
default:
return false; //break;
}
//return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
});
}
lv_layout.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"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/iv_photo"
android:maxHeight="80dp"
android:maxWidth="80dp"
android:adjustViewBounds="true"
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/textAppearanceLarge"
android:id="#+id/tv_name"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/iv_photo"
android:layout_toEndOf="#+id/iv_photo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/tv_details"
android:layout_below="#+id/tv_name"
android:layout_toRightOf="#+id/iv_photo"
android:layout_toEndOf="#+id/iv_photo" />
</RelativeLayout>
Main layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.ankhit.saveme.UserContacts"
android:background="#drawable/colourhighlight"
>
<ListView
android:id="#+id/lst_contacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/instructions"
android:id="#+id/button"
android:onClick="showInstructions"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked)
{
try
{
final int checkedCount = listView.getCheckedItemCount();
mode.setTitle(""+checkedCount);
if (checked)
{
adapter.setNewSelection(position, checked);
}
else
{
adapter.removeSelection(position);
}
adapter.notifyDataSetChanged();
}
catch (Exception e)
{
e.printStackTrace();
}
}
add these methods in your adapter class
private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();
public void setNewSelection(int position, boolean value)
{
mSelection.put(position, value);
}
public boolean isPositionChecked(int position)
{
Boolean result = mSelection.get(position);
return result == null ? false : result;
}
public Set<Integer> getCurrentCheckedPosition() {
return mSelection.keySet();
}
public void removeSelection(int position) {
mSelection.remove(position);
}
public void clearSelection() {
mSelection = new HashMap<Integer, Boolean>();
}
call this method inside getView() method of adapter it works properly
convertView.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
if (mSelection.get(position) != null)
{
convertView.setBackgroundColor(mContext.getResources().getColor(R.color.message_selector_holo_blue));
}
If you want highlighting selected row completely then change the background color of the convertview
lstContacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
}
});
Related
What i have tried :
listview_selector_focussed.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#008000"
android:endColor="#00FF00"
android:angle="90" />
</shape>
listview_selector_pressed.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#800000"
android:endColor="#FF0000"
android:angle="90" />
</shape>
listview_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_focused="true"
android:drawable="#drawable/listview_selector_focused" />
<item
android:state_pressed="true"
android:drawable="#drawable/listview_selector_pressed" />
</selector>
listview layout :
<ListView
android:listSelector="#color/listview_selector"
/>
I have tried this much.. but un-fortunately this thing wont work
I want to change the listview row color when i click and when i click on another row previous row must be deselected and regain original state
Adapter class :
public class ListViewAdapter extends BaseAdapter {
Context ctx;
ArrayList<HashMap<String, String>> arraylist;
LayoutInflater inflater;
TextView tvA, tvB;
String a, b;
String out;
ListViewAdapter(Context ctx, ArrayList<HashMap<String, String>> arraylist) {
this.ctx = ctx;
this.arraylist = arraylist;
}
#Override
public int getCount() {
return arraylist.size();
}
#Override
public Object getItem(int position) {
return arraylist.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listitem, parent, false);
tvA = (TextView) itemView.findViewById(R.id.tvA);
tvB = (TextView) itemView.findViewById(R.id.tvB);
tvA.setText(arraylist.get(position).get("a"));
a = arraylist.get(position).get("a");
b = arraylist.get(position).get("b");
return itemView;
}
listview adapter layout :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="horizontal">
<TextView
android:padding="5dp"
android:id="#+id/tvA"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minLines="2"
android:gravity="center_vertical"
android:text="Large Text"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#0052a5"
android:textSize="#dimen/font_large" />
<TextView
android:padding="5dp"
android:id="#+id/tvB"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#0052a5" />
</LinearLayout>
a good answer might be this one
You can set the onItemClick listener on your listview and in your adapter's getView() style the line.
PS: you will need to import the line background layout inside the adapter and set his background :)
try to something like my code
public class ReportActivity extends ActionBarActivity {
ListView listview;
Context mContext;
DatabaseHandler dbHandler;
ArrayList<ReportModel> mlist;
ReportAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report);
mContext=this;
listview=(ListView) findViewById(R.id.listView);
dbHandler=new DatabaseHandler(mContext);
mlist=dbHandler.getAllContacts();
Collections.reverse(mlist);
adapter = new ReportAdapter(mContext, R.layout.adapter_layout,
mlist);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
// Capture ListView item click
listview.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
// Capture total checked items
final int checkedCount = listview.getCheckedItemCount();
// Set the CAB title according to total checked items
mode.setTitle(checkedCount + " Selected");
// Calls toggleSelection method from ListViewAdapter Class
adapter.toggleSelection(position);
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.delete:
// Calls getSelectedIds method from ListViewAdapter Class
SparseBooleanArray selected = adapter
.getSelectedIds();
// Captures all selected ids with a loop
for (int i = (selected.size() - 1); i >= 0; i--) {
if (selected.valueAt(i)) {
ReportModel selecteditem = adapter
.getItem(selected.keyAt(i));
// Remove selected items following the ids
adapter.remove(selecteditem);
dbHandler.deleteContact(selecteditem.getId());
}
}
// Close CAB
mode.finish();
return true;
default:
return false;
}
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
adapter.removeSelection();
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
});
}
}
}
create xml file activity_main.xml in menu folder........
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/delete"
android:title="delete"/>
</menu>
I have a custom list view with layout contain two layouts, called upper and bottom.
upper layout contain spinner, set and remove buttons.
bottom layout contain text view and back button.
By default bottom layout is in GONE state and when the user clicks on set button upper layout is GONE and bottom is VISIBLE (clicking on back button in bottom layout will return upper bottom back).
my problem is spinner value is disappear after specific flows:
Flow 1:
Add two items
Click on SET button on the first item
Remove the second item
Click on 'Back' button on the first item
Flow 2:
Click on SET
Rotate screen
Click on Back
Just to be clear, spinner values are exist and if drop it down you'll found the names (and I have android:configChanges="keyboardHidden|orientation|screenSize" in my manifest).
So, why spinner value is disappear after those flows ?
Here is my code:
Names.java
public class Names
{
private String name;
private int nameIndex;
private Boolean isNameOnTop;
public Names()
{
name = "";
isNameOnTop = true;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNameIndex() {
return nameIndex;
}
public void setNameIndex(int nameIndex) {
this.nameIndex = nameIndex;
}
public Boolean getIsNameOnTop() {
return isNameOnTop;
}
public void setIsNameOnTop(Boolean isNameOnTop) {
this.isNameOnTop = isNameOnTop;
}
}
MainActivity.Java
public class MainActivity extends Activity
{
ArrayList<Names> namesArray = new ArrayList<>();
ListView lvNames;
ListviewAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvNames = (ListView) findViewById(R.id.listView);
adapter = new ListviewAdapter(this, namesArray);
lvNames.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_add)
{
namesArray.add(new Names());
adapter.notifyDataSetChanged();
return true;
}
return super.onOptionsItemSelected(item);
}
}
ListviewAdapter.java
public class ListviewAdapter extends BaseAdapter
{
public Activity context;
public LayoutInflater inflater;
private ArrayList<Names> namesID;
private boolean isDeleted;
public ArrayList<Names> getNamesID() {
return namesID;
}
public void setNamesID(ArrayList<Names> namesID) {
this.namesID = namesID;
}
// Constructor
public ListviewAdapter(Activity context, ArrayList<Names> names)
{
super();
setNamesID(names);
this.context = context;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return getNamesID().size();
}
#Override
public Names getItem(int position) {
return getNamesID().get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
public class ViewHolder
{
RelativeLayout relativeLayout_Upper;
RelativeLayout relativeLayout_Bottom;
Spinner spNames;
Button btn_set, btn_remove, btn_back;
TextView tvChosen;
int index;
}
#Override
public View getView(final int i, View view, final ViewGroup viewGroup) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.listview_row, null);
holder.relativeLayout_Upper = (RelativeLayout) view.findViewById(R.id.lvRow_upper_layout);
holder.relativeLayout_Bottom = (RelativeLayout) view.findViewById(R.id.lvRow_bottom_layout);
holder.spNames = (Spinner) view.findViewById(R.id.spNames);
holder.btn_set = (Button) view.findViewById(R.id.btn_set);
holder.btn_remove = (Button) view.findViewById(R.id.btn_remove);
holder.btn_back = (Button) view.findViewById(R.id.btn_back);
holder.tvChosen = (TextView) view.findViewById(R.id.tv_chosen);
view.setTag(holder);
}
else
holder = (ViewHolder) view.getTag();
holder.index = i;
if (isDeleted)
{
holder.spNames.setSelection(getItem(holder.index).getNameIndex());
holder.tvChosen.setText("Chosen: " + getItem(holder.index).getName());
if (getItem(holder.index).getIsNameOnTop())
{
holder.relativeLayout_Upper.setVisibility(View.VISIBLE);
holder.relativeLayout_Bottom.setVisibility(View.GONE);
}
else
{
holder.relativeLayout_Upper.setVisibility(View.GONE);
holder.relativeLayout_Bottom.setVisibility(View.VISIBLE);
}
}
// pop spinner names
String[] names = new String[]{"Tom", "Ben", "Gil", "Adam", "Moshe", "Adi", "Michael", "Yasmin", "Jessica", "Caroline", "Avi", "Yael"};
final ArrayAdapter<String> spNamesAdapter = new ArrayAdapter<String>
(view.getContext(), android.R.layout.simple_spinner_dropdown_item, names);
spNamesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.spNames.setAdapter(spNamesAdapter);
holder.spNames.setSelection(getItem(holder.index).getNameIndex());
holder.spNames.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//holder.spNames.setTag(position);
getItem(holder.index).setNameIndex(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
holder.btn_set.setTag(i);
holder.btn_set.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getItem(holder.index).setName(holder.spNames.getSelectedItem().toString());
int position = (Integer) v.getTag();
holder.tvChosen.setText("Chosen: " + getItem(position).getName());
holder.relativeLayout_Upper.setVisibility(View.GONE);
holder.relativeLayout_Bottom.setVisibility(View.VISIBLE);
getItem(holder.index).setIsNameOnTop(false);
}
});
// remove
holder.btn_remove.setTag(i);
holder.btn_remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = (Integer) v.getTag();
namesID.remove(position);
notifyDataSetChanged();
isDeleted = true;
}
});
// back
holder.btn_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.relativeLayout_Upper.setVisibility(View.VISIBLE);
holder.relativeLayout_Bottom.setVisibility(View.GONE);
getItem(holder.index).setIsNameOnTop(true);
}
});
return view;
}
}
activity_main.xml: contain ListView only.
upper_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spNames" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SET"
android:id="#+id/btn_set" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="REMOVE"
android:id="#+id/btn_remove" />
</LinearLayout>
bottom_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Chosen:"
android:id="#+id/tv_chosen"
android:layout_marginRight="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BACK"
android:id="#+id/btn_back" />
</LinearLayout>
listview_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lvRow_upper_layout">
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/upper_view"
android:id="#+id/includeRow_register"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp" />
</RelativeLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lvRow_bottom_layout"
android:visibility="gone">
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/bottom_view"
android:id="#+id/includeRow_showData"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp" />
</RelativeLayout>
</LinearLayout>
In your ListviewAdapter, change:
// back
holder.btn_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.relativeLayout_Upper.setVisibility(View.VISIBLE);
holder.relativeLayout_Bottom.setVisibility(View.GONE);
getItem(holder.index).setIsNameOnTop(true);
}
});
to:
// back
holder.btn_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.relativeLayout_Upper.setVisibility(View.VISIBLE);
holder.relativeLayout_Bottom.setVisibility(View.GONE);
getItem(holder.index).setIsNameOnTop(true);
notifyDataSetChanged();
// OR you can use this
// holder.spNames.requestLayout();
}
});
Whenever you click on "Add" in the method onOptionsItemSelected() you add an object new names() into the list, and in the Names class, the value of attribute name is set to ""
I guess that's why you getting an empty item in the spinner.
i'm new to android. I'm just trying to make one simple search functionality for my app. My app consists one ListView and one SearchView for search. My ListView contents are listed from a String list using custom adapter which is extends BaseAdapter
Now, what i'm trying to do is, i want to search any records from ListView For example, if i've some records like
aa abb ...
So, when i type some record name like a
The listview should listed the records which is started from a... I've referred something for this, from i got addTextChangedListener But, i don't know how to do this?
And, Can we do this same functionality with click of button
Has anyone having any idea on this? Thanks in advance
MainActivity.java
public class MainActivity extends ActionBarActivity {
ListView list ;
private SearchView mSearchView;
String [] a = {"aa","abb","cc","dd","ee","ff","gg","hh","ii","jj","kk","ll"};
String [] b = {"1","2","3","4","5","6","7","8","9","10","11","12"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSearchView = (SearchView) findViewById(R.id.search_view);
mSearchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
list.clearTextFilter();
} else {
Log.i("WSM",newText);
list.setFilterText(newText.toString());
}
return true;
}
});
list = (ListView) findViewById(R.id.lista);
list.setTextFilterEnabled(true);
list.setAdapter(new MyAdapter(this, a, b));
setupSearchView();
}
private void setupSearchView() {
mSearchView.setIconifiedByDefault(false);
mSearchView.setSubmitButtonEnabled(true);
mSearchView.setQueryHint("Search Here");
}
class SingleRow{
String ssid;
String pass;
public SingleRow (String ssid, String pass){
this.ssid = ssid;
this.pass = pass;
}
}
class MyAdapter extends BaseAdapter {
ArrayList<SingleRow> myList ;
Context c;
public MyAdapter (Context c, String[] ssid, String [] pass){
this.c = c;
myList = new ArrayList<SingleRow>();
for (int i = 0; i<ssid.length; i++){
myList.add(new SingleRow(ssid[i],pass[i]));
}
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return myList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return myList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) c.getSystemService(LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.singlerow, parent ,false);
TextView TV_ssid = (TextView) row.findViewById(R.id.ssid);
TextView TV_pass = (TextView) row.findViewById(R.id.pass);
SingleRow tmp = myList.get(position);
TV_ssid.setText(tmp.ssid);
TV_pass.setText(tmp.pass);
return row;
}
}
}
singlerow.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" >
<LinearLayout
android:layout_width="183dp"
android:layout_height="wrap_content"
android:layout_weight="0.82"
android:orientation="vertical" >
<TextView
android:paddingTop="10sp"
android:id="#+id/ssid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SSID: "
android:textAppearance="?android:attr/textAppearanceMedium"
android:typeface="monospace" />
<TextView
android:id="#+id/pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PASS:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingBottom="10sp"
android:typeface="monospace"/>
</LinearLayout>
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="56dp"
android:layout_height="49dp"
android:layout_gravity="center"
android:src="#drawable/abc_ic_menu_moreoverflow_normal_holo_light" />
</LinearLayout>
main_activity.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="vertical" >
<SearchView
android:id="#+id/search_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/lista"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
For searching feature I recommend you this component:
https://github.com/matessoftwaresolutions/AndroidFilterableList
This allows you to filter whatever you want in an extremely easy way. Change filters dynamically etc.
You would have to modify your code in order to adapt it to this way of working... but I think it worths.
Take a look to the sample!!
I hope this would help you.
Please, help me to find a solution how to set my checkboxes visible when I click on menu item "delete" and set invisible when I press "confirm" or "cancel".
I use SimpleCursorAdapter, cuz listview is filled from database with cursor loader...
here is my code:
MainActivity
public class MainActivity extends ActionBarActivity implements LoaderManager.LoaderCallbacks<Cursor>{
final String LOG = "phonebookLogs";
private static final int LOADER_ID = 1;
Intent intentAddContact;
ListView lvMain;
CheckBox cbxList;
MenuItem addContact, deleteContact, settings, expimp, confirmDelete, cancelDelete;
DB mDB;
SimpleCursorAdapter scAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//find ListView
lvMain = (ListView)findViewById(R.id.lvMain);
//calling for creating adapter
onCreateAdapter();
}
public void onCreateAdapter(){
String[] from = new String[] { DB.COL_LINK, DB.COL_SURNAME, DB.COL_NAME };
int[] to = new int[] { R.id.ivPhoto, R.id.tvSurname, R.id.tvName, };
if(to[0] == 0){
to[0] = R.drawable.default_contact;
}
scAdapter = new SimpleCursorAdapter(this, R.layout.item, null, from, to, 0);
lvMain.setAdapter(scAdapter);
// loader for reading data
getSupportLoaderManager().initLoader(LOADER_ID, null, this);
}
public void deleteRecord(){
//when MenuItem "Delete" is clicked
addContact.setVisible(false);
deleteContact.setVisible(false);
settings.setVisible(false);
expimp.setVisible(false);
confirmDelete.setVisible(true);
cancelDelete.setVisible(true);
}
public void onCancelDeleteRecord(){
////when MenuItem "cancel" is clicked
addContact.setVisible(true);
deleteContact.setVisible(true);
settings.setVisible(true);
expimp.setVisible(true);
confirmDelete.setVisible(false);
cancelDelete.setVisible(false);
}
public void confirmDeleteRecord(){
////when MenuItem "confirm" is clicked
addContact.setVisible(true);
deleteContact.setVisible(true);
settings.setVisible(true);
expimp.setVisible(true);
confirmDelete.setVisible(false);
cancelDelete.setVisible(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
//find all MenuItems
addContact = menu.findItem(R.id.addContact);
deleteContact = menu.findItem(R.id.deleteContact);
settings = menu.findItem(R.id.settings);
expimp = menu.findItem(R.id.expimp);
confirmDelete = menu.findItem(R.id.confirmDelete);
cancelDelete = menu.findItem(R.id.cancelDelete);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.addContact:
intentAddContact = new Intent(this, NewContact.class);
startActivity(intentAddContact);
break;
case R.id.deleteContact:
deleteRecord();
break;
case R.id.settings:
break;
case R.id.expimp:
break;
case R.id.confirmDelete:
confirmDeleteRecord();
break;
case R.id.cancelDelete:
onCancelDeleteRecord();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle bndl) {
return new MyCursorLoader(this, mDB);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
scAdapter.swapCursor(cursor);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
scAdapter.swapCursor(null);
}
}
DB.class and CursorLoader.class are fine, simple query and loading.
and my XML-file for ListView
<?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:layout_margin="#dimen/margin"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin"
android:layout_weight="0.5"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin"
android:orientation="vertical" >
<ImageView
android:id="#+id/ivPhoto"
android:layout_width="#dimen/imageWidth"
android:layout_height="#dimen/imageHeight"
android:layout_gravity="center_vertical"
android:contentDescription="#string/photoDescription"
android:src="#drawable/default_contact" />
</LinearLayout>
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/margin"
android:text=""
android:textSize="#dimen/textSize" />
<TextView
android:id="#+id/tvSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/margin"
android:text=""
android:textSize="#dimen/textSize" />
</LinearLayout>
<CheckBox
android:id="#+id/cbxList"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="#dimen/margin"
android:layout_weight="0.2"
android:visibility="invisible" />
</LinearLayout>
I have read a lot already, but how to realize it without creating my own adapter still can't get...
will be thanksfull to any help in finding solution!
You can set your checkbox as visible with: cbxlist.setVisibility(View.VISIBLE);
And if you want it completely gone, use (ie make it look as if it was never added): cbxlist.setVisibility(View.GONE);
If you want it to be just invisible, but still occupy the same space: cbxlist.setVisibility(View.INVISIBLE);
Edit:
for(int i = 0; i < lvMain.getChildCount(); i++) {
lvMain.getChildAt(i).findViewById(R.id.cbxList).setVisibility(View.VISIBLE);
}
That should set all of your checkbox's to visible. Similarly call for View.INVISIBLE when required.
If you want to change visibility of each checkbox in your listview (assuming your XML is your list row view), you need to create your custom adapter and override getView method.
In there, you will do the following:
#Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
cBox = (CheckBox ) convertView.findViewById(R.id.cbxList);
cBox.setVisibility(View.GONE);
return convertView;
}
Who have simple example for Drop Down Menu by Button Click?
Need make list for installed programs and select for starting.
Menu listMenu = null;
listMenu.add("quasatron"); listMenu.add("magnetron"); listMenu.add("atarrilix");
onCreateOptionsMenu(listMenu);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.popup_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.food:
makeToast("food","","","");
return true;
case R.id.other:
makeToast("other","","","");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And XML file of popup_menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/food" android:title="Food" />
<item android:id="#+id/other" android:title="Other" />
</menu>
This example is create myself to when you select any item in dropdown (spinner) list at a moment image display rightside on base on select item. so this example help to you.
MainActivity.java
public class MainActivity extends Activity implements OnClickListener, OnItemSelectedListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] technology = {"PHP", "Ruby", "Java", "SQL"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, technology);
final Spinner spinnertech = (Spinner) findViewById(R.id.spinnertech);
spinnertech.setAdapter(adapter);
spinnertech.setOnItemSelectedListener(this);
// Spinner Start....
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int position = arg0.getSelectedItemPosition();
ImageView ivtech = (ImageView) findViewById(R.id.imgtech);
if(position == 0) {
ivtech.setImageResource(R.drawable.php);
} else if(position == 1) {
ivtech.setImageResource(R.drawable.ruby);
} else if(position == 2) {
ivtech.setImageResource(R.drawable.java);
} else if(position == 3) {
ivtech.setImageResource(R.drawable.sql);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
// Spinner End....
}
main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/TableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imgtech"
android:layout_width="80dp"
android:layout_height="60dp"
android:layout_marginRight="10dp" />
<Spinner
android:id="#+id/spinnertech"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1" />
</LinearLayout>
</TableLayout>