Adding ItemListener in Android ListView - android

I've a swive view along with 4 tabs which displays in my first UI. I've added ListView for each Tabs. But OnItemClickListener is not working for ListFragment. My class is extended from ListFragment and implements OnItemClickListener. What i wanted to do is that when i select a item from the list, i've to display the Toast which displays the name of item that i've selected. But its not working for me. No error occurs but not getting the things done. Can anyone help me for this. My complete code for this is below:
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class TUFragment extends ListFragment implements OnItemClickListener {
ListView list;
View rootView;
final String[] courses = new String[] { "BIM", "BBA", "BBS", "BSc-CSIT",
"BSc-IT", "BHM", "BTTM", "MBA", "MBS", "MSc-IT", "MTTM" };
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_tu, container, false);
list = (ListView) rootView.findViewById(android.R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, courses);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
return rootView;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(this.getActivity(), "You have Selected: " + courses[arg2],
Toast.LENGTH_LONG).show();
}
}
Thankx in advance

Try this..
And also change to setListAdapter(adapter);
#Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
String val = ((TextView) v).getText().toString().trim();
Toast.makeText(getActivity(), "You have Selected: " + val,
Toast.LENGTH_LONG).show();
}

Related

OnItemClickListener in Listview with image is not working

I'm beginner in android. I created my OnItemListener in ListView with Image, but when I click the list item, my app crashes.
Here my MainActivity.java
package com.listview.test.arrayadapter;
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 android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
ListView l1;
// RelativeLayout r1;
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l1= (ListView) findViewById(R.id.listview);
// r1= (RelativeLayout) findViewById(R.id.activity_main);
String[] list = {"item1","item2","item3","item4","item5","item6","item7","item8","item9","item10"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.single_view , R.id.textview, list);
l1.setAdapter(adapter);
l1.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView temp = (TextView) view;
Toast.makeText(this, temp.getText() + "" + position, Toast.LENGTH_LONG).show();
}
}
I think you are getting an exception because your R.layout.single_view is not a TextView and you are tring to cast it into that. Do something like this instead:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView temp = (TextView) view.findViewById(R.id.textview);
Toast.makeText(this, temp.getText() + "" + position, Toast.LENGTH_LONG).show();
}

Set Id and get it to the items of ListView in Android

New android programmer is here.
First : I dont know many things about list view and as I found out , its complicated to work with it.
So , I want to put my database data (Contains Id , Name) to a listview and get the Id of the item is clicked.
I have searched many but i just found this :
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class test extends Activity {
String[] wordlist = new String[] { "a", "b", "c" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wordlist[2] = "abds";
ListView list = new ListView(this);
list.setAdapter((ListAdapter) new MyAdapter(test.this, wordlist));
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Object entry= parent.getItemAtPosition(position);
Toast.makeText(test.this, entry.toString(), Toast.LENGTH_SHORT).show();
}
});
setContentView(list);
}
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, String[] strings) {
super(context, -1, -1, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout listLayout = new LinearLayout(test.this);
listLayout.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT,
AbsListView.LayoutParams.WRAP_CONTENT));
listLayout.setId(5000);
TextView listText = new TextView(test.this);
listText.setId(5001);
listLayout.addView(listText);
listText.setText(super.getItem(position));
return listLayout;
}
}
}
Just i can show strings , not Id.
Use SimpleCursorAdapter to populate ListView with data from database, for example - https://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/ (first link from google).
How to get the item id in an onItemClick handler
Read more about ListView and Adapters, your code is awful )

How to populate ListView and List onclick data?

I have a database with Users and their data.
Then make ListView with their names.
How then set click listener to ListView assosiated with my List?
So when I click on List element strats new Activity with all User details, no only name?
package com.test.tabs;
import java.util.ArrayList;
import java.util.List;
import com.test.tabs.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class SelectFragment extends Fragment {
private ListView listView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_select, container, false);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
MySQLiteOpenHelper db = new MySQLiteOpenHelper(getActivity());
List<User> users = db.getAllUsers();
List<String> your_array_list2 = new ArrayList<String>();
for (int i = 0; i < users.size(); i++) {
your_array_list2.add(users.get(i).getFirstName());
}
listView = (ListView) getActivity().findViewById(R.id.listView);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,your_array_list2);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {}
});
}
}
You need to implement parcelable in your User class:
public class User implements Parcelable
{
....
}
Now you can attach/send your user to the next activity like so:
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
User user = adapter.getItem(position);
Bundle b = new Bundle();
b.putParcelable("user", user);
Intent i = new Intent(context, OtherActivity.class);
i.putExtra("bundle", b);
startActivity(i);
}
});
In the new activity call:
Bundle b = getIntent().getBundleExtra("bundle");
User user = b.getParcelable("user");
Use ListView#setOnItemClickListener(AdapterView.OnItemClickListener listener).
In my opinion, you should use Cursor Adpater to populate listview from database. On item click event for each row, you can get corresponding cursor object, pass on some details to next activity, so it can fetch more user details and display to user.
You need to understand the basics first. Have a look at this tutorial to have some idea.
Good Luck!

Android ListView Items open Activity

I have this problem i can't access to another layout or activity form List view , im trying to call Intent but I always get "confirm perspective switch" , the source not found .
here is the code
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class HomeTab extends Activity {
private List<home_items> myItems= new ArrayList<home_items>();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
populateHomeIteams();
populateListView();
registerClickCallback();
}
private void registerClickCallback() {
ListView list=(ListView)findViewById(R.id.listView1);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position,
long id) {
//Toast.makeText(HomeTab.this, "positiom is"+position, Toast.LENGTH_LONG).show();
if(position==0)
{
Intent intt=new Intent(HomeTab.this, Beauty_Tab.class);
startActivity(intt);
}
}
});
}
private void populateHomeIteams() {
myItems.add(new home_items("Beauty", R.drawable.bg_list_view));
myItems.add(new home_items("Healthy food", R.drawable.bg_list_view));
myItems.add(new home_items("Family Health", R.drawable.bg_list_view));
myItems.add(new home_items("Moda", R.drawable.bg_list_view));
}
private void populateListView() {
ArrayAdapter<home_items> adapter= new MyListAdapter();
ListView list= (ListView) findViewById(R.id.listView1);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<home_items> {
public MyListAdapter(){
super(HomeTab.this,R.layout.iteam_view,myItems);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView == null){
itemView=getLayoutInflater().inflate(R.layout.iteam_view,parent ,false);
}
home_items currentitem=myItems.get(position);
ImageView imageview=(ImageView)itemView.findViewById(R.id.iteam_icon);
imageview.setImageResource(currentitem.get_iconID());
TextView maketxt=(TextView)itemView.findViewById(R.id.iteam_txt);
maketxt.setText(currentitem.get_iteam_name());
return itemView;
}
}
}
the problem is in the function registerclickCallnack()
the toast is working will but the intent not working
private void registerClickCallback() {
ListView list=(ListView)findViewById(R.id.listView1);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position,
long id) {
//Toast.makeText(HomeTab.this, "positiom is"+position, Toast.LENGTH_LONG).show();
if(position==0)
{
Intent intt=new Intent(HomeTab.this, Beauty_Tab.class);
startActivity(intt);
}
}
});
}
as far as i remember in onItemClick() method position starts from 1
Your problem is that you instantiate 2 listView objects, one in populateListView and the other in registerClickCallback; You instantiate one listView but add the click callback on the other, make sure you use one single listView object all over.

How can I change the position of Item selected in list View in android

I am making an application in which I have to change the position of the selected item in list view to first position and set other items one position less than current How can i do this.
Can any body suggest me some tutorial or any suggestion.
Use Adapter, and arraylist on item click remove the item from that position. out it at the 0th position and use notifyDataSetChanged to reaarange the listview
Example
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Main_Screen extends ListActivity implements OnItemClickListener{
ArrayAdapter arrayAdapter = null;
/** Called when the activity is first created. */
Context context = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
penList.add("MONT Blanc");
penList.add("Gucci");
System.out.println("...1...");
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, penList);
arrayAdapter.add("last by adapter");
setListAdapter(arrayAdapter);
penList.add("Last By list");
arrayAdapter.add("last by adapter2");
getListView().setTextFilterEnabled(true);
ListView lv = getListView();
this.registerForContextMenu(lv);
lv.setOnItemClickListener(this);
}
static ArrayList<String> penList = new ArrayList<String>();
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
String str = penList.get(arg2);
penList.remove(arg2);
penList.add(0, str);
arrayAdapter.notifyDataSetChanged();
}
}

Categories

Resources