I have a DSLV with custom adapter which has two textviews and a radiobutton. I want the radiobutton of a particular row to be setchecked(true) when that row is clicked. To do that I used the below mentioned code.
public OnItemClickListener listclk = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
if(!rad.isChecked())
{
rad.setChecked(true);
}else{
rad.setChecked(false);
}
}
};
But its not working properly. Please Help!!...Thanks
Try this:
(assuming your DSLV is called "list")
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
finally the problem got sorted out by adding ..
#Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return namelist.size();
}
#Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return position;
}
after the getview().
Related
I have created simple list view,what i want to do is that when user click on list view,selected record should show in text view in my code it work properly,but problem is that when user again click i want to show that record in second text view,please give me idea/code how to do this..please please help me...i am new in android..
public class MainActivity extends Activity {
String item[]=new String[]{"rk","kk","kk","ll","mm","uu"};
TextView tv,tv1,tv2,tv3,tv4;
ListView li;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
li=(ListView)findViewById(R.id.listView1);
tv=(TextView)findViewById(R.id.setText);\
tv1=(TextView)findViewById(R.id.setText1);
tv2=(TextView)findViewById(R.id.setText2);
tv3=(TextView)findViewById(R.id.setText3);
tv4=(TextView)findViewById(R.id.setText4);
ArrayAdapter<String> adapter=new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,android.R.id.text1, item);
li.setAdapter(adapter);
li.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
// TODO Auto-generated method stub
int itm=position;
Toast.makeText(getApplicationContext(),""+itm+""+li.getItemAtPosition(position),40).show();
tv.setText(""+li.getItemAtPosition(position));
}
});
There are many ways you can deal with it.
ArrayList<Boolean> firstTimers = new ArrayList<Boolean>();
for (Int I =0; I< item.length(); I++){
firstTimers.add(false);
}
li.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
// TODO Auto-generated method stub
if( !firstTimers.get(position ){
int itm=position;
Toast.makeText(getApplicationContext(),""+itm+""+li.getItemAtPosition(position),40).show();
tv.setText(""+li.getItemAtPosition(position));
firstTimers.set(position,true);
}else{ // show in other textview
}
}
});
I hope i'm understanding you right. You want different textviews to have text depending on how many tiems it's clicked.If so, set a static counter to count how many times it's clicked. In your OnItemClick increment this counter and check for it's value, then set the text of the TextViews depending on it's value.
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
// TODO Auto-generated method stub
int itm=position;
Toast.makeText(getApplicationContext(),""+itm+""+li.getItemAtPosition(position),40).show();
if (myCounter >=2){
someTextView.setText(""+li.getItemAtPosition(position));
}else{
anotherTextView.setText("blabla")'
}
}
Edit: Make an ArrayList holding your TextViews in the correct order. Using the position int given in OnItemClick you can take out the correct TextView and use .setText on them!
In Fragment onCreateView i have the following code
consumerlist=(ListView)view.findViewById(R.id.consumerlist);
consumerlist.setAdapter(new consumerListAdapter(getActivity(), dataList));
consumerlist.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listview, View view, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "ID"+id, 1);
}
});
Button btn=(Button)view.findViewById(R.id.button1);
registerForContextMenu(consumerlist);
return view;
if i write registerForContextMenu(btn) then it works.but list view row long click does nothing.Even my listView onItemClick listener does not work.how to fix it?Anybody helps me greatly appreciated.Thanks in advance.
try this:
consumerlist.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
registerForContextMenu( view );
openContextMenu( view );
return false;
}
});
also this tutorial should help you a lot ...
//EDIT
Move
registerForContextMenu(consumerlist);
to
public void onActivityCreated(Bundle savedState) {
Remove scollview in layout or define it as not focusable make the code working.
I have a list view and using two listeners - OnItemClicked and OnItemSelected. The clicked listener is working properly but onItemSelected Listener is not getting invoked. I need OnItemSelected listener because sometimes selected is set pragmatically.
allClues.post(new Runnable() {
public void run() {
listView.setSelection(ind);
}
});
I am expecting that when selection is set its OnSelection listener will be invoked. But its not happening.
listView.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
iSelected = arg2 ;
Log.e("listargs", (String.valueOf(arg1)) + " " + String.valueOf(arg3));
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
What could be wrong here ?
OnItemSelectedListener is used for the Spinner not for the Listview For Listview you have to use OnItemClickListener
You have to use OnItemClickListener like this
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int i,
long j) {
// do ur code
}
});
Is it possible to update references in OnItemSelected after notifyDataSetChanged was called?
e.g
ImageView onFront;
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
onFront = (ImageView) view;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
I am using onFront to rotate image, but after I call notifyDataSetChanged - I cannot.
Make your code to remember which view was clicked.
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
view.setTag(postion);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Then use this position in getview of adapter
int pos = (Integer)view.getTag();
if(pos == position){
//Make your change in view to make it look different
}
To update the reference and reflect it into view , you should call notifyDataSetChanged(). This is the only way to tell your adapter that something has changed , Who is controlling view.
Try calling this method after your method:
listview_adapater.invalidate();
Try to make onFront static like this.
private static ImageView onFront;
I want to design each list item in the ListView can be clickable, and triger out which list item be clicked. But it can not. I tried the two methods: setOnItemClickListener() and setOnItemSelectedListener() on my code. I have had googled couple of references about the article, however it still can not work(clickable).
I would like post the code below: The code can display the list items and I can see the Log.d content for the line of Log.d(" mListView01.getCount()="," "+vc); on LogCat well. But, there is no any response if I clicked on the list Item.
if you don't mind, could you help point me out where I was wrong, thanks !
Code for creating the listView using the Activity Widget:
......
setContentView(R.layout.main_open);
TextView itemText = (TextView) findViewById(R.id.itemText);
TextView codeText = (TextView) findViewById(R.id.codeText);
itemText.setText(selectedItem);
codeText.setText(selectedCode);
ListView mListView01 = (ListView)findViewById(R.id.main_open_listview1);
String[] keys = new String[] {"title","title_image", "content",
"title1","title1_image","content1","title2","title2_image","content2"};
int[] resValues = new int[] { R.id.title, R.id.title_image, R.id.content,
R.id.title1, R.id.title1_image, R.id.content1,R.id.title2, R.id.title2_image, R.id.content2};
openDocAdapter opendoc = new openDocAdapter(this,localdcoumentlist, R.layout.main_open_content, keys, resValues );
mListView01.setSelected(true);
mListView01.setClickable(true);
mListView01.setAdapter(opendoc);
int vc = mListView01.getCount();
Log.d(" mListView01.getCount()="," "+vc);
mListView01.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Log.d("Selected From setOnItemSelectedListener, arg2=", " "+ arg2);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
mListView01.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
selectedViewPos = arg2;
Log.d("TitlesSelectionDialog(),selectedViewPos= "," "+ selectedViewPos);
Toast.makeText(getApplicationContext(), "selectedViewPos= "+ selectedViewPos, Toast.LENGTH_LONG).show();
}
});
......
Code for openDocAdapter:
private class openDocAdapter extends SimpleAdapter
{
private Context _con;
private List _List;
private int _listviewId;
private String[] _keys;
private int[] _resValues;
public openDocAdapter(Context context, ArrayList<HashMap<String,Object>> List , int listviewId, String[] keys, int[] resValues )
{
super(context, List, listviewId, keys, resValues);
_con =context;
_List = List;
_listviewId = listviewId;
_keys = keys;
_resValues = resValues;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.main_open_content, null);
}
TextView title = (TextView) v.findViewById(R.id.title);
(...Similiar codes define textView, imageViewsd.)
return v;
}
#Override
public int getCount()
{
// TODO Auto-generated method stub
return super.getCount();
}
#Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return super.getItem(position);
}
#Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return super.getItemId(position);
}
}
Edit1: I found an article here About the Focus setting on the layout will cause clickable work or not work. So, I remove the lines of (I don't while it be coded here) in the xml file of layout. Then the setOnItemSelectedListener() method is worked while scrolling the list list with orange focus change. But it still not meet my expection.
Provlem Solved ! After couple hous googling/search and try_eror. And I would like share it if you are interesting.
The main cause of the problem is: I used the ScrollView as the basic layout for the row.xml(containing the content for each listview row). Then, I used the LinearLayout(Vertial) instead of it. The setOnItemClickedListener() method works fine now. I do not have any idea regarding this that will cause the ListView to be not clickable. If somebody know it, please tell us,