Android Adding list item giving Error - android

I have two lists I am adding the item from the list1, when clicked, to the other list but when I click an item. program is ending with out any errors
debug shows exception error
Following is the code :-
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String lst1[] = {"abdul","yousuf","khalid","john","egor"};
String lst2[]= {};
Double lp1[]={10.0,5.0,2.0,0.5,1.5};
Double lp12[]={};
ListView lis1= (ListView)findViewById(R.id.lv1);
ListView lis2= (ListView)findViewById(R.id.lv2);
ArrayAdapter<String> adapter1 =new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,lst1);
final ArrayAdapter<String> adapter2 =new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,lst2);
lis1.setAdapter(adapter1);
lis2.setAdapter(adapter2);
lis1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = ((TextView)view).getText().toString();
adapter2.add(item);
}
});
}

Because when you use
ArrayAdapter<String> adapter1 =new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,lst2);`
// Internally a fixed size list will be generated ^^^^
then lst2 array will be converted to a fixed length list which cannot accept new entries
public More ...ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) {
init(context, resource, textViewResourceId, Arrays.asList(objects));
// fixed length array
}
Read about asList
Solution :
ArrayAdapter<String> adapter1 =new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,
new ArrayList<String>(Arrays.asList(lst1));
// Pass array as new mutable list
final ArrayAdapter<String> adapter2 =new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,
new ArrayList<String>(Arrays.asList(lst2));

try this
lis1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item =lst1[position];
adapter2.add(item);
}
});

here is solution
lis1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = ((TextView)view).getText().toString();
lst2.add(item);
adapter2.notifyDataSetChanged();
}
});

Related

multiple spinner android depend on each other

i have 3 spinners, first spinner is txt_kategori ;
second txt_kondisi ;
third txt_penanganan ; the second depends on the first and the third depends on the second input, here is my code
txt_kategori.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(view.getContext(), android.R.layout.simple_spinner_item, condition[position]);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
txt_kondisi.setAdapter(spinnerArrayAdapter);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
txt_kondisi.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(view.getContext(), android.R.layout.simple_spinner_item, handling[position]);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
txt_penanganan.setAdapter(spinnerArrayAdapter);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
the problem is java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.View.getContext()' on a null object reference
at ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(view.getContext(), android.R.layout.simple_spinner_item, handling[position]);
Use ActivityName.this instead view.getContext()
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(view.getContext(), android.R.layout.simple_spinner_item, condition[position]);
change to
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(ActivityName.this, android.R.layout.simple_spinner_item, condition[position]);

I want to display selected item with some title in Spinner,Like item is:item6

I am new in android ,I want work like, In Spinner if i select any other items form array,that item should get selected including with some text,For example,if My spinner having 10 items,if i select 6th item that item should display like "Your Item Is: Item6",This should be display in my spinner as first item,After select of items form the array,Please help me ,,Thanks in advance..
String[] categories = {"Select one","Automobile","Computers","Education"};
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
then onItemSelected Listerner Do :-
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
categories[0] = item;
ArrayAdapter<String> newdataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
spinner.setAdapter(newdataAdapter);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
ArrayAdapter arrayAdapter = null;
String[] list=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//you can replace the below list with your list
list = {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"};
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinner.setPrompt("Your selected item is:"+list[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}

I would like the listview item to display the data of the relevant item in new activity

Currently, the code i have only displays the data (on a new activity when clicked) at index 0 of that list view - not the item i actually clicked. i would like to somehow change my code so it can display the data at index n (n being the item i clicked in list view):
ResultsActivity.java
final ArrayList<String> searchResults = getValuesFromJSON(jsonResult);
final ListView listView = (ListView) findViewById(R.id.listView);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, searchResults);
listView.setAdapter(arrayAdapter);
//list items become clickable and open the movie detail page which displays the movie data of the specific movie
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent newActivity = new Intent(ResultsActivity.this, MoviePage.class);
newActivity.putStringArrayListExtra("movie data", searchResults);
startActivity(newActivity);
}
});
}
MoviePage.java
ArrayList<String> searchResults = getIntent().getStringArrayListExtra("movie data");
TextView t = (TextView)findViewById(R.id.textViewfinal);
t.setText(searchResults.get(9));
Just set a onclick listener on your listview so you can listen for item click events.
Put this in your onCreate method in your activity:
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
//you can move the textview to a class variable if you want
TextView t = (TextView)findViewById(R.id.textViewfinal);
t.setText(searchResults.get(position));
}
});
update: you'll want to pass the position to the next activity the same way your passing the searchResults to MoviePage so just do this:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent newActivity = new Intent(ResultsActivity.this, MoviePage.class);
newActivity.putStringArrayListExtra("movie data", searchResults);
newActivity.putExtra("position",position);//pass the position to the next activity
startActivity(newActivity);
}
});
now in MoviePage.java
ArrayList<String> searchResults = getIntent().getStringArrayListExtra("movie data");
int position = getIntent().getIntExtra("position",0);
TextView t = (TextView)findViewById(R.id.textViewfinal);
t.setText(searchResults.get(position));
Try this :
ResultsActivity.java
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent newActivity = new Intent(ResultsActivity.this, MoviePage.class);
newActivity.putExtra("movie data", searchResults.get(position));
startActivity(newActivity);
}
});
MoviePage.java
Bundle extras = getIntent().getExtras();
if (extras != null) {
TextView t = (TextView)findViewById(R.id.textViewfinal);
t.setText(extras.getString("movie data"));
}

Android ListView Removing Item on click

I am trying to remove an item from the list when I click on the selected item but I get an error on logcat saying that the removeViewAt method is not supported on AddapterView. Any idea of what I could use here in order to achieve this ?
Many thanks.
Here is my code.
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
listView.removeViewAt(position);
listView.invalidateViews();
}
});
}
Try this:
ArrayList<String> arrlist=new ArrayList<String>();
arrlist.add("A");
arrlist.add("B");
arrlist.add("C");
arrlist.add("D");
ListView listView = (ListView) findViewById(R.id.mylist);
String[] items = { "Milk", "Butter", "Yogurt", "Toothpaste", "Ice Cream" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
items.remove(position);
adapter.notifyDataSetChanged();
}
});
}
This fades away the clicked item using Animation, Requires API level 16
private List<String> myList;
File file;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listerX);
myList = new ArrayList<String>();
File directory = Environment.getExternalStorageDirectory();
file = new File( directory + "/myAppCache/" );
File list[] = file.listFiles();
for( int i=0; i< list.length; i++)
{
myList.add( list[i].getName() );
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
view.animate().setDuration(2000).alpha(0)
.withEndAction(new Runnable() {
#Override
public void run()
{
Toast.makeText(getApplicationContext(), ""+item,
Toast.LENGTH_LONG).show();
}
});
}
});
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, myList);
listView.setAdapter(adapter); //Set all the file in the list.
}
you are populate list with any array or collection item
Then remove item at position of clicked position.
And now notify adapter by calling adapter.notifydatasetchange();
Enjoy your code

Android listview setOnItemClickListener

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_campaign_list);
Campaign campaign_data[] = new Campaign[]
{
new Campaign(R.drawable.hlbb, "MSIG HLBB PA Extra"),
new Campaign(R.drawable.hlbb, "MSIG HLBB SSP Plus")
};
CampaignAdapter adapter = new CampaignAdapter(this, R.layout.listview_header_row, campaign_data);
listView1 = (ListView)findViewById(R.id.listView1);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
I can run the code above without setonitemclicklistener function, when i add the setOnItemClickListener, it stopped working.What is the error?? I'm new in Android..Thx
I don't know how is your CampaignAdapter code, but you are trying to cast the row view which you are inflating into TextView. You should use your adapter to access the item using the position of element:
CampaignAdapter adapter = new CampaignAdapter(this, R.layout.listview_header_row, campaign_data);
listView1 = (ListView)findViewById(R.id.listView1);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Campaign item = (Campaign) adapter.getItem(position);
// Now you can access to the campaing value that you want
// For instance, item.getText()
Toast.makeText(getBaseContext(), item.getText(), Toast.LENGTH_LONG).show();
}
});

Categories

Resources