Android ListView Item is not Clickable - android

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,

Related

how to get a particular column value from a list view on click of a row from that list view?

I have a list view where in i have four to five columns. The values to the list view are assigned through a database query. The list view is created using SimpleCursorAdapter. I am trying to get the value of the first column of the list view... but m not able to retrieve it. How do i do that???
Here's my custom adapter
private class MyCursorAdapter extends SimpleCursorAdapter
{
#SuppressWarnings("deprecation")
public MyCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to,int flags) {
super(context, layout, c, from, to);
// TODO Auto-generated constructor stub
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//get reference to the row
View view = super.getView(position, convertView, parent);
//check for odd or even to set alternate colors to the row background
if(position % 2 == 0){
view.setBackgroundColor(Color.rgb(238, 233, 233));
}
else {
view.setBackgroundColor(Color.rgb(255, 255, 255));
}
return view;
}
}
here is where i am assigning my adapter to the listview
MyCursorAdapter myDataAdapter=new MyCursorAdapter(this,R.layout.list_row_layout,cursor, columns, to, 0);
listView=(ListView)findViewById(R.id.listView1);
listView.addHeaderView(getLayoutInflater().inflate(R.layout.list_header, null, false));
listView.setAdapter(myDataAdapter);
and here is the onclick listener
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
//View view=
String ttsl_can=(( listView.getItemAtPosition(position).toString()));
Toast.makeText(PendingActivity.this, ttsl_can, Toast.LENGTH_LONG).show();
}
});
by clicking on a particular row of the listview m getting the response as android.database.SQLiteCursor#40d2f13d
how do i get the response as the value of the first column on which ever row i click???
what change do i have make in my code to achieve that???
Please Help!! Thanks in Advance!!
Try out like this:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
Cursor cursor = listView.getItemAtPosition(position);
// Here you have to getString using index
String str = cursor.getString(<you column index>);
Toast.makeText(PendingActivity.this, str, Toast.LENGTH_LONG).show();
}
});

Custom Drag Sort Listview with Radiobutton

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().

How to change images in grid view as the position changes in android?

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().

onclicklistener for subitems of listview

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

Constructing Dynamic List View In Android

Iam trying to create a list view dynamically, Please provide me an example for list view in android...
Activity class......
public class UsersListActivity extends ListActivity{
RegisterUser registerUser;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] statesList = getListOfAllUsers();
Toast.makeText(getApplicationContext(),
"States Array lentgh is : " + statesList.length,
Toast.LENGTH_LONG).show();
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
statesList));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"You selected : "+((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
}}
and create an xml file named list_item.xml and paste the below code.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" android:textColor="#ffffff" android:textStyle="bold" android:background="#drawable/border_cell">
</TextView>
You can create your own List View by extending Base adapter class
public class ListAdapterDroidman extends BaseAdapter{
private ArrayList<ListitemDroidman> list = new ArrayList<ListitemDroidman>();
#SuppressWarnings("unused")
private Context context;
public ListAdapterDroidman(Context context) {
this.context = context;
}
public void addItem(ListitemDroidman item) {
list.add(item);
}
public void addItem(ListitemDroidman item,int pos)
{
list.add(pos, item);
}
public void removeItem(int pos)
{
list.remove(pos);
}
public void clearList()
{
list.clear();
}
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return list.get(position);
}
}
Then create your own layout
public class ListitemDroidman extends LinearLayout {
TextView filename;
ImageView iv;
public ListitemDroidman(Context context) {
super(context);
}
public void setInfo(Context context, Bitmap icon, String fname,int colorId) {
iv=new ImageView(context);
iv.setImageBitmap(icon);
iv.setPadding(0, 0, 20, 0);
addView(iv);
filename = new TextView(context);
filename.setTextSize(20);
filename.setTextColor(getResources().getColor(colorId));
filename.setText(fname);
addView(filename);
}
}
Now create listitems in your activityClass
create object of the adapter that u have created
myListitem = new ListitemDroidman(this);
icon = BitmapFactory.decodeResource(getResources(),
R.drawable.folder_icon);
myListitem.setInfo(getApplicationContext(), icon, filelist[i],
R.color.color_white);
add the listitem to the adaper as in normal list
la_file.addItem(myListitem);
Try this tutorial to understand how to use ListView.
http://developer.android.com/resources/tutorials/views/hello-listview.html
Why don't you just take a look at the ApiDemos of your android SDK..
You can find 14 examples of ListActivities implementations..
Or just search here or on Google, you can find lots of examples
Then if you want to achieve something in particular you can ask a more specific question here, does it sound fair?

Categories

Resources