Custom Dialog not working within a Adapter, Crashes App - java

I have a listview with a requestQueue that calls POST to mysqli database to get a array JSONobject. A base Adapter is use for the listview to layout the data. There a button on every item on the list. I try to figure out why the custom dialog is crashing the app. It might be how I call the dialog. I know showing the dialog is the cause for the crash.
Sensors_Activity.java
public class Sensors_Activity extends AppCompatActivity
{
#Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensors_);
final ListView S_lv = (ListView) findViewById(R.id.S_ListView);
RequestQueue requestQueue;
requestQueue = (RequestQueue) Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(POST, DBStrings.DataUrl, null, new Response.Listener<JSONObject>()
{
#Override
public void onResponse(JSONObject response)
{
....
SensorsAdapter sensorsAdapter = new SensorsAdapter(getApplicationContext(),id,room_id,state,type,value);
S_lv.setAdapter(sensorsAdapter);
....
});//Response ends here
requestQueue.add(jsonObjectRequest);
}//OnCreate ends here
}
SensorAdapter
package com.example.djwiz.test5;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import java.security.AccessController;
import java.util.zip.Inflater;
import static java.security.AccessController.getContext;
public class SensorsAdapter extends BaseAdapter
{
LayoutInflater mInflater;
String[] Id_Array;
String[] Room_Id_Array;
String[] State_Array;
String[] Type_Array;
String[] Value_Array;
Context TheContext;
public SensorsAdapter(Context c, String[] I, String[] R, String[] S, String[] T, String[] V)
{
Id_Array = I;
Room_Id_Array = R;
State_Array = S;
Type_Array = T;
Value_Array = V;
TheContext = c;
mInflater = (LayoutInflater.from(c));
}
#Override
public int getCount()
{
return Id_Array.length;
}
#Override
public Object getItem(int position)
{
return Id_Array[position];
}
#Override
public long getItemId(int position)
{
return position;
}
#Override
public View getView(final int positon, View convertView, ViewGroup parent)
{
View v = mInflater.inflate(R.layout.sensor_list, null);
TextView idtv = (TextView) v.findViewById(R.id.S_id_TV);
TextView roomtv = (TextView) v.findViewById(R.id.S_room_id_TV);
final TextView typetv = (TextView) v.findViewById(R.id.S_type_TV);
TextView valuetv = (TextView) v.findViewById(R.id.S_value_TV);
TextView statetv = (TextView) v.findViewById(R.id.S_state_TV);
final Button Setting_btn = (Button) v.findViewById(R.id.S_Settings_Btn);
Setting_btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(TheContext, typetv.getText() +" was click at " + (positon +1) , Toast.LENGTH_SHORT).show();
String type;
type = typetv.getText().toString();
if(type.equals("lock"))
{
AlertDialog.Builder S_LockBuilder;
LayoutInflater inflater = (LayoutInflater) TheContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
S_LockBuilder = new AlertDialog.Builder(TheContext);
final View S_LockView;
S_LockView = inflater.inflate(R.layout.linear_lock_dialog,null);
TextView L_State_TV;
TextView L_Value_TV;
ToggleButton L_T_BTN;
L_T_BTN = (ToggleButton) S_LockView.findViewById(R.id.D_TOGGLE_BTN);
S_LockBuilder.setView(S_LockView);
AlertDialog Lockdialog = S_LockBuilder.create();
Lockdialog.show();
}
}
});
String ID = Id_Array[positon];
String ROOM = Room_Id_Array[positon];
String STATE = State_Array[positon];
String TYPE = Type_Array[positon];
String VALUE = Value_Array[positon];
idtv.setText(ID);
roomtv.setText(ROOM);
typetv.setText(TYPE);
valuetv.setText(VALUE);
statetv.setText(STATE);
return v;
}
}
Logcat
02-20 00:43:22.126 13775-13775/com.example.djwiz.test5 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.djwiz.test5, PID: 13775
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:765)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.app.Dialog.show(Dialog.java:330)
at com.example.djwiz.test5.SensorsAdapter$1.onClick(SensorsAdapter.java:106)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Any help would be graceful.
Thank You

First you have to change
SensorsAdapter sensorsAdapter = new
SensorsAdapter(Sensors_Activity.this,id,room_id,state,type,value);
S_lv.setAdapter(sensorsAdapter);
please pass activity not context.
Then in your adapter change
Context TheContext;
to
Activity theActivity
Then pass your theActivity to dialog
S_LockBuilder = new AlertDialog.Builder(theActivity);

You need an Activity Context for Dialogs. An Application Context does not work.

Write like this:
SensorsAdapter sensorsAdapter = new SensorsAdapter(Sensors_Activity.this,id,room_id,state,type,value);
S_lv.setAdapter(sensorsAdapter);

Related

How do I access/alter views within a listview item from a different item in the same listview?

I have a small app that uses a ListView, each item having a play button, an open/close button, and an ImageView whose visibilty is set to View.GONE. When I press the open/close button, it should set the ImageView in that one list item to View.VISIBLE, change the open/close button, and close any other list items that are displaying the ImageView as VISIBLE and change their open/close button to reflect closed. This is my onClick in my custom ArrayAdapter.
boolean open = false;
imgOpen_Close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!open){
openClose.setImageResource(R.drawable.open_arrow);
mainImage.setVisibility(View.VISIBLE);
open = true;
}else{
openClose.setImageResource(R.drawable.close_arrow);
mainImage.setVisibility(View.VISIBLE);
open = false;
}
});
How do I access a View in one list item from another list item?
Entire Custom ArrayAdapter.
package net.androidbootcamp.mypersonalplaylist;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] songs;
private final Integer[] imgID;
MediaPlayer mpKickStart, mpGirlsGirls, mpHomeSweetHome;
public MyListAdapter(Activity context, String[] songs, Integer[] imgID){
super(context, R.layout.list_view_custom, songs);
this.context=context;
this.songs=songs;
this.imgID=imgID;
}
public View getView(final int position, View view, ViewGroup parent){
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_view_custom, null, true);
TextView txtSongName = rowView.findViewById(R.id.txtSongName);
final ImageView imgCoverArt = rowView.findViewById(R.id.imgCoverArt);
final ImageView imgPlayButton = rowView.findViewById(R.id.imgPlayButton);
final ImageView imgOpen_Close = rowView.findViewById(R.id.imgOpen_Close);
txtSongName.setText(songs[position]);
imgCoverArt.setImageResource(imgID[position]);
mpKickStart = new MediaPlayer();
mpKickStart = MediaPlayer.create(context, R.raw.kickstartmyheart);
mpGirlsGirls = new MediaPlayer();
mpGirlsGirls = MediaPlayer.create(context, R.raw.girlsgirlsgirls);
mpHomeSweetHome = new MediaPlayer();
mpHomeSweetHome = MediaPlayer.create(context, R.raw.homesweethome);
imgPlayButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (position){
case 0:
break;
case 1:
break;
case 2:
break;
default:
break;
}
}
});
imgOpen_Close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("POSITION", "Position Clicked: " + position);
}
});
return rowView;
}
}
MainActivity
package net.androidbootcamp.mypersonalplaylist;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
ListView list;
String[] songs = {"Name 1", "Name 2", "Name 3"};
Integer[] imgID = {R.drawable.kickstart_my_heart, R.drawable.girls_girls_girls, R.drawable.home_sweet_home};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyListAdapter adapter = new MyListAdapter(this, songs, imgID);
list = findViewById(R.id.listView);
list.setAdapter(adapter);
}
}

SetOnClickListener Not Working When Button Clicked in Fragment

I have a button inside a fragment that is inside an activity. The fragment contains Edittext for user to enter input then press a button and some action is processed. When I click the button I don't even see Log.v output on my logcat tab. That means android is not detecting the onClickListener event. I wonder if is because that event needs to happen in the activity thread and the fragment works in another thread. If someone could clarify how to get this working?
Activity
public class TracerouteActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_traceroute);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}
Fragment
package org.pctechtips.netdroid.traceroute;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import static android.content.ContentValues.TAG;
public class TraceRouteFragment extends Fragment {
public static final String tag = "TraceroutePing";
public static final String INTENT_TRACE = "INTENT_TRACE";
private Button buttonLaunch;
private EditText editTextPing;
private ProgressBar progressBarPing;
private ListView listViewTraceroute;
private TraceListAdapter traceListAdapter;
protected ProgressDialog scanProgressDialog;
private TracerouteWithPing tracerouteWithPing;
private final int maxTtl = 40;
private List<TracerouteContainer> traces;
/**
* onCreate, init main components from view
*/
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.traceroute, container, false);
this.tracerouteWithPing = new TracerouteWithPing(this);
this.traces = new ArrayList<TracerouteContainer>();
this.buttonLaunch = (Button) view.findViewById(R.id.buttonLaunch);
this.editTextPing = (EditText) view.findViewById(R.id.editTextPing);
this.listViewTraceroute = (ListView) view.findViewById(R.id.listViewTraceroute);
this.progressBarPing = (ProgressBar) view.findViewById(R.id.progressBarPing);
traceListAdapter = new TraceListAdapter(getContext());
listViewTraceroute.setAdapter(traceListAdapter);
buttonLaunch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (editTextPing.getText().length() == 0) {
Toast.makeText(getContext(), getString(R.string.no_text_trace), Toast.LENGTH_SHORT).show();
} else {
traces.clear();
traceListAdapter.notifyDataSetChanged();
startProgressBar();
hideSoftwareKeyboard(editTextPing);
tracerouteWithPing.executeTraceroute(editTextPing.getText().toString(), maxTtl);
}
Log.v(tag, "traceroute to " + editTextPing.getText().toString());
}
});
return view;
}
/**
* initView, init the main view components (action, adapter...)
*/
private void initView() {
}
/**
* Allows to refresh the listview of traces
*/
/*public void refreshList(TracerouteContainer trace) {
final TracerouteContainer fTrace = trace;
runOnUiThread(new Runnable() {
#Override
public void run() {
traces.add(fTrace);
traceListAdapter.notifyDataSetChanged();
}
});
}*/
/**
* The adapter of the listview (build the views)
*/
public class TraceListAdapter extends BaseAdapter {
private Context context;
public TraceListAdapter(Context c) {
context = c;
}
public int getCount() {
return traces.size();
}
public TracerouteContainer getItem(int position) {
return traces.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
// first init
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.item_list_trace, null);
TextView textViewNumber = (TextView) convertView.findViewById(R.id.textViewNumber);
TextView textViewIp = (TextView) convertView.findViewById(R.id.textViewIp);
TextView textViewTime = (TextView) convertView.findViewById(R.id.textViewTime);
ImageView imageViewStatusPing = (ImageView) convertView.findViewById(R.id.imageViewStatusPing);
// Set up the ViewHolder.
holder = new ViewHolder();
holder.textViewNumber = textViewNumber;
holder.textViewIp = textViewIp;
holder.textViewTime = textViewTime;
holder.imageViewStatusPing = imageViewStatusPing;
// Store the holder with the view.
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
TracerouteContainer currentTrace = getItem(position);
if (position % 2 == 1) {
convertView.setBackgroundResource(R.drawable.table_odd_lines);
} else {
convertView.setBackgroundResource(R.drawable.table_pair_lines);
}
if (currentTrace.isSuccessful()) {
holder.imageViewStatusPing.setImageResource(R.drawable.ic_check_green_24dp);
} else {
holder.imageViewStatusPing.setImageResource(R.drawable.ic_close_red_24dp);
}
holder.textViewNumber.setText(position + "");
holder.textViewIp.setText(currentTrace.getHostname() + " (" + currentTrace.getIp() + ")");
holder.textViewTime.setText(currentTrace.getMs() + "ms");
return convertView;
}
// ViewHolder pattern
class ViewHolder {
TextView textViewNumber;
TextView textViewIp;
TextView textViewTime;
ImageView imageViewStatusPing;
}
}
/**
* Hides the keyboard
*
* #param currentEditText The current selected edittext
*/
public void hideSoftwareKeyboard(EditText currentEditText) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(currentEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
public void startProgressBar() {
progressBarPing.setVisibility(View.VISIBLE);
getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
scanProgressDialog = new ProgressDialog(getContext(), R.style.NewDialog);
scanProgressDialog.setCancelable(false);
scanProgressDialog.getWindow().setGravity(Gravity.CENTER);
scanProgressDialog.setTitle("Tracing.... Please wait");
scanProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
scanProgressDialog.show();
}
public void stopProgressBar() {
// progressBarPing.setVisibility(View.GONE);
// getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
scanProgressDialog.dismiss();
}
#Override
public void onPause() {
super.onPause();
}
}
In your buttonLaunch.setOnClickListener() method parameter, you have used ' new View.onClickListener(){}', you have to change this to 'new OnClickListener(){}', everything else inside it will be same.
Check out this post for more:
Android Fragment onClick button Method
It should work. Check your adapter to be sure it works fine. Also it best you show us your logs.
try using
buttonLunch = (ImageButton) getActivity().findViewById(R.id.buttonLaunch);

Android toggle button save state custom listview

Hello friend i am new in android i want to know how to save ToggleButton state in custom ArrayAdapter i create custom ListView with help of ArrayAdapter now i want how save its state in Adapter here is my code pleae explain what logic used to achieve this goal i am on beginner level don't how to achieve this goal?
package bible.swordof.God;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.opengl.Visibility;
import android.preference.PreferenceManager;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import com.amulyakhare.textdrawable.TextDrawable;
import com.amulyakhare.textdrawable.util.ColorGenerator;
import com.hitomi.cmlibrary.CircleMenu;
import com.hitomi.cmlibrary.OnMenuSelectedListener;
import com.hitomi.cmlibrary.OnMenuStatusChangeListener;
import java.util.List;
import java.util.Locale;
import es.dmoral.toasty.Toasty;
import static android.content.Context.MODE_PRIVATE;
import static android.database.sqlite.SQLiteDatabase.CONFLICT_NONE;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
import static android.support.constraint.Constraints.TAG;
import static android.support.v4.content.ContextCompat.createDeviceProtectedStorageContext;
import static android.support.v4.content.ContextCompat.startActivity;
public class FullverseAdopter extends ArrayAdapter < String > {
private ALLVERSE activity;
private List < String > versenumber;
private List < String > verseid;
private List < String > verselist;
private List < String > refernce;
TextToSpeech textToSpeech;
private DatabaseHelper mDBHelper;
private SQLiteDatabase mDb;
private boolean save;
public static final String MyPREFERENCES = "MyPrefs";
//check for availabe language
int result;
public FullverseAdopter(ALLVERSE context, int resource, List < String > versenumber, List < String > verselist, List < String > refernce, List < String > verseid) {
super(context, resource, versenumber);
this.activity = context;
this.versenumber = versenumber;
this.verselist = verselist;
this.refernce = refernce;
this.verseid = verseid;
}
#Override
public int getCount() {
return versenumber.size();
}
#Override
public String getItem(int position) {
return versenumber.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// If holder not exist then locate all view from UI file.
if (convertView == null) {
// inflate UI from XML file
convertView = inflater.inflate(R.layout.versedisplayrow, parent, false);
// get all UI view
holder = new ViewHolder(convertView);
// set tag for holder
holder.versenumber = (TextView) convertView.findViewById(R.id.versenumber);
holder.verselist = (TextView) convertView.findViewById(R.id.verse);
convertView.setTag(holder);
} else {
// if holder created, get tag from view
holder = (ViewHolder) convertView.getTag();
}
holder.versenumber.setText(versenumber.get(position));
holder.verselist.setText(verselist.get(position));
//share verse
holder.share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toasty.info(activity, "Sharing a verse.", Toast.LENGTH_SHORT, true).show();
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, refernce.get(position) + ":" + versenumber.get(position) + '\n' + verselist.get(position));
sendIntent.setType("text/plain");
activity.startActivity(sendIntent);
}
});
textToSpeech = new TextToSpeech(activity, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
result = textToSpeech.setLanguage(Locale.ENGLISH);
} else {
Toast.makeText(activity, "YOUR DEVICE NOT SUPPORTED", Toast.LENGTH_SHORT).show();
}
}
});
//My toggle button
holder.bookmark.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
} else {
}
}
});
//mDBHelper = new DatabaseHelper(activity);
//mDb = mDBHelper.getWritableDatabase();
//ContentValues contentValues=new ContentValues();
//contentValues.put("id",verseid.get(position));
//contentValues.put("bookname",refernce.get(position));
//contentValues.put("versenumber",versenumber.get(position));
//contentValues.put("verse",verselist.get(position));
//long check=mDb.insert("favourite",null,contentValues);
//Log.d("MY_TAG","DB IS NOW "+check);
//Toasty.success(activity, "Added in favouite"+check, Toast.LENGTH_SHORT, true).show();
holder.speakverse.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(activity, "I AM CLICKED", Toast.LENGTH_SHORT).show();
if (result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA) {
Toast.makeText(activity, "Language not supported or Missing", Toast.LENGTH_SHORT).show();
} else {
textToSpeech.speak(verselist.get(position), TextToSpeech.QUEUE_FLUSH, null);
}
}
});
/* holder.removebookmark.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDBHelper = new DatabaseHelper(activity);
mDb = mDBHelper.getWritableDatabase();
// long delete= mDb.delete("favourite","id=?",new String[]{verseid.get(position)});
//Toasty.error(activity, "Remove in favouite"+delete, Toast.LENGTH_SHORT, true).show();
}
});*/
return convertView;
}
static class ViewHolder {
private ToggleButton favourite;
private TextView versenumber;
private TextView verselist;
private CircleMenu circleMenu;
private ImageView share;
//toggle button
public ToggleButton bookmark;
private boolean defaultvalue;
private ALLVERSE activity;
private ImageView speakverse;
public ViewHolder(View v) {
versenumber = (TextView) v.findViewById(R.id.versenumber);
verselist = (TextView) v.findViewById(R.id.verse);
share = (ImageView) v.findViewById(R.id.share);
bookmark = (ToggleButton) v.findViewById(R.id.adbookmark);
speakverse = (ImageView) v.findViewById(R.id.speakverse);
}
}
}
Ideally an adapter is an object to adapt a particular dataset to render a view within a listview. So you should have a data structure (class object) for every row visible in that list view and put it in an array list. Based on the position in getView method, retrieve the data for the row and initialize views from this data. Now this data set can be saved on server or in local memory in any format preferably in JSON and can be retrieved back in future.

Passing String array to another Activity with Custom View

I'm having an issue passing an array into a listview
I can pass an image to the second activty but when I
try to pass an array to the listview I get a lang.NullPointerException error
and the app crashes.
any helpful input would be great Thanks
here is the code
Main Activity
package com.example.androidlistview;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView listView;
// Defined the text values to show in ListView
String[] values = new String[]{"Barack Obama","Donald Trump","Bill Clinton",
"Hillary Clinton","Joe Biden"};
// Defined the image ids to show in ListView
Integer[] images = {
R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,
R.drawable.image5
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create the custom view
CustomView adapter = new CustomView(MainActivity.this, values, images);
// Get ListView object from xml
listView = (ListView) findViewById(R.id.container);
//set adapter
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
if(position == 0){
Intent contact = new Intent(MainActivity.this, Details.class);
String[] details= new String[]{"Barack Obama","The White House","1-234-567-8900",
"Barack.Obama#whitehouse.com","note"};
Integer image = images[0];
contact.putExtra("image", image);
contact.putExtra("details", details);
startActivity(contact);
}
if(position == 1){
Intent contact = new Intent(MainActivity.this, Details.class);
String[] details= new String[] {"Donald Trump","Trump Tower","1-234-567-8901",
"Donald.Trump#whitehouse.com","note"};
Integer image = images[1];
contact.putExtra("image", image);
contact.putExtra("details", details);
startActivity(contact);
}
if(position == 2){
Intent contact = new Intent(MainActivity.this, Details.class);
String[] details = new String[] {"Bill Clinton","New York","1-234-567-8902",
"Bill.Clinton#whitehouse.com","note"};
Integer image = images[2];
contact.putExtra("image", image);
contact.putExtra("details", details);
startActivity(contact);
}
if(position == 3){
Intent contact = new Intent(MainActivity.this, Details.class);
String[] details = new String[] {"Hillary Clinton","New Jersey","1-234-567-8903",
"Hillary.Clinton#whitehouse.com","note"};
Integer image = images[3];
contact.putExtra("image", image);
contact.putExtra("details", details);
startActivity(contact);
}
if(position == 4){
Intent contact = new Intent(MainActivity.this, Details.class);
String[] details= new String[] {"Joe Biden","OHIO","1-234-567-8903",
"Joe.Biden#whitehouse.com","note"};
Integer image = images[4];
contact.putExtra("image", image);
contact.putExtra("details", details);
startActivity(contact);
}
}
});
}
}
Second Activity I want to pass the array to
package com.example.androidlistview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Details extends Activity {
ListView list;
String []web={"Name:","Address:","Phone Number:","Email:","Note:"};
int image;
String[] details;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
// create the custom view
CustomList Adapter= new CustomList(Details.this, web, details);//, details should go here
// Get ListView object from xml
list=(ListView)findViewById(R.id.list);
//set adapter
list.setAdapter(Adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
}
});
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
image= bundle.getInt("image");
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(image);
}
}
}
and the custom view i'm using for the Second Activity details
package com.example.androidlistview;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomList extends ArrayAdapter<String>{
private final Activity mContext;
private final String[] web;
private final String[] details;
public CustomList(Activity mContext,String[] web, String[] details){ //, String[] details should go here
super(mContext, R.layout.list_single, web);
this.mContext=mContext;
this.web=web;
this.details=details;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = mContext.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
txtTitle.setText(web[position]);
TextView txtTitle1 = (TextView)rowView.findViewById(R.id.txt1);
txtTitle1.setText(details[position]);
return rowView;
}
}
This is what i did to fix this
Main Activity
package com.example.androidcontacts;
import android.app.ListActivity;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends ListActivity {
ListView mylist;
SearchView searchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Uri allContacts = Uri.parse("content://"+"com.example.androidcontacts.Books" + "/Contact");
Cursor c;
CursorLoader cursorLoader = new CursorLoader(
this,allContacts,null,null,null,null);
c = cursorLoader.loadInBackground();
String[] columns = new String[] {Contact._ID,Contact.TITLE,Contact.AUTHOR,Contact.YEAR,Contact.IMAGE};
int[] views = new int[] {R.id.contactID,R.id.contactName,R.id.contactAuthor,R.id.contactYear,R.id.Image};
SimpleCursorAdapter adapter;
adapter = new SimpleCursorAdapter(this, R.layout.activity_main,
c, columns, views,CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
this.setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView list, View view, int position, long id) {
//Intent i=new Intent(MainActivity.this,Details.class);
//startActivity(i);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == R.id.action_search){
Intent intent = new Intent(this, Search.class);
startActivity(intent);
finish();
return true;
}
if(id == R.id.action_add){
Intent intent = new Intent(this, Add.class);
startActivity(intent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Second activity Details
package com.example.androidlistview;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class Details extends Activity {
//Variables for Bundle
int image;
String name;
String address;
String phone;
String email;
String note;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
Button SMS = (Button) findViewById(R.id.button1);
SMS.setOnClickListener(new OnClickListener() {
//when button1 is clicked open messenger
#Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", phone, null)));
}
});
Button Email = (Button) findViewById(R.id.button2);
Email.setOnClickListener(new OnClickListener() {
//when button2 is clicked open email
#Override
public void onClick(View v) {
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
startActivity(Intent.createChooser(emailIntent, "Choose Email Client"));
}
});
Button Phone = (Button) findViewById(R.id.button3);
Phone.setOnClickListener(new OnClickListener() {
//when button3 is clicked start call
#Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null)));
}
});
// get details passed from MainActivity
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
image= bundle.getInt("image");
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(image);
name = bundle.getString("name");
TextView nameView = (TextView) findViewById(R.id.name1);
nameView.setText(name);
address = bundle.getString("address");
TextView addressView = (TextView) findViewById(R.id.address1);
addressView.setText(address);
phone = bundle.getString("phone");
TextView phoneView = (TextView) findViewById(R.id.phone1);
phoneView.setText(phone);
email = bundle.getString("email");
TextView emailView = (TextView) findViewById(R.id.email1);
emailView.setText(email);
note = bundle.getString("note");
TextView noteView = (TextView) findViewById(R.id.note1);
noteView.setText(note);
}
}
}
My Custom View
package com.example.androidlistview;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomView extends ArrayAdapter<String>{
private final Activity context;
private final String[] values;
private final Integer[] images;
//Constructor
public CustomView(Activity context,String[] values, Integer[] images) {
super(context, R.layout.single_row, values);
this.context = context;
this.values = values;
this.images = images;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.single_row, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
txtTitle.setText(values[position]);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
imageView.setImageResource(images[position]);
return rowView;
}
}

Issues with Searching ListView (Android)

I have been having trouble trying to get my Searchbox working to search the Listview for appropriate rows, and then displaying as required.
Basically, when I type in the searchbox, either the app crashed or gives me 'null object reference' errors from various files (ListView.java, TextView.java etc..)
I suspect it has a lot to do with inexperience on comparing strings between the EditText and the array created from JSON values. Displaying the listview is fine, searching it has been a frustrating hurdle. Would appreciate any help possible, thanks!
My code sections are as below,
ListViewActivity.java
package com.example.myapplication;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.util.Log;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import com.example.myapplication.WaterWellRigsJsonUrl;
import com.example.myapplication.Rig;
public class ListViewActivity extends Activity {
private ListView listview;
private ArrayList<Rig> Rigs;
private ArrayList<Rig> RigsTemp;
private ArrayAdapter<Rig> adapter;
private ArrayAdapter<Rig> adapter2;
private EditText et;
String searchString = "";
private final static String TAG = ListViewActivity.class.getSimpleName();
private final static String url = "http://www.world-rigs.com/waterwellrigs/json.php";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
listview = (ListView) findViewById(R.id.listview);
setListViewAdapter();
getDataFromInternet();
final EditText myFilter = (EditText) findViewById(R.id.myFilter);
myFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//adapter.getFilter().filter(s);
String searchString = s.toString();
if (s.length() > 0) {
for (int i = 0; i < Rigs.size();i++) {
if (searchString.equalsIgnoreCase(Rigs.get(i).getName())) {
Rig rigtemp = new Rig();
rigtemp.setName(Rigs.get(i).getName());
rigtemp.setImageUrl(Rigs.get(i).getImageUrl());
rigtemp.setRigId(Rigs.get(i).getRigId());
RigsTemp.add(rigtemp);
Log.e("myTag2", "value:" + RigsTemp.size());
}
}
}
adapter2 = new CustomListViewAdapter(ListViewActivity.this, R.layout.item_listview, RigsTemp);
listview.setAdapter(adapter2);
}
});
}
private void getDataFromInternet() {
new WaterWellRigsJsonUrl(this, url).execute();
}
private void setListViewAdapter() {
Rigs = new ArrayList<Rig>();
adapter = new CustomListViewAdapter(this, R.layout.item_listview, Rigs);
listview.setAdapter(adapter);
listview.setTextFilterEnabled(true);
}
//parse response data after asynctask finished
public void parseJsonResponse(String result) {
Log.i(TAG, result);
try {
JSONObject json = new JSONObject(result);
JSONArray jArray = new JSONArray(json.getString("rig_array"));
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
Rig rig = new Rig();
rig.setName(jObject.getString("name"));
rig.setImageUrl(jObject.getString("image"));
rig.setRigId(jObject.getString("rigid"));
Rigs.add(rig);
Log.e("myTag", Rigs.get(i).getRigId());
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
CustomListViewAdapter.java
package com.example.myapplication;
import java.util.List;
import android.app.Activity;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.myapplication.Rig;
import com.loopj.android.image.SmartImageView;
import com.squareup.picasso.Picasso;
public class CustomListViewAdapter extends ArrayAdapter<Rig> {
private Activity activity;
public CustomListViewAdapter(Activity activity, int resource, List<Rig> rigs) {
super(activity, resource, rigs);
this.activity = activity;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// If holder not exist then locate all view from UI file.
if (convertView == null) {
// inflate UI from XML file
convertView = inflater.inflate(R.layout.item_listview, parent, false);
// get all UI view
holder = new ViewHolder(convertView);
// set tag for holder
convertView.setTag(holder);
} else {
// if holder created, get tag from view
holder = (ViewHolder) convertView.getTag();
}
Rig rig = getItem(position);
holder.name.setText(rig.getName());
holder.authorName.setText("WR" + rig.getRigId());
Picasso.with(activity).load(rig.getImageUrl()).into(holder.image);
return convertView;
}
private static class ViewHolder {
private TextView name;
private TextView authorName;
private ImageView image;
public ViewHolder(View v) {
name = (TextView) v.findViewById(R.id.title);
image = (SmartImageView) v.findViewById(R.id.thumbnail);
// SmartImageView image = (SmartImageView) v.findViewById(R.id.my_image);
authorName = (TextView) v.findViewById(R.id.author);
}
}
}
Also getting the following errors, which do get frustrating but I think it has something to do with null reference in my Adapter or RigsTemp array while searching...
01-30 13:17:45.300 16835-16835/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 16835
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
at android.widget.ListView.setAdapter(ListView.java:487)
at com.example.myapplication.ListViewActivity$1.onTextChanged(ListViewActivity.java:84)
at android.widget.TextView.sendOnTextChanged(TextView.java:7663)
at android.widget.TextView.handleTextChanged(TextView.java:7723)
at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9440)
at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:964)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:515)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:454)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:33)
at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:685)
at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:445)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:340)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
You don't need to create two adapters and initialize them again and again. Create just one adapter and use notifyDataSetChanged to change data and update ListView
private ArrayList<Rig> RigsTemp;
private ArrayAdapter<Rig> adapter;
Change your function as follows:
private void setListViewAdapter() {
Rigs = new ArrayList<Rig>();
RigsTemp = new ArrayList<Rig>();
adapter = new CustomListViewAdapter(ListViewActivity.this, R.layout.item_listview, RigsTemp);
listview.setAdapter(adapter);
listview.setTextFilterEnabled(true);
}
And Change the TextWatcher code with following code:
public void onTextChanged(CharSequence s, int start, int before, int count) {
String searchString = myFilter.getText().toString();
RigsTemp.clear();
if (s.length() > 0) {
for (int i = 0; i < Rigs.size(); i++) {
if (searchString.equalsIgnoreCase(Rigs.get(i).getName())) {
Rig rigtemp = new Rig();
rigtemp.setName(Rigs.get(i).getName());
rigtemp.setImageUrl(Rigs.get(i).getImageUrl());
rigtemp.setRigId(Rigs.get(i).getRigId());
RigsTemp.add(rigtemp);
}
}
} else {
// Only if you want to show all results when user has not entered anything in EditText
// Else remove this line to show empty ListView at start
RigsTemp.addAll(Rigs);
}
adapter.notifyDataSetChanged();
}

Categories