I have implemented a ListView with headers from sqlite in a separate project which is like this:
When I implemented onClick() in a position of Slider Menu, It is displayed in Slider Menu rather than in the background View
onCreate() code snippet
if(position == 3)
{
mAdapter = new MyCustomAdapter();
mAdapter.addSeparatorItem(q.get(0).getA_name());
mAdapter.addItem(q.get(0).getAS_name());
for (int i = 1; i < 460; i++) {
if (!(q.get(i).getA_name().trim().equals(q.get(i-1).getA_name().trim()))) {
mAdapter.addSeparatorItem(q.get(i).getA_name());
c++;
}
mAdapter.addItem(q.get(i).getAS_name());
}
setListAdapter(mAdapter);
toggleMenu(arg1);
}
Adapter class
//Adapter Class
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparatorItem(final String item) {
mData.add(item);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
public int getCount() {
return mData.size();
}
public String getItem(int position) {
return mData.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.activity_main1, null);
holder.textView = (TextView)convertView.findViewById(R.id.text);
holder.textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder nointernetconnection = new AlertDialog.Builder(
temp);
nointernetconnection
.setIcon(R.drawable.ic_launcher)
.setTitle(q.get(position-1).getAS_name())
.setMessage(q.get(position-1).getDesc_art())
.setCancelable(true)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg,
int arg1) {
}
});
AlertDialog a = nointernetconnection.create();
a.show();
}
});
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.activity_main2, null);
holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
So, MyCustomAdapter class is dispalying the list in slider menu. But this list should not be displayed in slider menu rather it should be displayed behind slider menu. I hope the problem is clear. Please tell my mistake.
The setlistadapter method you are calling is being called for the slider rather than your list view on the activity/fragment in the background.
do a ListView list = (ListView) getactivity().findViewById(R.id.yourListViewName) or something like this, and then set the list adapter.
Related
I am new to android and working on web view demo, I have implemented it. But the problem with me is when I check any check box and scrolling the list view , The checked position changes.
I have tried some links from stack over flow with no luck, SO I request someone can help me to resolve this issue, please
My adapter is as below,
adapter
public class FilterAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
public FilterAdapter(Context context) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparatorItem(final String item) {
mData.add(item);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
public int getCount() {
return mData.size();
}
public String getItem(int position) {
return mData.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.raw_filter, null);
holder.textView = (TextView) convertView.findViewById(R.id.tv_filter);
holder.chk_filter = (CheckBox) convertView.findViewById(R.id.chk_filter);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.raw_headr, null);
holder.textView = (TextView) convertView.findViewById(R.id.tv_hdr);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
class ViewHolder {
public TextView textView;
public CheckBox chk_filter;
}
add clickListener in your getView() method, it will fix your set checked box in the entire listView like this:
checkBoxSelected.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
instrumentClickListener.onCheckBoxClick(instrumentDTO, checkBoxSelected.isChecked(), position);
Log.d(TAG, "checked instruments is check: " + checkBoxSelected.isChecked() + " \ninstrument: " + position);
}
});
I have an Adapter class that implements ListView with headers.
//Adapter Class
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private static final int TYPE_GRAY_SEPARATOR = 2;
private TreeSet<Integer> mGraySeparatorsSet = new TreeSet<Integer>();
private ArrayList<ContentWrapper> mData = new ArrayList<ContentWrapper>();
private LayoutInflater mInflater;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
public MyCustomAdapter(Context context)
{
mInflater = LayoutInflater.from(context);
}
public void addItem(ContentWrapper value) {
mData.add(value);
notifyDataSetChanged();
}
public void addSeparatorItem(ContentWrapper value) {
mData.add(value);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
public ContentWrapper getItem(int position) {
return mData.get(position);
}
#Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
public int getCount() {
return mData.size();
}
public long getItemId(int position) {
Log.v("getItemId Position", ""+position);
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.white, null);
holder.textView = (TextView)convertView.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.black, null);
holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
} holder.textView.setText(mData.get(position).getItem());
if (type == TYPE_ITEM) {
holder.textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder x = new AlertDialog.Builder(getActivity());
x.setIcon(R.drawable.ic_launcher)
.setTitle(mData.get(position).getItem())
.setMessage(mData.get(position).getItemDescription())
.setCancelable(true)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg,
int arg1) {
}
});
AlertDialog a = x.create();
a.show();
}
return null;
}
});
} else {
holder.textView.setOnClickListener(null);
}
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
In this I truly understand the implementation of getView method (ViewHolder,convertView,etc).
The above code runs smoothy but I do not understand that why we are using the methods
addItem
addSeparatorItem
getItemViewType
getViewTypeCount
getCount
getItemId
Can Anyone explain me the scenario clearly !
Thanks in advance..
These all are the call back methods. You can have the clear definition of these methods in below mentioned link.
http://developer.android.com/reference/android/widget/BaseAdapter.html
Let me give you an example of getItem()
Whenever new listview row is created, adapte willl get item details from this call back method.
Similarly whenever list is created, getCount() is called.
Suppose you have 12 items in listview. But you pass 10 in getCount(). it will show only ten items in list view.
The below code is running the custom listview implemented in a seperate project.
public class MainActivity extends ListActivity implements OnTouchListener{
private MyCustomAdapter mAdapter;
Activity temp = this;
String []s = new String[500];
ArrayList<GS> q = new ArrayList<GS>();
ListView lv;
int count=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DBAdapter db = DBAdapter.getDBAdapter(getApplicationContext());
if (!db.checkDatabase())
{
db.createDatabase(getApplicationContext());
}
db.openDatabase();
q = db.getData();
mAdapter = new MyCustomAdapter();
mAdapter.addSeparatorItem(new ContentWrapper(q.get(0).getA_name(),null));
mAdapter.addItem(new ContentWrapper(q.get(0).getAS_name(), q.get(0).getDesc_art()));
for (int i = 1; i < 460; i++) {
if (!(q.get(i).getA_name().trim().equals(q.get(i-1).getA_name().trim()))) {
mAdapter.addSeparatorItem(new ContentWrapper(q.get(i).getA_name(), null));
}
mAdapter.addItem(new ContentWrapper(q.get(i).getAS_name(), q.get(i).getDesc_art()));
}
setListAdapter(mAdapter);
}
//Adapter Class
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList<ContentWrapper> mData = new ArrayList<ContentWrapper>();
private LayoutInflater mInflater;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(ContentWrapper value) {
mData.add(value);
notifyDataSetChanged();
}
public void addSeparatorItem(ContentWrapper value) {
mData.add(value);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
public ContentWrapper getItem(int position) {
return mData.get(position);
}
#Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
public int getCount() {
return mData.size();
}
public long getItemId(int position) {
Log.v("getItemId Position", ""+position);
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.activity_main1, null);
holder.textView = (TextView)convertView.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.activity_main2, null);
holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
count++;
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
} holder.textView.setText(mData.get(position).getItem());
if (type == TYPE_ITEM) {
holder.textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder x = new AlertDialog.Builder(temp);
Log.v("position",""+position);
x.setIcon(R.drawable.ic_launcher)
// .setTitle(q.get(position-count).getAS_name())
.setTitle(mData.get(position).getItem())
// .setMessage(q.get(position-count).getDesc_art())
.setMessage(mData.get(position).getItemDescription())
.setCancelable(true)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg,
int arg1) {
}
});
AlertDialog a = x.create();
a.show();
}
});
} else {
holder.textView.setOnClickListener(null);
}
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
}
Now I want it to be running the same in my App with Fragment.
I just know that using fragment = new ContentsFragment(); initiates it in fragment.
should it be extending ListFragment with a default constructor & an onCreateView(...) which I have inflated the another activities
I am new to fragments & i don't know what things should be changed in the code.
Please help !
EDIT:
I am showing my implemented code of what i have tried & i am getting 4 errors which are have specified in the code:
public class ContentsFragment extends Fragment implements OnTouchListener{
private MyCustomAdapter mAdapter;
Activity temp = this;// error: Type mismatch: cannot convert from ContentsFragment to Activity
String []s = new String[500];
ArrayList<GS> q = new ArrayList<GS>();
ListView lv;
int count=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DBAdapter db = DBAdapter.getDBAdapter(getApplicationContext());//error :The method getApplicationContext() is undefined for the type ContentsFragment
if (!db.checkDatabase())
{
db.createDatabase(getApplicationContext());//error : The method getApplicationContext() is undefined for the type ContentsFragment
}
db.openDatabase();
q = db.getData();
mAdapter = new MyCustomAdapter();
mAdapter.addSeparatorItem(new ContentWrapper(q.get(0).getA_name(),null));
mAdapter.addItem(new ContentWrapper(q.get(0).getAS_name(), q.get(0).getDesc_art()));
for (int i = 1; i < 460; i++) {
if (!(q.get(i).getA_name().trim().equals(q.get(i-1).getA_name().trim()))) {
mAdapter.addSeparatorItem(new ContentWrapper(q.get(i).getA_name(), null));
}
mAdapter.addItem(new ContentWrapper(q.get(i).getAS_name(), q.get(i).getDesc_art()));
}
setListAdapter(mAdapter); //error : The method getApplicationContext() is undefined for the type ContentsFragment
}
//Adapter Class
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList<ContentWrapper> mData = new ArrayList<ContentWrapper>();
private LayoutInflater mInflater;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(ContentWrapper value) {
mData.add(value);
notifyDataSetChanged();
}
public void addSeparatorItem(ContentWrapper value) {
mData.add(value);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
public ContentWrapper getItem(int position) {
return mData.get(position);
}
#Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
public int getCount() {
return mData.size();
}
public long getItemId(int position) {
Log.v("getItemId Position", ""+position);
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.activity_main1, null);
holder.textView = (TextView)convertView.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.activity_main2, null);
holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
count++;
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
} holder.textView.setText(mData.get(position).getItem());
if (type == TYPE_ITEM) {
holder.textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder x = new AlertDialog.Builder(temp);
Log.v("position",""+position);
x.setIcon(R.drawable.ic_launcher)
// .setTitle(q.get(position-count).getAS_name())
.setTitle(mData.get(position).getItem())
// .setMessage(q.get(position-count).getDesc_art())
.setMessage(mData.get(position).getItemDescription())
.setCancelable(true)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg,
int arg1) {
}
});
AlertDialog a = x.create();
a.show();
}
});
} else {
holder.textView.setOnClickListener(null);
}
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
}
setListAdapter is a method of ListFragment. So you need to extend ListFragment to use the same.
Change this
mAdapter = new MyCustomAdapter();
to
mAdapter = new MyCustomAdapter(getActivity());
And Then
public MyCustomAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
I tried make list view has TextView and CheckBox. It is worked fine but when select CheckBox in row another CheckBox in another row selected too.
Example: if I checked first row and scroll down I found another row selected too.
This my Adapter Code
public class ReadersrListAdapter extends BaseAdapter{
private static final int TYPE_SEPARATOR = 0;
private static final int TYPE_ITEM = 1;
private static final int TYPE_MAX_COUNT = 2;
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private LayoutInflater mInflater=null;
public ReadersrListAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
mInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return !data.get(position).get(UtiliShare.KEY_TITLE).startsWith("-");
}
#Override
public int getItemViewType(int position) {
return data.get(position).get(UtiliShare.KEY_TITLE).startsWith("-") ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
ViewHolder2 holder2 = null;
int type = getItemViewType(position);
if (convertView == null) {
//holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.reader_list_item, null);
holder.textView = (TextView)convertView.findViewById(R.id.readerTitle);
holder.textView.setTypeface(UtiliShare.getTf());
convertView.setTag(holder);
break;
case TYPE_SEPARATOR:
holder2 = new ViewHolder2();
convertView = mInflater.inflate(R.layout.reader_list_devider, null);
holder2.textView = (TextView)convertView.findViewById(R.id.readerTitle);
holder2.textView.setTypeface(UtiliShare.getTf());
convertView.setTag(holder2);
break;
}
//convertView.setTag(holder);
} else {
if(type==TYPE_ITEM) holder = (ViewHolder)convertView.getTag();
else holder2 = (ViewHolder2)convertView.getTag();
}
HashMap<String, String> curdata = new HashMap<String, String>();
curdata = data.get(position);
String txt = curdata.get(UtiliShare.KEY_TITLE);
if(type == TYPE_SEPARATOR){
txt = txt.replace("-", "");
holder2.textView.setText(txt);
}else{
//if(position <= 100) System.out.println("getView " + position + " " + convertView + " type = " + type);
holder.star = (CheckBox) convertView.findViewById(R.id.btn_Fav);
holder.star.setOnCheckedChangeListener(((ReadersListActivity) this.activity).mStarCheckedChanceChangeListener);
holder.textView.setText(txt);
}
//holder.textView.setText(txt);
return convertView;
}
private static class ViewHolder {
public CheckBox star;
public TextView textView;
}
private static class ViewHolder2 {
public TextView textView;
}
}
And this OnCheckedChangeListener in activity
public OnCheckedChangeListener mStarCheckedChanceChangeListener = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Cyril: Not implemented yet!
final int position = list.getPositionForView(buttonView);
Toast.makeText(ReadersListActivity.this, ""+position, Toast.LENGTH_SHORT).show();
}
};
How Can I fix this???
Thanks.
As you may know the layouts are reused as you scroll, so you need to track whether each row should be checked yourself. Let's add a new member to your Adapter:
private SparseBooleanArray checked = new SparseBooleanArray();
And a new method:
public void toggleCheck(int position) {
boolean state = expanded.get(position, false);
expanded.put(!state);
}
Now call our new method in onCheckedChanged():
final int position = list.getPositionForView(buttonView);
adapter.toggleCheck(position);
Lastly let's tweak getView() to change the checked state (and a few other things):
// Don't create a new HashMap that you'll never use
//HashMap<String, String> curdata = new HashMap<String, String>();
HashMap<String, String> curdata = data.get(position);
String txt = curdata.get(UtiliShare.KEY_TITLE);
if(type == TYPE_SEPARATOR){
txt = txt.replace("-", "");
holder2.textView.setText(txt);
}else{
//if(position <= 100) System.out.println("getView " + position + " " + convertView + " type = " + type);
// These two lines should be in `if(convertView == null)` with the others
holder.star = (CheckBox) convertView.findViewById(R.id.btn_Fav);
holder.star.setOnCheckedChangeListener(((ReadersListActivity) this.activity).mStarCheckedChanceChangeListener);
// Set the appropriate values
holder.textView.setText(txt);
holder.star.setChecked(expanded.get(position, false));
}
I have some trouble with layout in my list activity.
My list contain separators and text rows
SetupActivity extend ListActivity
private MyCustomAdapter mAdapter;
TextView selection;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mAdapter = new MyCustomAdapter();
mAdapter.addItem("Help/FAQ");
mAdapter.addSeparatorItem("Connection to Server");
// mAdapter.addItem("Connection");
// mAdapter.addItem("Network");
// mAdapter.addItem("config");
// mAdapter.addItem("User");
// mAdapter.addItem("pass");
// mAdapter.addItem("Email");
// mAdapter.addItem("PlatForm");
mAdapter.addSeparatorItem("Consumption");
// mAdapter.addItem("100%");
mAdapter.addSeparatorItem("Map");
// mAdapter.addItem("Map rotation");
// mAdapter.addItem("auto Zoom");
// mAdapter.addItem("Measure Units");
// mAdapter.addItem("Show Heading");
// mAdapter.addItem("Compass North");*/
mAdapter.addFooterItem(getString(R.string.setup_note_map));
mAdapter.addSeparatorItem("Support");
mAdapter.addItem("About");
/*
* mAdapter.addItem("Contact Us"); mAdapter.addItem("Tutorial");
* mAdapter.addItem("Setup Wizard");
*/
mAdapter.addSeparatorItem("Blogs");
mAdapter.addFooterItem(getString(R.string.setup_note_blogs));
setListAdapter(mAdapter);
// selection = (TextView) findViewById(R.id.text);
}
public void onListItemClick(ListView parent, View view, int position,
long id) {
parent.getChildAt(position).setBackgroundColor(position);
if (position == 0) {
Intent myIntent = new Intent(SetupActivity.this,
WebviewHandlerActivity.class);
myIntent.putExtra("ressource", "help");
SetupActivity.this.startActivity(myIntent);
} else if (position == 6) {
Intent myIntent = new Intent(SetupActivity.this,
AboutActivity.class);
SetupActivity.this.startActivity(myIntent);
}
}
// Adapter Class
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 2;
private static final int TYPE_SEPARATOR = 0;
private static final int TYPE_FOOTER = 1;
private static final int TYPE_MAX_COUNT = TYPE_ITEM + 1;
private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
private TreeSet<Integer> mFooterSet = new TreeSet<Integer>();
public MyCustomAdapter() {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparatorItem(final String item) {
mData.add(item);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
public void addFooterItem(final String item) {
mData.add(item);
// save separator position
mFooterSet.add(mData.size() - 1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
if (mSeparatorsSet.contains(position))
return TYPE_SEPARATOR;
else if (mFooterSet.contains(position))
return TYPE_FOOTER;
return TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
public int getCount() {
return mData.size();
}
public String getItem(int position) {
return mData.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.item1, null);
holder.textView = (TextView) convertView
.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.item2, null);
holder.textView = (TextView) convertView
.findViewById(R.id.textSeparator);
break;
case TYPE_FOOTER:
convertView = mInflater.inflate(R.layout.footer, null);
holder.textView = (TextView) convertView
.findViewById(R.id.note);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
my xml item1 & item2 contain a LinearLayout with a TextView inside
and my footer.xml only a textView
My problem is when i click on a row it doesn't get orange to say that i clicked on it except my footer...(the one i don't want)
So i figure out it's because it's not in a LinearLayout so i tried to put off the LinearLayout of item1.xml but i can't compile anymore.
Does someone can explain to me how to get my row with the click Animation and not on my footers ?
Cheers
You are not getting clicked color because you have set a background color `
setBackgroundColor
You need to set a statelistDrawable as background
Edit: You need to set a drwable something like this
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed_img" android:state_pressed="true"/>
<item android:drawable="#drawable/default_img"/>
</selector>