Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Android
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Liutauras Vilda
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Devaka Cooray
Paul Clapham
Saloon Keepers:
Scott Selikoff
Tim Holloway
Piet Souris
Mikalai Zaikin
Frits Walraven
Bartenders:
Stephan van Hulst
Carey Brown
Forum:
Android
listview not clickable
Astha Sharma
Ranch Hand
Posts: 250
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hello,
I am developing an app using ListView but ListView's onClick event is not working. I'v searched a lot on google but not getting the solution. Tried all ways of building a list, but nothing works. Help needed. This is the
java
code-
public class MainActivity extends Activity implements OnClickListener, OnItemClickListener{ private static final int REQUEST_CODE = 0; Button b1; ListView lv1; String bookName; static int pageCount; ImageView iv; static FileHandler fh=null; static String PATH= "data/data/sharma.astha.book.store2/books"; public static TextView bookMenu; Context context =this; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1=(Button)findViewById(R.id.b1); b1.setOnClickListener(this); lv1=(ListView)findViewById(R.id.lv1); lv1.setOnItemClickListener(this); Log.d("1","oncreate"); fh= new FileHandler(); ArrayList<String> folders= new ArrayList<String>(); folders=fh.updateBookList(); Log.d("1","result after updateBookList"); if(folders!=null && folders.size()>0) { MyBookList myContactListAdapter = new MyBookList(folders, MainActivity.this); Log.d("1","folder"+folders.size()); lv1.setAdapter(myContactListAdapter); } } public void onClick(View v) { // TODO Auto-generated method stub Log.d("1","onclick"); Intent i=new Intent(this, BookName.class); startActivityForResult(i, REQUEST_CODE); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d("1","onactivityresult" ); if(requestCode==REQUEST_CODE) { Log.d("1","onrequest code verified "+resultCode); if(resultCode==-2) { try { Log.d("1","result after enter book name "+bookName); Bundle b=data.getExtras(); bookName= b.getString("bookName"); pageCount=fh.pageCount(bookName); Log.d("1","pageCount= "+pageCount); Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); //Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); String newPath = PATH+"/"+bookName+"/"+pageCount+".jpg"; Log.d("1",newPath); File file = new File( newPath ); file.mkdirs(); Uri outputFileUri = Uri.fromFile( file ); //String absoluteOutputFileUri = file.getAbsolutePath(); intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); startActivityForResult(intent, 0); } catch(Exception e) { Log.d("1","exotn in path"+e.getMessage()); e.printStackTrace(); } } else { fh= new FileHandler(); ArrayList<String> folderName=fh.updateBookList(); Log.d("1","result after capture"+folderName.size()); MyBookList myContactListAdapter = new MyBookList(folderName, context); lv1.setAdapter(myContactListAdapter); } } } public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) { // TODO Auto-generated method stub Log.d("1", "onItemClick"); fh=new FileHandler(); try { String bookName= arg0.getItemAtPosition(pos).toString(); fh.showBookPages(bookName); } catch(Exception e) { Log.d("1", "msg "+e.getMessage()); } } } class MyBookList extends BaseAdapter { Context context; private ArrayList<String> folders = new ArrayList<String>(); public MyBookList(ArrayList<String> mfolders, Context mcontext) { // TODO Auto-generated constructor stub Log.d("1", "MyBookList constructor 1"); this.folders = mfolders; this.context = mcontext; Log.d("1", "MyBookList constructor 2"+folders.size()); } public int getCount() { Log.d("1", "getCount"); // TODO Auto-generated method stub return folders.size(); } public Object getItem(int pos) { Log.d("1", "getItem"); // TODO Auto-generated method stub return folders.get(pos); } public long getItemId(int pos) { Log.d("1", "getItemId"); // TODO Auto-generated method stub return pos; } public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub try { Log.d("1", "MyBookList get view"); if (convertView == null) { Log.d("error","after Error 12"); LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); Log.d("error","after Error 13"); convertView = inflater.inflate(R.layout.list, null); } MainActivity.bookMenu = (TextView) convertView.findViewById(R.id.bookMenu); // MainActivity.address = (TextView) convertView.findViewById(R.id.tv4); Log.d("text", "Ok "+folders.size()+position+folders.get(position)); MainActivity.bookMenu.setText(folders.get(position)); Log.d("error","after Error4"); } catch(Exception e) { Log.d("1", "msg "+e.getMessage()); } Log.d("1", "onItemClick"); MainActivity.fh=new FileHandler(); convertView.setOnClickListener(new OnClickListener() { public void onClick(View v) { try { Log.d("1", "inside getview onclick "); String bookName= (String)getItem(position); MainActivity.fh.showBookPages(bookName); } catch(Exception e) { Log.d("1", "msg "+e.getMessage()); } } }); // convertView.setOnClickListener(new OnClickListener() { // // @Override // // public void onClick(View v) { // // TODO Auto-generated method stub // try // { // Log.d("1", "inside getview onclick "); // String bookName= (String)getItem(position); // MainActivity.fh.showBookPages(bookName); // } // catch(Exception e) // { // Log.d("1", "msg "+e.getMessage()); // } // } // }); return (convertView); } }
This is the xml file-
<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" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="30dp" android:textStyle="italic" android:text="Book Store" /> <ListView android:id="@+id/lv1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="50dp" android:layout_marginBottom="70dp"> </ListView> <Button android:id="@+id/b1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="10dp" android:text="Add New Book" /> </RelativeLayout>
Astha - OCPJP 6 (90%)
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Why error in this code
nullpointer when i try to initiate onclicklistener
virtual key board source code
Accessing child TextView inside ListView
getting null pointer
More...