last day (14 days later) » 

8:21 AM
-2
Q: how can i put conditions on ListView Item?

z.alI'm working on listview. I have one activity typemenu.java, where items are coming from server in listview e.g pizza, pasta, lasagna etc. If I click listview item pizza or pasta then on next activity there is another listview contains items which is related to pizza like bianca pizza or cheese p...

 
Post the entire logcat statements that occur around the error.
 
plz see d updated logcat
it says null pointer but the items are coming from database and everything works fine..only after putting condition its showing error
 
The logcat says the problem is occuring at line number 161 Can you post that line here in comments ? My text editor is not able to find it :P
 
if ( listview.getItemAtPosition(position).toString().equals("Bian‌​ca Pizza")||listview.getItemAtPosition(position).toString().equ‌​als("Cheese Pizza"))
i know its because of the above condition..thts d problem i cant figure out how to put correct condition
 
What happens if you try logging the value of just listview.getItemAtPosition(position)? Hint: Insert that line just before the condition and run your project.
 
8:21 AM
hmm ok i try this
Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference at com.example.zeba.broccoli.SubMenu$DownloadJSON.onItemClick(S‌​ubMenu.java:161)
 
Did you insert the line Log.d("ANDROID", listview.getItemAtPosition(position)) ?
 
k let me try ..but before the if condition
 
The error you posted just now is saying that the .toString() method is returning null, but according to you, it should return a String.
Yes. Before the if condition
Then post the logcat
 
wrong 2nd argument type. Found: 'java.lang.Object', required: 'java.lang.String..
after putting ur line it gives me red line with msg
 
Log.d("ANDROID", listview.getItemAtPosition(position)).toString());
 
8:30 AM
same red line with same msg
 
Can't be true
Log.d("ANDROID", listview.getItemAtPosition(position)).toString());
^this doesn't give you anything>?
There has to be some output for this line in the logcat
 
Log.d("ANDROID", listview.getItemAtPosition(position)).toString());
if ( listview.getItemAtPosition(position).toString().equals("Bianca Pizza"))
{
like this u want
 
Yes.
 
but 1st line gives me red line..
 
You mean Syntax Error?
 
8:32 AM
yes i told u
 
Log.d("ANDROID", listview.getItemAtPosition(position).toString());
Try this
 
ok
FATAL EXCEPTION: main
Process: com.example.zeba.broccoli, PID: 10580
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.example.zeba.broccoli.SubMenu$DownloadJSON.onItemClick(SubMenu.java:160)
at android.widget.AdapterView.performItemClick(AdapterView.java:339)
at android.widget.AbsListView.performItemClick(AbsListView.java:1532)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3700)
at android.widget.AbsListView$3.run(AbsListView.java:5684)
 
Wait
Can you explain why you are implementing the Adapter's onItemClickListener Interface inside your AsyncTask ClasS?
AsyncTask is for asynchronous operations which happen on a separate thread. onPostExecute() is only fired after the doInBackground() method has completed execution.
You need to implement, or in your case, move the entire onClick Code inside the onCreate Method of your Activity.
RIght now, the onPostExecute is not firing, which is why your listview variable is null.
 
but it was working fine without my if condition
all things are happing only after putting condition ...i m getting the listview item and on click of item the alertbox was also opening
 
Wow.
 
8:40 AM
and that alertbox is sending the customer requirements to server
that part is also working
all things r working fine
 
Interesting.
Try this:
 
wait i ll show my alertbox
 
Log.d("ANDROID", listview.toString());
All right.
 
k
 
Put that line before the if statement and check the logcat
 
8:41 AM
k
 
k
 
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.example.zeba.broccoli.SubMenu$DownloadJSON.onItemClick(SubMenu.java:161)
at android.widget.AdapterView.performItemClick(AdapterView.java:339)
at android.widget.AbsListView.performItemClick(AbsListView.java:1532)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3700)
at android.widget.AbsListView$3.run(AbsListView.java:5684)
at android.os.Handler.handleCallback(Handler.java:739)
 
Cool
so the alertview is firing
 
yup
 
What is that logcat output you jsut posted?
just*
 
8:44 AM
yes
 
See.
 
i got this aftr updated line
 
That means your listview is referring to a null object
 
yeah i knw but y this happeing ...its only happeing coz of condition ..otherwise onitem click the alertbox was there...
but here i have to make conditions for dieffrent diff alertboz as per the item
 
Yes. I get your point.
Yes. The use case is fine and it shoudl work.
Let me investigate.
Wait.
 
8:50 AM
i tried doing this way also but its just taking position value ...and showing diff diff alertboxes ...example if thr is cheese pizza then bianca pizza..n so on in list its showing the diff diff alertbox but here it shud show only that box which is related with pizza...
package com.example.zeba.broccoli;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
 
reading
 
the thing is it shud read the string and according to that shud show the box
 
That's because you are using variable position inside switch
position is a number
and pizza is also a number
 
yeah see i have declared global variable also
but i knw tht will nt work ..just tryin
thts y i tried if condition
 
Yes. wait.
Hey
try calling
parent.getItemAtPosition
If that gives you an eror
like method doesn't exist
then try
`this.getItemAtPosition()
the parent call should work, I guess.
Did it?
 
8:55 AM
ok
 
sure
 
ok let me try
 
yes. try and see if it works
 
Log.d("ANDROID",this.parent.getItemAtPosition());
like this ur saying?
 
yes
No No
Log.d("ANDROID", parent.getItemAtPosition());
^try that
Log.d("ANDROID", parent.getItemAtPosition(position));
 
8:59 AM
getItemAtPosition cannot applied
to ()
 
Log.d("ANDROID", parent.getItemAtPosition(position).toString());
Now?
 
wt
 
sure
 
FATAL EXCEPTION: main
Process: com.example.zeba.broccoli, PID: 12050
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.example.zeba.broccoli.SubMenu$DownloadJSON.onItemClick(SubMenu.java:160)
at android.widget.AdapterView.performItemClick(AdapterView.java:339)
at android.widget.AbsListView.performItemClick(AbsListView.java:1532)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3700)
at android.widget.AbsListView$3.run(AbsListView.java:5684)
 
Damn
wait
 
9:03 AM
ok i wll be here aftr an hour ...gt some work
 
Sure
 
10:01 AM
hi, r u thr
 

last day (14 days later) »