Android Studio search bar list view with json data - java

Im trying to implement search bar to search an idea by title name and then press a button. Lets say I put in the search field the letter T, and clicked search.
I want the list view to update with only ideas that begin with the letter T.
Can anybody help a brother out? Thanks!
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import ideas.ideasportal.api.Idea;
import ideas.ideasportal.api.JsonHandler;
import java.util.ArrayList;
public class ViewIdeas extends AppCompatActivity {
public ArrayList<Idea> ideas = new ArrayList<Idea>();
public ListView ideasList;
Intent viewIdea;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_ideas);
viewIdea = new Intent(this, ViewIdea.class);
JsonHandler jsonHandler = new JsonHandler();
ideas = jsonHandler.handleIdeas((String)getIntent().getExtras().get("ideas"));
ideasList = (ListView)findViewById(R.id.listView);
ArrayList<String> arr = new ArrayList<>();
for(Idea idea : ideas)
{
arr.add(idea.getTitle());
}
ArrayAdapter<String> ar = new ArrayAdapter<String>(this, R.layout.list_item, arr);
ideasList.setAdapter(ar);
ideasList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Idea idea = ideas.get(position);
viewIdea.putExtra("id", idea.getId());
startActivity(viewIdea);
}
});
}
}

Related

multiple spinner in which second spinner values should be related to the selected value in first spinner

using 2 spinner first one contains the main category and second one contains the items related to the selected category.
but the second spinner values are not displayed.
package com.learning.double_spinner;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Spinner categoryspn,unitspn;
EditText inputedt;
Button convertbtn;
TextView resulttxtv;
ArrayList<String> categoryarraylist;
ArrayList<String> weightarraylist;
ArrayList<String> lengtharraylist;
ArrayAdapter<String> categoryarrayadapter;
ArrayAdapter<String> unitarrayadapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
categoryspn= findViewById(R.id.categoryspn);
unitspn=findViewById(R.id.unitspn);
inputedt=findViewById(R.id.inputedt);
convertbtn=findViewById(R.id.convertbtn);
resulttxtv=findViewById(R.id.resulttxtv);
categoryarraylist= new ArrayList<>();
categoryarraylist.add("Select Category");
categoryarraylist.add("Weight");
categoryarraylist.add("Length");
categoryarrayadapter= new ArrayAdapter<>(getApplicationContext(),android.R.layout.simple_spinner_item,categoryarraylist);
categoryspn.setAdapter(categoryarrayadapter);
weightarraylist=new ArrayList<>();
weightarraylist.add("Kilogram");
weightarraylist.add("Pound");
lengtharraylist= new ArrayList<>();
lengtharraylist.add("kilometer");
lengtharraylist.add("mile");
categoryspn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (categoryarraylist.equals(1))
{
unitarrayadapter= new ArrayAdapter<>(getApplicationContext(),android.R.layout.simple_spinner_item,weightarraylist);
}
if (categoryarraylist.equals(2))
{
unitarrayadapter=new ArrayAdapter<>(getApplicationContext(),android.R.layout.simple_spinner_item,lengtharraylist);
}
unitspn.setAdapter(unitarrayadapter);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
using 2 spinner first one contains the main category and second one contains the items related to the selected category.
but the second spinner values are not displayed.

how can I let the array list to be clickable?

I am trying to handle this list to be clickable.
When I set an intent activity it doesn't work so how can I handle each element in this array list.
The error is when I press on any element in this array list it left me to the last intent I gave it.
package com.adnan.android.autofix;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.menu.MenuView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import java.net.Inet4Address;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Array list content
ArrayList<String> CarsBrand = new ArrayList<String>();
CarsBrand.add("Alfa Romeo");
CarsBrand.add("Audi");
CarsBrand.add("BMW");
CarsBrand.add("Brilliance");
CarsBrand.add("BYD");
CarsBrand.add("Changan");
CarsBrand.add("Chery");
CarsBrand.add("Chevorlet");
CarsBrand.add("Chrysler");
ArrayAdapter<String> CarsBrandAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, CarsBrand);
GridView listView = (GridView) findViewById(R.id.ListCarsBrands);
listView.setAdapter(CarsBrandAdapter);
// handling the array list
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent AlfaRomeoIntent = new Intent(MainActivity.this, AlfaRomeo.class);
startActivity(AlfaRomeoIntent);
Intent AudiIntent = new Intent(MainActivity.this, com.adnan.android.autofix.Audi.class);
startActivity(AudiIntent);
}
});
}
}
Maybe you can map your list items to their corresponding activity classes like this:
package com.adnan.android.autofix;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.menu.MenuView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import java.net.Inet4Address;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements {
HashMap<String, Class> intentMap ;
ArrayList<String> CarsBrand;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Array list content
CarsBrand = new ArrayList<String>();
CarsBrand.add("Alfa Romeo");
CarsBrand.add("Audi");
CarsBrand.add("BMW");
CarsBrand.add("Brilliance");
CarsBrand.add("BYD");
CarsBrand.add("Changan");
CarsBrand.add("Chery");
CarsBrand.add("Chevorlet");
CarsBrand.add("Chrysler");
ArrayAdapter<String> CarsBrandAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, CarsBrand);
intentMap = new HashMap<String, Class>()
{{
put("Alfa Romeo", AlfaRomeo.class);
put("Audi", Audi.class);
// PUT ALL STRING-CLASS PAIRS LIKE THIS
}};
GridView listView = (GridView) findViewById(R.id.ListCarsBrands);
listView.setAdapter(CarsBrandAdapter);
// handling the array list
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Class selectedIntentClass = intentMap.get(CarsBrand.get(position));
Intent intent = new Intent(MainActivity.this, selectedIntentClass);
startActivity(intent);
}
});
}
}
You Can Try Like this :
package com.adnan.android.autofix;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.menu.MenuView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import java.net.Inet4Address;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Array list content
ArrayList<String> CarsBrand = new ArrayList<String>();
CarsBrand.add("Alfa Romeo");
CarsBrand.add("Audi");
CarsBrand.add("BMW");
CarsBrand.add("Brilliance");
CarsBrand.add("BYD");
CarsBrand.add("Changan");
CarsBrand.add("Chery");
CarsBrand.add("Chevorlet");
CarsBrand.add("Chrysler");
ArrayAdapter<String> CarsBrandAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, CarsBrand);
GridView listView = (GridView) findViewById(R.id.ListCarsBrands);
listView.setAdapter(CarsBrandAdapter);
// handling the array list
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final String item = (String)parent.getItemAtPosition(position);
switch (item) {
case "Alfa Romeo":
Intent AlfaRomeoIntent = new Intent(MainActivity.this, AlfaRomeo.class);
startActivity(AlfaRomeoIntent);
break;
case "Audi":
Intent AudiIntent = new Intent(MainActivity.this, com.adnan.android.autofix.Audi.class);
startActivity(AudiIntent);
break;
}
}
});
}
}

Clicked list item into a String and transported to another activity

I have made this app where in one particular activity i have a all the items listed in a list view. when you click the list item it goes to another activity where similar thing is happening. after that i was the clicked list items to be converted into a strings and transported into a 3rd activity where i can display those.
when i try to display them this shows in the text view where the clicked text item should have appeared:
this is code for the first activity:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.internal.Objects;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class TicketCategory extends AppCompatActivity {
public static String Category;
public String getCategory() {
return Category;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ticket_category);
populateTicketCategoryList();
final ListView listView = (ListView) findViewById(R.id.lvTicketCategory);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (i == 0) {
Category = listView.getItemAtPosition(i).toString();
Intent intent = new Intent(TicketCategory.this, Subcategory.class);
startActivity(intent);
}
}
});
}
private void populateTicketCategoryList()
{
ArrayList<CompTicketCategory> arrayOfTicket = CompTicketCategory.getTicket();
CompTicketCategoryAdapter adapter = new CompTicketCategoryAdapter(this, arrayOfTicket);
ListView listView = (ListView) findViewById(R.id.lvTicketCategory);
listView.setAdapter(adapter);
}
}
the code for the second activity is:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class Subcategory extends AppCompatActivity {
public String Category;
public static String Subcat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_subcategory);
populateSubcategoryList();
final ListView listView = (ListView) findViewById(R.id.lvSubcategory);
ArrayAdapter arrayAdapter = new ArrayAdapter<String>(Subcategory.this, android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Subcat = listView.getItemAtPosition(i).toString();
Intent intent = new Intent(Subcategory.this, SubmitTicket.class);
startActivity(intent);
}
});
and this is the code for the activity where both of the clicked items should be displayed:
public class SubmitTicket extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_submit_ticket);
Spinner spinner = (Spinner) findViewById(R.id.spinner_priority);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.priority_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
final Button butt = findViewById(R.id.submit);
butt.setOnClickListener(new View.OnClickListener()
{
public void onClick (View view){
Toast.makeText(getApplicationContext(), "The ticket has been submitted", Toast.LENGTH_SHORT).show();
}
});
TextView textView = (TextView)findViewById(R.id.Category_submit_report);
textView.setText(TicketCategory.Category);
TextView tv = (TextView)findViewById(R.id.Subcategory_submit_report);
tv.setText(Subcategory.Subcat);
}
Please help me. i would appreciate any output. thanks!
UPDATE:
after trying
CompTicketCategory model = listView.getItemAtPosition(i);
Category=model.Category; // your Category variable
Category=model.getCategory();
this error is shown;
screenshot
You can use Intent Extra Feature.
In the First Activity,
Intent intent = new Intent(Subcategory.this, SubmitTicket.class);
switch1.putExtra("deviceID", listView.getItemAtPosition(i).toString(););
startActivity(intent);
Then Next activity recall them,
Intent intent = getIntent();
String data = intent.getStringExtra("data");
Try this in your TicketCategory actvity
Use this:
CompSubcategory model = listView.getItemAtPosition(i);
Category=model.Category; // your Category variable
Category=model.getCategory(); // or use getter setter method
Instead of this:
Category = listView.getItemAtPosition(i).toString();

How to use viewHolder pattern to keep UI elements on scroll in listView?

I am looking into using the viewHolder pattern for making my UI elements not get reset to the default when my listview is scrolled. After looking into it Im not sure how to go about this. Im not even sure if my existing code will even allow me to use viewHolder pattern. I am a beginner so please elaborate and do not skip the details. Thank you
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import java.util.ArrayList;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> Chores = new ArrayList<>();
Chores.add("");
final ListAdapter MyAdapter = new CustomAdapter(this, Chores);
ListView listViewObject = (ListView)findViewById(R.id.customListView_ID);
listViewObject.setAdapter(MyAdapter);
listViewObject.setOnItemClickListener(
new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String ChoreString = String.valueOf(parent.getItemAtPosition(position));
}
}
);
final Button button = (Button) findViewById(R.id.button_ID);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Chores.add("");
((ArrayAdapter)MyAdapter).notifyDataSetChanged();
}
});
}
}
CustomAdapter.java
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import static com.example.emilythacker.chorelist.R.id.textView_ID;
class CustomAdapter extends ArrayAdapter{
public CustomAdapter(Context context, ArrayList choreText) {
super(context, R.layout.custon_listview_row, choreText);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater myInflater = LayoutInflater.from(getContext());
View customView = myInflater.inflate(R.layout.custon_listview_row, parent, false);
ImageButton imageButton = (ImageButton) customView.findViewById(R.id.imageButton_ID);
final TextView textView = (TextView) customView.findViewById(textView_ID);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//what happens when textView is clicked
AlertDialog alertDialog = new AlertDialog.Builder(getContext()).create();
final EditText input = new EditText(getContext());
input.setHint("hint");
alertDialog.setView(input);
alertDialog.setTitle("Set Chore");
alertDialog.setMessage("Alert message to be shown");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//this will set text to "hello" only if user does not enter anything into input
//textView.setText("hello");
//this also will only work if there is no input entered into input...wich will change textView into empty space
textView.setText(input.getText().toString());
//works the same with or without dialog.dismiss();
dialog.dismiss();
}
});
alertDialog.show();
}
});
imageButton.setImageResource(R.drawable.clock);
return customView;
}
}
I found this tutorial really helpful when learning to use the ViewHolder Pattern. It is pretty clear in explaining how to use it, but feel free to ask if you feel stuck.
When you are performing some change to any list Item we should call notifyDataSetChanged(),so that the listview gets updated.
Answer to your question:
call notifyDataSetChanged() in textview.onClickListener() method after updating the text.
Better to shift to RecyclerView, In this when we add new item we can just call notifyItemAdded(position) which will refresh only the view at that position but not the whole.
And when we update any item we can call notifyItemChanged(position).

Using TextView on Android Problem

import android.app.Activity;
import android.app.ListActivity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class textfile extends ListActivity {
// private static final int PICKFILE_RESULT_CODE = 0;
private List<String> items = null;
private File currentDirectory;
private ArrayAdapter<String> fileList;
Intent myIntent = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentDirectory = new File("/sdcard/myfolder");
getFiles(currentDirectory.listFiles());
setContentView(R.layout.main);
}
protected void onListItemClick (AdapterView<?> parent, ListView l, View v, int position, long id)
{
int selectedRow = (int)id;
currentDirectory = new File(items.get(selectedRow));
if(currentDirectory.isDirectory()){
getFiles(currentDirectory.listFiles());
} else{
//if the selected file is not a directory. get the filename
currentDirectory.getPath();
}
Intent myIntent = null;
if(((TextView) v).getText().equals("sdcard/myfolder/anskey.txt")){
myIntent = new Intent(v.getContext(), dialog.class);
}
startActivity(myIntent);
}
private void getFiles(File[] files){
items = new ArrayList<String>();
for(File file : files){
items.add(file.getPath());
}
fileList = new ArrayAdapter<String>(this,R.layout.list_item, items);
setListAdapter(fileList);
}
}
In this program i am displaying directory structure display "sdcard/myfolder" as a list.
now what i want to do is that when i click on "sdcard/myfolder/anskey.txt" dialog.class activity should open.
there is no exception but on clicking "sdcard/myfolder/anskey.txt" ,the dialog.class activity is not opening.
From what I can see, your onListItemClick() signature is not right. It should be:
protected void onListItemClick(ListView l, View v, int position, long id)
This is the first thing you will want to correct. There could be other issues with your code.
I didn't see you assigned the Listener to the listView and I didn't see you declared your class to implement onListItemClickListener as well, so the implementation of onListItemListener is just another method. Follow this simple tutorial to accomplish that

Categories