i have a problem which i narrowed it down to a focus problem. At least it seems like a focus problem, i might be wrong.
Declerations:
LogListAdapter adapter;
Spinner filterTypeMenu;
ListView list;
onCreate:
list = (ListView) findViewById(R.id.loglist);
filterTypeMenu = (Spinner) findViewById(R.id.spinner1);
adapter = new LogListAdapter (this, R.layout.row, filteredLogs);
adapter.notifyDataSetChanged();
filterTypeMenu.setOnItemSelectedListener(this); // tried with inner class, same result
in my interface method when i complete my process:
..
..
adapter.notifyDataSetChanged();
..
..
and click listeners:
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, Integer.toString(position),
Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
My main layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="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:id="#+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/spinnerlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/filter_array"
android:prompt="#string/filter"/>
</LinearLayout>
<LinearLayout
android:id="#+id/listlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false">
<ListView
android:id="#+id/loglist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false">
</ListView>
</LinearLayout>
</LinearLayout>
Notify method is called repeatedly real quick and my list is updating quite fast due to behaviour of the process. When i remove the update block of the code, click events of the spinner is active, but when the update block is active, events are not working. I thought this was a focus problem but i couldn't be sure, since i the menu is clickable. The problem is that when i select one of the items in the list, spinner is not selecting that item and keep the old selected value, and the event of the click is not working either. Ideas will be much appreciated.
Funny, i fixed my problem with adding these two settings to the spinner, i was sure i tried this before but oh well, android focus is still a half mystery to me.
filterTypeMenu.setFocusable(true);
filterTypeMenu.setFocusableInTouchMode(true);
Related
I have a ListView which has a Custom TextView which i got from here: FlowTextView
The problem is if i use a normal default TextView everything works perfectly fine,But if i use this custom text view all the Click Events (LongClick and OnClick) dont work if clicked inside the text view i.e. the click event only work at places where this Custom TextView doesn't reach to (The edges of my List Row).
I have tried
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="true"
But none of it worked, or maybe i did it in the wrong way...if you think any of these methods work please elaborate on how to use this
My implementation in the FlowTextView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content"
android:layout_height="wrap_content" android: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=".MainActivity"
android:background="#ff71ff34">
<uk.co.deanwild.flowtextview.FlowTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textView2"
android:text="Description"
android:textSize="24sp"
android:background="#1b1b1f"
android:textColor="#android:color/white">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#ffffbb52"/>
</uk.co.deanwild.flowtextview.FlowTextView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CodeLearn 1"
android:background="#1b1b1f"
android:textColor="#android:color/white"/></RelativeLayout>
inside OnCreate
ListView mainList = (ListView)findViewById(R.id.listView1);
new Connect(mainList,this).execute(aa);
mainList.setClickable(true);
mainList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
public boolean onItemLongClick(AdapterView <?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub
// Toast.makeText(this, "Wrong Username/Password! TRY AGAIN!", Toast.LENGTH_LONG).show();
Log.d("long clicked","pos: " + pos);
return true;
}
});
activity_main.xml which contains the root listview
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"></ListView>
As said by #Vikram in comments
Check line 88 of FlowTextView.java: Link. That should answer your
question.
so basically the solution was to setOnTouchListener for that object to null
setOnTouchListener(null)
I encountered a silly problem.
I have an identical thing working already, but for the second try, the onItemClick is not doing anything. I re-re-rechecked so many times now, but nothing seems to catch my eye. Please take a look and let me know what is wrong
So here is my setup.
The activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_promotionale);
ListView mylist = (ListView) findViewById(R.id.lv_promotionale);
mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Toast.makeText(PromotionaleActivity.this,"I touched an item at position"+Integer.toString(position),Toast.LENGTH_SHORT).show();
}
});
The activity layout is:
<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">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="370dp"
android:id="#+id/lv_promotionale" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Some text here"
android:id="#+id/textView"
android:paddingLeft="15dp"
android:paddingStart="15dp"
android:paddingTop="15dp"
android:paddingRight="0dp"
android:paddingEnd="0dp"
android:paddingBottom="0dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
EDIT
Here is the method where I load my listview with data using an external adapter
public void LoadListWithStuff(){
SQLiteDatabase db = new myDbHelper(getApplicationContext()).getWritableDatabase();
Cursor mCursor = db.rawQuery("select _id,data,numar,idclient,numeclient,tipclient,trimisa from cereri_mst order by data,numar desc" , null);
idid.clear();
idclient.clear();
numeclient.clear();
tipclient.clear();
datacerere.clear();
numarcerere.clear();
trimisa.clear();
if (mCursor.moveToFirst()) {
do {
idid.add(Integer.toString(mCursor.getInt(0)));
datacerere.add(mCursor.getString(1));
numarcerere.add(mCursor.getString(2));
idclient.add(Integer.toString(mCursor.getInt(3)));
numeclient.add(mCursor.getString(4));
tipclient.add(mCursor.getString(5));
trimisa.add(mCursor.getString(6));
} while (mCursor.moveToNext());
}
DisplayCereriAdapter disadpt = new DisplayCereriAdapter(PromotionaleActivity.this,idid,idclient,
numeclient, tipclient,datacerere,numarcerere,trimisa);
ListView lv = (ListView) findViewById(R.id.lv_promotionale);
lv.setAdapter(disadpt);
mCursor.close();
db.close();
}
Also, I must say here that the listview is populated. I can see the contents of the listView. The only problem is that I cannot seem to catch the ItemClick and longClick events on the listview...
Why doesn't it work?????
I am going crazy here...
Thank you
The solution I found was to stop using the checkBox and replace it with ImageView... Now all works fine
Hello everyone and thank you for reading my question!
I'm currently developing an android app that has the following hierarchy:
One FragmentActivity is divided by three ListFragments.
One of these ListFragments uses a custom ArrayAdapter to display a List<> with a xml template on a ListView
This List<> is filled with dataset which are given id's. Datasets with id's lower than 10 are flagged "important", the other ones are flagged "not so important"
This is what I am looking for:
I have two layout templates, and I want to display the "important" datasets with one layout and the "not so important" with the other layout.
I have searched for quite a while now, but it looks like noone has asked a question like this before.
I tried to use a second ListView and apply a second custom ArrayAdapter on it, but unfortunately, there is a restriction: Since I'm using ListFragment, I'm forced to use findViewById like this.
ListView list;
list = (ListView) rootView.findViewById(android.R.id.list);
When I tried to write the second custom ArrayAdapter, it showed an error here:
ListView list;
list_flat = (ListView) rootView.findViewById(android.R.id.list_flat);
Here is the entire ListFragment
public class LeaguePage extends ListFragment{
Global global_var;
ListView list, list_flat;
List <League> leagues = null;
ListAdapter adapter = null;
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.league_page, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
list_flat = (ListView) rootView.findViewById(android.R.id.list_flat);
try {
leagues = Leagues_Parser.parse(getActivity().getAssets().open("league_raw.xml"));
} catch (IOException e) {
e.printStackTrace();
}
adapter = new LeagueAdapter (getActivity(), R.layout.list_row, leagues);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Global.selectedTeam = Integer.valueOf(leagues.get(arg2).getInternalID());
Global.mViewPager.setCurrentItem(1, true);
}
});
}
}
This is the xml file I pass on to my ArrayAdapter
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/liga_flat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dip"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:paddingBottom ="10dip"
android:textColor="#040404"
android:textSize="25sp"
android:textStyle="normal"
android:typeface="serif" />
</RelativeLayout>
This is the xml layout file for the ListFragment
<?xml version="1.0" encoding="utf-8"?>
<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: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=".Main_Activity$search_page" >
<RelativeLayout
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=".Main_Activity$search_page" >
<ListView
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="0dp"
android:divider="#null">
</ListView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
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=".Main_Activity$search_page" >
<ListView
android:id="#+id/android:list2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#null"
android:dividerHeight="0dp" >
</ListView>
</RelativeLayout>
</RelativeLayout>
You shouldn't give a ListView an ID like this :android:id="#+id/android:list2", the ID of your ListView should be
<ListView android:id="#id/android:list"
...... ></ListView>
or
<ListView android:id="#+id/sampleList"
...... ></ListView>
i want to add data into the listview when pressing the button.
but it is not getting updated pressing the button dont know how it is not getting worked.
Please help me.
here is the code below
#SuppressLint("NewApi")
public class Messanger extends Activity {
ArrayAdapter<String> adapter;
ListView output;
EditText box;
String msg[];
Button btn;
int counter=0;
ArrayList<String> listItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messanger);
output=(ListView)findViewById(R.id.textOutput);
box=(EditText)findViewById(R.id.textInput);
listItems = new ArrayList<String>();
adapter = new ArrayAdapter<String> (Messanger.this,android.R.layout.simple_list_item_1, android.R.id.text1, listItems);
output.setAdapter(adapter);
btn=(Button)findViewById(R.id.btnSend);
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(Messanger.this, "in event", Toast.LENGTH_LONG).show();
sendMessage(box.getText().toString());
//stItems.notifyAll();
}
});
}
public void sendMessage(String message)
{
listItems.add("me: "+message);
adapter.addAll(listItems);
adapter.notifyDataSetChanged();
}
}
And my Layout looks like this
<?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">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="10" >
<ListView
android:id="#+id/textOutput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp" />
</ScrollView>
<LinearLayout
android:id="#+id/linearLayout1"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="100"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:baselineAligned="true">
<EditText android:layout_weight="1" android:id="#+id/textInput"
android:layout_height="45dp" android:layout_width="fill_parent">
<requestFocus></requestFocus>
</EditText>
<Button android:layout_weight="1" android:text="Send"
android:layout_height="45dp" android:layout_width="125dp"
android:id="#+id/btnSend"></Button>
</LinearLayout>
</LinearLayout>
thanks in advance
Not sure why is isn't updating. Code looks OK to me. You might try clearing the array before calling addAll() because if you don't you will end up with duplicates in your list (which probably isn't what you want. Should look like this:
listItems.add("me: "+message);
adapter.clear();
adapter.addAll(listItems);
adapter.notifyDataSetChanged();
The reason it isn't updating is probably somewhere else though. Post your layout, maybe the problem is in there.
EDIT: After seeing the layout:
Yep, the problem is in your layout. You don't put a ListView inside a ScrollView. A ListView already knows how to scroll. Get rid of the surrounding ScrollView and you should be good.
Notice that you always add the same items by calling
adapter.addAll(listItems);
Regardless,
Try debugging you getView() function in the adapter.
Actually i have a viewpager that holds a Imageview and a listview over it, So when i click on the Listview i need to hide the listview and the bottom bar to make the ImageView fullscreen for all the views in side the viewpager. And onclick of the image view get back the views. But my issues are:
I cant implement onClickListner on Listview, to make the whole view to handle the tap. Am forced to use the onItemclickListner and it only hides when you click on the item(which is not desired).
When i click Listview(which is inside Fragment) inside the pager it doesn't hide for all the views except for the currentview, But am able to hide the actionbar and the bottom bar(on the Fragment Activity).
a screen shot for reference:
How can i achieve this or this is there any better way that i can hide the views.
Here is my ListView in side a Fragment:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
android:divider="#android:color/transparent"
android:listSelector="#android:color/transparent"
android:scrollbars="none" />
</RelativeLayout>
Here's my Fragment Activity layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/detailLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/form" >
</android.support.v4.view.ViewPager>
<RelativeLayout
android:id="#+id/form"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:padding="2dp" >
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/btnSend"
android:drawableRight="#drawable/edit_keyboard"
android:inputType="textMultiLine"
android:maxLines="3" />
<ImageButton
android:id="#+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/send" />
</RelativeLayout>
</RelativeLayout>
Here am successful with the onitemclicklister to the ListView:
listComments.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
if (getSherlockActivity().getSupportActionBar().isShowing()) {
getSherlockActivity().getSupportActionBar().hide();
listComments.setVisibility(View.GONE);
form.setVisibility(View.GONE);
} else {
getSherlockActivity().getSupportActionBar().show();
listComments.setVisibility(View.VISIBLE);
form.setVisibility(View.VISIBLE);
}
}
});
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
getSherlockActivity().getSupportActionBar().show();
listComments.setVisibility(View.VISIBLE);
form.setVisibility(View.VISIBLE);
}
});
Could you set the clickable property for the listView and check ? Also, whenever you add a new edittext to the do you add it dynamically ? If so that would mean that the you are adding items to the listView dynamically. The listView would not know if you have clicked on an item of the view itself hence a callback would get registered when you click the item as that takes preference.