I have a listview and each item of the lisview is a linearlayout.
Each one of the linearlayouts contains 3 textviews.
How do i set a onclicklistener for those textviews?
i tried this:
TextView tv=(TextView)findById(R.id.textView1);
tv.setOnClickListener(...);
This throws me a nullpointerexception.
I also tried setonitemclickedlistener for the listview,but this only allows me to operate on the linearlayout,not the textview.
thanks in advance.
If this is needed statically and your view is XML based, this is what I did:
<TextView
...
android:clickable="true"
android:onClick="myHandler"
/>
This calls myHandler whenever the textview is touched/clicked. As you are using this in a list view, you will still need to add a tag in getView() and use that in myHandler() to figure out which row/field were pressed.
Hope this helps.
For getting this requirement you must you use custom adapter class, so that you can get this very easily. using custom adapter is very easy and simple process.
click on this link, its simple application, and In place of Buttons you can use TextView.
Have a nice day..
you should go for creating custome adpter like Use BaseAdpter
and here pass your list and set that list according to position to Textview and here you can set onClick event also
you can create base Adpter like ex
(1)
public class GridAdpter extends BaseAdapter
{
List<String> checkednamelist;
public GridAdpter(Context c, List<String> myitem) {
this.checkednamelist =myitem
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//Here change with yur row xml file name and 3 textview control id name
View grid;
if (convertView == null) {
grid = layoutInflater.inflate(R.layout.row_grid, null);
} else {
grid = convertView;
}
TextView textView2 = (TextView) grid.findViewById(R.id.txtlable2);
textView.setText(checkednamelist.get(position);
textView2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// do here as per your require
}
});
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return myitem.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return myitem.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public List<String> mycheckeditem() {
return checkednamelist;
}
}
// finally set this adpter with your listview
Related
I have a ListView in which i add row items dynamically. Each row has a no of childs. Now i am setting on click listeners on these childs, and in them it asks me to keep these child items final, it works fine with them and it is ok to do this as i am creating a class in each on click listener.
Here isFree is a final checkBox.
isFree.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (isFree.isChecked()){...}
}
});
But i dont feel it as a good programming practice, i want to use some function which should detect which child was clicked and then i should be able to access its row using getParent and then its sibling items using getChildAt(index)
So, i used this:
isFree.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
CheckBox tv=(CheckBox) getCurrentFocus();
TableRow row = (TableRow)tv.getParent();
TextView tv1=(TextView) row.getChildAt(5);
if (tv.isChecked()){...}
}
});
But when i use it, on focussed item actualy returns the listview, but i want focused item in the row.
you can get which child of listview was clicked with onItemClickListener, but if you want to do some action on a row which has some checkbox or some other clickable view and you want to perform some action on those click then i think you have to use click listeners on those views itself.
but if you want to get listitem row at click of those individual views, you can use setTag method of view class to store the index and then getting the row item from that index on your click listeners.
public view getView(int position, View convertView, ..) {
....
....
....
// your code
convertView.setTag(position);
return convertView;
}
isFree.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (arg0.isChecked()){
int index = arg0.getTag();
View v = listview.getItemAtPosition(index);
// or get the adapter get data list from it and then index it.
}
}
});
NOTE that: I havent used final free inside oncheckedchange method.
On the setOnCheckedChangeListener method, the first parameter is a CompoundButton that is a super-class of CheckBox. So, actually, arg0 is your checkbox. Use it to retrieve the parent list item and make your stuff.
isFree.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
TableRow row = (TableRow)arg0.getParent();
TextView tv1=(TextView) row.getChildAt(5);
if (arg0.isChecked()){...}
}
});
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().
Is there any way to remove selected item from gridview.
I want to delete the selected item from my gridview.
I did not find any thing . my code is
public class ImageAdapter extends BaseAdapter{
Context context;
public ImageAdapter(Context context)
{
this.context = context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 5, 0, 0);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
public Integer[] mThumbIds = {
R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,
R.drawable.sample_3,R.drawable.sample_1,R.drawable.sample_2,
R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_1
};
}
//////////////////
public class ImageActivity extends Activity {
ImageAdapter iAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
iAdapter = new ImageAdapter(this);
final GridView gView = (GridView)findViewById(R.id.grid_view);
gView.setAdapter(iAdapter);
gView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//gView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
// gView.setItemChecked(position, true);
Toast.makeText(ImageActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
iAdapter.notifyDataSetChanged();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_image, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.menu_delete)
{
Toast.makeText(this, "Delete",Toast.LENGTH_SHORT ).show();
}
return super.onOptionsItemSelected(item);
}
}
can anyone have idea .
thank
you are using a table :
public Integer[] mThumbIds = {
R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,
R.drawable.sample_3,R.drawable.sample_1,R.drawable.sample_2,
R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_1}
Tables are not modifiable.
Replace it by a List on which you will be able to make add or remove operations. Simply call notifyDataSetChanged when a change is made to let the adapter know that its list has been modified.
As Teovald and pskink suggested, you cannot have a fixed list of images and then implement the remove functionality that you are looking for. If you want to add remove, change your design and make sure your data set is also removable. What you have tried so far seems to be using some really basic code which is good to show basic GridView feature.
Just try this. Create a image data set class which returns the actual images. Store the images in a List that can be modified. Add setters/getters to this data set and also add a method to delete a particular item. Change your image adapter to use the image from this new data set. Whenever an image is deleted, call the notifyDataSetChanged on the adpater.
Good luck
I am splitting an image and placing in grid view. On clicking two images in grid view, I want to swap their positions. I am able to change their positions, but the image is not changing. I have used bitmap array for splitting images and placing them in grid view. So how do I change images in grid view by clicking two images? How should I do the swapping of images? How can any one help me?
public class Imagepieces extends Activity {
ArrayList<Bitmap> breakedimages,duplicate;
GridView g;
int i=0,temp,temp2,rpos;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
breakedimages = getIntent().getParcelableArrayListExtra("breaked image");
duplicate = new ArrayList<Bitmap>(breakedimages);
Collections.shuffle(duplicate);
g = (GridView) findViewById(R.id.gridView1);
g.setAdapter(new CutAdapter(this, breakedimages));
g.setNumColumns((int) Math.sqrt(breakedimages.size()));
g.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
//=================================================
{
}
});
}
class CutAdapter extends BaseAdapter {
int iwidth, iheight;
Context context;
public CutAdapter(Imagepieces ipieces, ArrayList<Bitmap> breakedimages) {
// TODO Auto-generated constructor stub
iwidth = breakedimages.get(0).getWidth();
iheight = breakedimages.get(0).getHeight();
context = ipieces;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return duplicate.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return duplicate.get(arg0);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView i=new ImageView(context);
i.setLayoutParams(new GridView.LayoutParams(iwidth +5,iheight +5));
i.setPadding(0, 0, 0, 0);
i.setImageBitmap(duplicate.get(arg0));
return i;
}
}
}
How to swap images in the grid view? When i click on the grid view on the first click i have to get position, and second click second position. Then I the images in the two positions and after changing all images i want to compare with the image that has been splitted if both are equal. Then i want to display a toast as game finished. Can anyone help me
In GridView,when you want to change a item , all you need is change the Adapter which you link to the GridView.
For example, swap two item:(GridAdapter extends ArrayAdapter)
GridAdapter adapter = (GridAdapter)gridv.getAdapter();
String firstItem = adapter.getItem(firstPosition);
String secondItem = adapter.getItem(secondPosition);
adapter.remove(firstItem);
adapter.insert(firstItem, secondPosition);
adapter.remove(secondItem);
adapter.insert(secondItem, firstPosition);
You also can change other things in your custom Adapter by override the getView().
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,