getView Returning Rows Multiple Times - android

I am doing some troubleshooting on my code, I have noticed that sometimes the listview row is returned multiple times when I post new data instead of once. I am reading contents of the listview row from database and projecting it into the else if statement of getView (commented out the Bitmap to test with textview, still having same problem)
getView:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row=convertView;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (request_my_username.equals(arrRequest_UserContact.get(position))) //YOU
{
if (Request_Message.get(position).equals("MESSAGE-FAILED-TO-POST"))
{
row = inflater.inflate(R.layout.activity_chatprivate_single_right, parent, false);
TextView txtMessage = (TextView) row.findViewById(R.id.textMessage);
txtMessage.setTypeface(null, Typeface.BOLD);
txtMessage.setText(Request_Message.get(position));
}
else if (Request_Message.get(position).contains("/storage/"))
{
/***
row = inflater.inflate(R.layout.activity_chatprivate_single_right_img, parent, false);
Bitmap bmp_ico = BitmapFactory.decodeFile(Request_Message.get(position));
ImageButton ib = (ImageButton) row.findViewById(R.id.ib);
ib.setImageBitmap(bmp_ico);
***/
row = inflater.inflate(R.layout.activity_chatprivate_single_right, parent, false);
TextView txtMessage = (TextView) row.findViewById(R.id.textMessage);
txtMessage.setText(Request_Message.get(position));
}
else
{
row = inflater.inflate(R.layout.activity_chatprivate_single_right, parent, false);
TextView txtMessage = (TextView) row.findViewById(R.id.textMessage);
txtMessage.setText(Request_Message.get(position));
}
}
Read MySQL:
private class JsonReadChatPrivate extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
}
return answer;
}
#Override
protected void onPostExecute(String result) {
adapter.notifyDataSetChanged();
try{
ListDrawer_readPrivateChat(); //has ConnectionException (when it cannot reach server)
}catch (Exception e){
}
}
}
public void accessWebService_readChatPrivate() {
JsonReadChatPrivate task = new JsonReadChatPrivate();
task.execute(new String[] { "http://website/php/file_to_read_db.php?pcontactSelected="+contactSelected+"&pIMEI="+IMEI+"&pmysql_room_id="+mysql_room_id});
}
public void ListDrawer_readPrivateChat() {
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("request_chat_private");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String request_message = jsonChildNode.optString("Message");
String request_time = jsonChildNode.optString("Time");
mysql_room_id = jsonChildNode.optString("_id");
request_last_user = jsonChildNode.optString("LastUser");
arrRequest_UserContact.add(request_last_user);
arrRequest_Message.add(request_message);
arrRequest_Time.add(request_time);
adapter.notifyDataSetChanged();
}
} catch (JSONException e) {
}
}
Listening every 5 seconds to see if there is any new data on MySQL and if there is, return only the contents from the last MySQL ID (so that I do not re-add items that exist onto the listview):
private void DBListern() {
accessWebService_readChatPrivate();
}
private void loop() {
handler.postDelayed(new Runnable() {
public void run() {
DBListern();
handler.postDelayed(this, 5000);
}
}, 5000);
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/bg_chat_2"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:transcriptMode="normal"
android:stackFromBottom="true"
android:divider="#null"
android:dividerHeight="0dp"
>
</ListView>
</LinearLayout>
...
With above said, when I post content as A then B then C it should return the correct output as A, B, C however it does this A, AA, BBB, CCCC <-- repeating the view with same data.

OK, I continued troubleshooting with logs and toasts, the problem was not the getView, it was actually the selecting of image from gallery, when doing so it calls onPauce() and after selecting an image it then calls onResume which runs the loop() call again while it as already running. All I did was cancel the previous loop (handler.removeCallbacksAndMessages(null);) as it will be called again with onResume
I admit my focus was more on the getView code of the listView, I never thought selecting an image calls system methods. Always good to work with logs.

Related

onResume Does Not Update ListView With New Data

I am trying to update a ListView on previous fragment after back button press. The onResume is called (verified with Toast) and the webservice runs (listView is displayed after it is cleared). The problem is that the ListView is still showing old values and not new value after accessWebService_getUsername is called. I verify the values from MySQL and even though the DB is updated, the ListView only returns old values.
#Override
public void onResume() {
Toast.makeText(getActivity(), "onResume", Toast.LENGTH_SHORT).show();
super.onResume();
adapter.clear();
getIMEI();
accessWebService_getUsername();
adapter.notifyDataSetChanged();
}
Update:
//ListView
ListView lv =(ListView)view.findViewById(R.id.listView);
adapter = new ContactsAdapter(getActivity(), arrRequest_Contact, arrRequest_NameSurname, arrRequest_MessageCount, arrRequest_Time, arrRequest_Image);
lv.setAdapter(adapter);
// Json
private class JsonGetUsername extends AsyncTask<String, Void, String> {
//Pending 01
private ProgressDialog dialog = new ProgressDialog(getActivity());
#Override
protected void onPreExecute() {
this.dialog.setMessage("Loading Contacts, Please Wait");
this.dialog.show();
}
#Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
// e.printStackTrace();
Toast.makeText(getActivity(),"Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}
#Override
protected void onPostExecute(String result) {
//Pending 02
if (dialog.isShowing()) {
dialog.dismiss();
}
adapter.notifyDataSetChanged();
try{
ListDrawer_getUsername(); //has ConnectionException (when it cannot reach server)
}catch (Exception e){
Toast.makeText(getActivity(), "Please check your connection..", Toast.LENGTH_LONG).show();
}
}
}// end async task
public void accessWebService_getUsername() {
JsonGetUsername task = new JsonGetUsername();
// passes values for the urls string array
task.execute(new String[] { "http://mywebsite/php/get_username.php?pIMEI="+IMEI});
}
// build hash set for list view
public void ListDrawer_getUsername() {
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("username_info");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
request_username = jsonChildNode.optString("Username");
}
accessWebService_getContacts();
} catch (JSONException e) {
System.out.println("Json Error Rooms" +e.toString());
//Toast.makeText(getApplicationContext(), "No Rooms To Load", Toast.LENGTH_SHORT).show();
}
}
UPDATE 2:
//ContactsAdpater
class ContactsAdapter extends ArrayAdapter<String>
{
Context context;
List<String> Request_Contact;
List<String> Request_NameSurname;
List<String> Request_MessageCount;
List<String> Request_Time;
List<String> Request_Image;
ContactsAdapter(Context c, List<String> Request_Contact, List<String> Request_NameSurname, List<String> Request_MessageCount, List<String> Request_Time, List<String> Request_Image)
{
super(c, R.layout.activity_contacts_single, R.id.textContact, Request_Contact);
this.context=c;
this.Request_Contact=Request_Contact;
this.Request_NameSurname=Request_NameSurname;
this.Request_MessageCount=Request_MessageCount;
this.Request_Time=Request_Time;
this.Request_Image=Request_Image;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row=convertView;
if(row==null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.activity_contacts_single, parent, false);
}
TextView txtContact = (TextView) row.findViewById(R.id.textContact);
TextView txtNameSurname = (TextView) row.findViewById(R.id.textNameSurname);
TextView txtMessageCount = (TextView) row.findViewById(R.id.textMessageCount);
TextView txtTime = (TextView) row.findViewById(R.id.textTime);
ImageView imageView = (ImageView) row.findViewById(R.id.imageView);
txtContact.setText(Request_Contact.get(position));
txtNameSurname.setText(Request_NameSurname.get(position));
txtMessageCount.setText(Request_MessageCount.get(position));
txtTime.setText(Request_Time.get(position));
Picasso.with(context).load(arrRequest_Image.get(position)).transform(new CircleTransform()).placeholder(R.drawable.ic_launcher).into(imageView);
return row;
}
}
You'll need to override the clear method in your ContactsAdapter to actually clear the lists you are storing your data in.
It looks like you'll need to clear all your lists, so if you add this to ContactsAdapter, your code should work as expected:
#Override
public void clear() {
super.clear();
Request_Contact.clear();
Request_NameSurname.clear();
Request_MessageCount.clear();
Request_Time.clear();
Request_Image.clear();
}

ListView Not Working On Real Device

I have a problem with my listview, it works on emulator but not on real device. I tested with 2 real devices and it does not populate. I get listview data from database using json. Json result is ok as it prints to logcat and populates listview on emulator.
ListView Java:
public class ActivityRequestsFrom extends MainActivity implements AdapterView.OnItemClickListener {
______________________________________________________________________________
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_requests_from);
lv = (ListView) findViewById(R.id.listRequests);
RequestsAdapter adapter = new RequestsAdapter(this, arrRequest_Name, arrRequest_Number,
arrRequest_Username, arrRequest_Result, imageId);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
}
______________________________________________________________________________
class RequestsAdapter extends ArrayAdapter<String>
{
Context context;
List<String> Request_Name;
List<String> Request_Number;
List<String> Request_Username;
List<String> Request_Result;
Integer[] imgid;
RequestsAdapter(Context c, List<String> Request_Name,
List<String> Request_Number, List<String> Request_Username,
List<String> Request_Result, Integer[] imgid)
{
super(c, R.layout.activity_requests_single, R.id.textName, Request_Name);
this.context=c;
this.Request_Name=Request_Name;
this.Request_Number=Request_Number;
this.Request_Username=Request_Username;
this.Request_Result=Request_Result;
this.imgid=imgid;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row=convertView;
if(row==null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.activity_requests_single, parent, false); }
TextView txtName = (TextView) row.findViewById(R.id.textName);
TextView txtNumber = (TextView) row.findViewById(R.id.textNumber);
TextView txtUsername = (TextView) row.findViewById(R.id.textUsername);
TextView txtResult = (TextView) row.findViewById(R.id.textResult);
ImageView imageView = (ImageView) row.findViewById(R.id.imageView);
Map<String, Integer> drawableMap = new HashMap<String, Integer>();
drawableMap.put("ok",R.drawable.request_pending_from);
drawableMap.put("pending",R.drawable.request_pending_from);
drawableMap.put("rejected",R.drawable.request_rejected_from);
drawableMap.put("blocked",R.drawable.request_blocked_from);
txtName.setText(Request_Name.get(position));
txtNumber.setText(Request_Number.get(position));
txtUsername.setText(Request_Username.get(position));
txtResult.setText(Request_Result.get(position));
//imageView.setImageResource(imgid[position]);
imageView.setImageResource(drawableMap.get(Request_Result.get(position).toLowerCase()));
return row;
}
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
TextView tvUsername = (TextView) view.findViewById(R.id.textUsername);
usernameSelected = tvUsername.getText().toString();
TextView tvResult = (TextView) view.findViewById(R.id.textResult);
resultSelected = tvResult.getText().toString();
if (resultSelected.equals("Pending"))
{
pendingOptions();
}
else if (resultSelected.equals("Rejected"))
{
rejectedOptions();
}
else if (resultSelected.equals("Blocked"))
{
blockedOptions();
}
else
{
}
}
ListView Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listRequest"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</ListView>
ListView Single Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/LLdummy"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="italic"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="#+id/textUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textNumber"
android:layout_alignParentRight="true"
android:layout_below="#+id/textNumber"
android:text="Username"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textName"
android:layout_below="#+id/textName"
android:text="Number"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/LLdummy"
android:layout_centerVertical="true"
android:src="#drawable/request_pending_to" />
Json
private class JsonReadTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
// e.printStackTrace();
Toast.makeText(getApplicationContext(),"Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}
#Override
protected void onPostExecute(String result) {
try{
ListDrwaer(); //has ConnectionException (when it cannot reach server)
}catch (Exception e){
Toast.makeText(getApplicationContext(), "Please check your connection..", Toast.LENGTH_LONG).show();
}
}
}// end async task
public void accessWebService() {
JsonReadTask task = new JsonReadTask();
// passes values for the urls string array
task.execute(new String[] { "http://server/file.php?pIMEI="+IMEI });
}
// build hash set for list view
public void ListDrwaer() {
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("request_info");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String request_name = jsonChildNode.optString("Request_Name");
String request_number = jsonChildNode.optString("Request_Number");
String request_username = jsonChildNode.optString("Request_Username");
String request_result = jsonChildNode.optString("Request_Result");
arrRequest_Name.add(request_name);
arrRequest_Number.add(request_number);
arrRequest_Username.add(request_username);
arrRequest_Result.add(request_result);
System.out.println("Request_Name: "+request_name);
System.out.println("Request_Number: "+request_number);
System.out.println("Request_Username: "+request_username);
System.out.println("Request_Result: "+request_result);
}
} catch (JSONException e) {
System.out.println("Json Error Requests" +e.toString());
Toast.makeText(getApplicationContext(), "No Requests Pending", Toast.LENGTH_SHORT).show();
}
}
Call Async
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_requests_from);
...
accessWebService();
//ListView
lv = (ListView) findViewById(R.id.listRequests);
RequestsAdapter adapter = new RequestsAdapter(this, arrRequest_Name, arrRequest_Number, arrRequest_Username, arrRequest_Result, imageId);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
The problem here is that the data isn't received when the Adapter is set on the ListView. This is because the data is populated in an AysncTask which is async hronous by nature. This means that it will run in the background while the other code runs (i.e. the ListView populating.
So, in onPostExecute() you need to use notifyDataSetChanged() to let the ListView know that there is new items to populate.
#Override
protected void onPostExecute(String result) {
// your code
adapter.notifyDataSetChanged(); // this line here
}// end async task
In your example, this will obviously require you making the Adapter a member variable or passing it to your AsyncTask.

Data varying in GridView in Android App

I am developing an Android app which displays information of different historical places. In my App, I have made a page to review and rate the place. The app is retrieving usernames, ratings, and comments from database and displaying it in a GridView. It is displaying correct number of comments but instead of displaying all the comments it duplicates some of the comments. Here is my code which retrieve data from database.
Can anyone tell what is the problem with my code??
class task extends AsyncTask<String, String, Void>
{
private ProgressDialog progressDialog = new ProgressDialog(Comments.this);
InputStream is = null ;
String result = "";
protected void onPreExecute()
{
if (get)
progressDialog.setMessage("Retrieving reviews...");
else
progressDialog.setMessage("Posting review...");
progressDialog.show();
progressDialog.setOnCancelListener(new OnCancelListener()
{
#Override
public void onCancel(DialogInterface arg0)
{
task.this.cancel(true);
}
});
}
#Override
protected Void doInBackground(String... params)
{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_select);
try
{
httpPost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
//read content
is = httpEntity.getContent();
}
catch (Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
}
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = "";
while((line=br.readLine())!=null)
{
sb.append(line+"\n");
}
is.close();
result=sb.toString();
}
catch (Exception e)
{
// TODO: handle exception
Log.e("log_tag", "Error converting result "+e.toString());
}
return null;
}
protected void onPostExecute(Void v)
{
try
{
if(get)
{
name=new ArrayList<String>();
comment=new ArrayList<String>();
rating=new ArrayList<Float>();
JSONArray Jarray = new JSONArray(result);
for(int i=0;i<Jarray.length();i++)
{
JSONObject Jasonobject = new JSONObject();
Jasonobject = Jarray.getJSONObject(i);
String names=null;
names=Jasonobject.getString("name");
name.add(names);
comment.add(Jasonobject.getString("comment"));
rating.add((float)Jasonobject.getDouble("rating"));
}
CustomGrid adapter = new CustomGrid(Comments.this, name,comment,rating);
grid.setAdapter(adapter);
get=false;
}
progressDialog.dismiss();
}
catch (Exception e)
{
// TODO: handle exception
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}
Adapter Code:
public class CustomGrid extends BaseAdapter
{
private Context mContext;
private final ArrayList<String> name;
private final ArrayList<String> comment;
private final ArrayList<Float> rating;
public CustomGrid(Context c,ArrayList<String> name, ArrayList<String> comment, ArrayList<Float> rating )
{
mContext = c;
this.name= name;
this.comment = comment;
this.rating=rating;
}
#Override
public int getCount()
{
// TODO Auto-generated method stub
return comment.size();
}
#Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid, null);
TextView textName = (TextView) grid.findViewById(R.id.grid_name);
TextView textComment = (TextView) grid.findViewById(R.id.grid_comment);
RatingBar ratingBar1 = (RatingBar)grid.findViewById(R.id.grid_rating);
textName.setText(name.get(position));
textComment.setText(comment.get(position));
ratingBar1.setRating(rating.get(position));
}
else
{
grid = (View) convertView;
}
return grid;
}
}
convertView is null just once. After you inflate and return it, due to the recycling mechanism of the GridView/ListView it will be never null again. What you are doing is assign to your TextViews the content of your dataset at position 0, and then return on of the pooled view with the same content over and over. Change your getView like:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.grid, null);
}
TextView textName = (TextView) convertView.findViewById(R.id.grid_name);
TextView textComment = (TextView) convertView.findViewById(R.id.grid_comment);
RatingBar ratingBar1 = (RatingBar)convertView.findViewById(R.id.grid_rating);
textName.setText(name.get(position));
textComment.setText(comment.get(position));
ratingBar1.setRating(rating.get(position));
return convertView;
}
also you probably want to look in the ViewHolder pattern, which makes your GridView/ListView scroll smoother

Nullpointerexception while updating UI

I'm reading some records from database and loading them in ListView. The ListView consist CheckBox and TextView. Loading is done over AsyncTask. This part of application works fine.
The next step is automatically checking some checkboxes according to some flags from database and here I get problem. I'm trying to check those items inside onPostExecute() and then I get error about NullPointerException. If I do same from, for example, setOnClickListener() of button widget than it works fine.
The question is how to check if ListView is populated, are checkboxes and TextView loaded and visible on screen?
I don't know if it will help the part of code where program breaks looks like:
for (j=0; j<3; j++)
{
LinearLayout itemLayout = (LinearLayout)listView.getChildAt(j); // Find by under LinearLayout
CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.ColChk);
for (k=0; k<rbmjere.size(); k++)
{
if (checkbox.getTag().toString() == rbmjere.get(k).toString())
{
checkbox.setChecked(true);
}
}
}
It breaks on that line:
LinearLayout itemLayout = (LinearLayout)listView.getChildAt(j);
I must mention that this works only if I manually press on button which runs code for automatic checking the boxes and this works only when all data is loaded.
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spckontrola_update);
listView = (ListView)findViewById(R.id.listView1);
btnPohrani = (Button)findViewById(R.id.btnPohrani);
btnProvjeri = (Button)findViewById(R.id.btnProvjeri);
btnProvjeri.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
new loadSPCKontrole().execute("FCN");
}
});
MyArrList = new ArrayList<HashMap<String, String>>();
public void fillData()
{
SimpleAdapter listadapter = new SimpleAdapter(this, MyArrList, R.layout.activity_list_row,
new String[] {"OpisMjere", "RbMjere"}, new int[] {R.id.ColOpis, R.id.ColCode});
listView = (ListView)findViewById(R.id.listView1);
listView.setAdapter(listadapter);
}
private class loadSPCKontrole extends AsyncTask<String, Void, Void>
{
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SPCUpdate.this);
pDialog.setMessage("Loading in progress ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected Void doInBackground(String... params) {
HashMap<String, String> map;
String k = params[0].toString();
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("sIDKategorija", k));
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.16.48" + "/spc/get_spcmjere.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection "+ e.toString());
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}
catch(Exception e)
{
Log.e("log_tag", "Error converting result " + e.toString());
}
int ct_id;
String ct_name;
try
{
jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
ct_id=json_data.getInt("RbMjere");
ct_name=json_data.getString("OpisMjere");
map = new HashMap<String, String>();
map.put("RbMjere", String.valueOf(ct_id));
map.put("OpisMjere", ct_name);
MyArrList.add(map);
}
}
catch(JSONException e1)
{
Log.e("Greška konvertiranja", e1.toString());
}
catch (ParseException e1)
{
e1.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
fillData();
listView.setAdapter(new SPCMjereAdapter(SPCUpdate.this));
pDialog.dismiss();
}
}
public class SPCMjereAdapter extends BaseAdapter
{
private Context context;
public SPCMjereAdapter(Context c)
{
context = c;
}
public int getCount() {
return MyArrList.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_list_row, null);
}
// ColID
TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis);
txtOpis.setText(MyArrList.get(position).get("OpisMjere") +".");
// ColCode
TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode);
txtRbMjere.setText(MyArrList.get(position).get("RbMjere"));
// ColChk
CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
Chk.setTag(MyArrList.get(position).get("RbMjere"));
return convertView;
}
}
And here is code how I get items that should be checked:
DB objDB = new DB();
ArrayList<Integer> rbmjere = objDB.getCheckedSPC(ID);
int k=0;
int j=0;
for (j=0; j<MyArrList.size(); j++)
{
LinearLayout itemLayout = (LinearLayout)listView.getChildAt(j); // Find by under LinearLayout
CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.ColChk);
for (k=0; k<rbmjere.size(); k++)
{
if (checkbox.getTag().toString() == rbmjere.get(k).toString())
{
checkbox.setChecked(true);
}
}
}
This code above works only when listview is populated with items and if this code is under OnClickListener() but it doesn't work if I run it from onPostExecute because it seems that all rows in listview are not loaded. So my question is what should I do to get information when is loading of all rows finished and after that check which items should be checked according to data that i get from database?
Because of view recycling, listView.getChildAt() will only return a view for the positions it is displaying, not severals.
you can check this other question here. it has the answer you are looking for:
ListView getChildAt returning null for visible children
In your case I think that piece of code should be inside your adapter's getView implementation.
Here you go with examples on
SimpleCursorAdapters
CustomCursorAdapters
And how to use them with your List.
First of all you know when the list is loaded by reading the number of items in your data set and if the listview's data set is equal to that, then all the data is loaded on the listview.. unfortunately I can't help you more with this as I can't see your data load code here..
Second, you should change:
for (j=0; j<3; j++)
with
int listChildSize = listView.getChildCount();
for (j = 0; j < listChildSize ; j++)
EDIT
jfs is right, you should add:
if (checkbox.getTag().toString() == rbmjere.get(k).toString())
{
checkbox.setChecked(true);
}
In a custom Adapter.
Here is a sample of custom adapter and also ViewHolder pattern.

JSON Android ListView

I build this webservice on netbeans,
package in.figures.on.mobile;
import db.koneksi.dbKoneksi;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import org.json.simple.JSONValue;
/**
*
* #author Setyadi
*/
#WebService()
public class AksesData {
/**
* Web service operation
*/
#WebMethod(operationName = "Kategori")
public String Kategori() {
//TODO write your implementation code here:
dbKoneksi con = new dbKoneksi();
Statement statement;
Properties properties;
List list = new ArrayList();
String sql = "SELECT idPrimary_key, kategori FROM kategori ";
ResultSet hasil;
String kategori = null;
try{
statement = con.getConnection().createStatement();
hasil = statement.executeQuery(sql);
while (hasil.next()) {
properties = new Properties();
properties.put("idPrimary_key", hasil.getString(1));
properties.put("kategori", hasil.getString(2));
list.add(properties);
}
kategori = JSONValue.toJSONString(list);
}
catch(Exception e){
}
return kategori;
}
}
And return a JSON like this
[{"idPrimary_key":"21ye21","kategori":"FirstCategory"},
{"idPrimary_key":"89oy89","kategori":"SecondCategory"},
{"idPrimary_key":"34ew34","kategori":"ThirdCategory"}]
And I try to consume in Android ListView like this, but still got errors,
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE transportSE = new HttpTransportSE(URL);
try {
transportSE.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
result = response.toString();
} catch (Exception e) {
e.printStackTrace();
}
String jsonAN = "{\"kat\":"+result+"}"; //try to build to be like this {"kat":[{blablablaJSON}]}
String kategoriJSONList[][] = new String[99][2];
String katList[] = new String[99]; //tobe shown on listview, derived from two dimensional array above.
try {
jsonObject = new JSONObject(jsonAN);
jsonArray = jsonObject.getJSONArray("kat");
for(int i=0; i < jsonArray.length() ; i++){
kategoriJSONList[i][0] = jsonArray.getJSONObject(i).getString("idPrimary_key").toString();
kategoriJSONList[i][1] = jsonArray.getJSONObject(i).getString("kategori").toString();
}
for(int i=0; i < jsonArray.length(); i++){
katList[i] = kategoriJSONList[i][1];
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ListView list = (ListView) findViewById(R.id.listKategori);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
WebServiceActivity.this, android.R.layout.simple_list_item_1,katList
);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
final String kategori = (String) ((TextView)arg1).getText();
Toast.makeText(WebServiceActivity.this, kategori,
Toast.LENGTH_LONG).show();
}
});
Need help how to consume the JSONValue that return as shown above to be shown as ListView.
I got stress in this days.
Thanks in advance.
Ok. Try this bellow code. It is full functional to me. You should implement the HttpRequest in the commented line. Pay atention to that the JSON array is hard-coded.
// the Adapter
public class ListViewAdapter extends BaseAdapter {
private Context context = null;
private List<String> fields = null;
public ListViewAdapter(Context context, JSONArray arr) {
this.context = context;
this.fields = new ArrayList<String>();
for (int i=0; i<arr.length(); ++i) {
try {
fields.add(arr.getJSONObject(i).toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
#Override
public int getCount() {
return fields.size();
}
#Override
public Object getItem(int position) {
return fields.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.itemlist, null);
TextView txt = (TextView) convertView.findViewById(R.id.ItemList_txt);
txt.setText(fields.get(position));
return convertView;
}
}
// the activity
public class ListViewActivity extends Activity {
public final String result = "[{\"idPrimary_key\":\"21ye21\",\"kategori\":\"FirstCategory\"},{\"idPrimary_key\":\"89oy89\",\"kategori\":\"SecondCategory\"},{\"idPrimary_key\":\"34ew34\",\"kategori\":\"ThirdCategory\"}]";
public final String obj = "{\"kat\":"+result+"}";
private ListViewAdapter adapter = null;
private ListView myList = null;
private JSONArray items = new JSONArray();
final Handler handler = new Handler() {
#Override
public void handleMessage(android.os.Message msg) {
if (msg.what == 0) { // server returned null, try again
loadFields();
} else if(msg.what == 1) { // error in json
// do something to treat it
} else if (msg.what == 2) { // ready to roll the list
adapter = new ListViewAdapter(ListViewActivity.this, items);
myList.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myList = (ListView) findViewById(R.id.Lists_notificationsListview);
loadFields();
}
private void loadFields() {
new Thread() {
#Override
public void run() {
Looper.prepare();
StringBuilder builder = new StringBuilder(obj);
if (builder != null) {
try {
// HERE, you should implement the HTTP request...
items = new JSONObject(obj).getJSONArray("kat");
handler.sendEmptyMessage(2);
} catch (JSONException e) {
handler.sendEmptyMessage(1);
}
} else {
handler.sendEmptyMessage(0);
}
Looper.loop();
}
}.start();
}
And the xml files:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical"
android:isScrollContainer="true">
<ListView
android:id="#+id/Lists_notificationsListview"
android:layout_width="fill_parent" android:layout_height="match_parent">
</ListView>
</RelativeLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView
android:id="#+id/ItemList_txt"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
</LinearLayout>
As result, it generates the following view:
Of course, you can customize it to create lists that you want, just parsing the jsons!
Hope that I've helped in some way...

Categories

Resources