Android ListView Items open Activity - android

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.

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();
}

How to reset the whole View by clicking the delete Button

I have made a custom View which contains an ImageView, a TextView and a delete Button.
I want to reset the particular View when I click the delete Button associated with it.
Please tell how to implement this.
Here is my MainActivity.java
package com.khurana.nikhil.list1;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
ListView lv;
TextView tv1, tv2;
View v1;
public String[] s = {"nikhil", "mukul", "kunal"};
public int[] img = {R.drawable.rty, R.drawable.sf, R.drawable.rty};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView) findViewById(R.id.listView);
CustomAdapter cad = new CustomAdapter(MainActivity.this, s, img);
lv.setAdapter(cad);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent,View v,int position,long id)
{
Toast.makeText(MainActivity.this,"You clicked "+s[position],Toast.LENGTH_SHORT).show();
}
});
}
}
Here is CustomAdapter.java
package com.khurana.nikhil.list1;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<String>{
Context c1;
String s1[];
int s2[];
CustomAdapter(Context c,String s[],int s3[])
{
super(c,R.layout.tcustom,s);
this.c1=c;
this.s1=s;
this.s2=s3;
}
public View getView(int position,View v,ViewGroup parent)
{
LayoutInflater li=(LayoutInflater) c1.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=li.inflate(R.layout.tcustom,null);
TextView tv=(TextView)v.findViewById(R.id.textView);
ImageView im=(ImageView)v.findViewById(R.id.imageview);
tv.setText(s1[position]);
im.setImageResource(s2[position]);
Button bt=(Button)v.findViewById(R.id.button);
return v;
}
}
((LinearLayout)yourView.getParent()).removeView(yourView);
or you can call from the layout directly
yourRelativeLayout.removeView(yourView)
I would recommend to use an ArrayList instead of a String[] in your Adapter class. It makes it a lot easier to delete or edit a View and the associated data behind it. I used this post to convert your String[] to an ArrayList, delete the item and convert back to String[].
This should work on button click:
In your Adapter class:
public View getView(int position,View v,ViewGroup parent)
{
LayoutInflater li=(LayoutInflater) c1.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=li.inflate(R.layout.tcustom,null);
TextView tv=(TextView)v.findViewById(R.id.textView);
ImageView im=(ImageView)v.findViewById(R.id.imageview);
tv.setText(s1[position]);
im.setImageResource(s2[position]);
Button bt=(Button)v.findViewById(R.id.button);
bt.setTag(position); //important so we know which item to delete on button click
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
int positionToRemove = v.getTag(); //get the position of the view to delete stored in the tag
removeItem(positionToRemove); //remove the item
}
});
return v;
}
public void removeItem(int position){
//convert array to ArrayList, delete item and convert back to array
ArrayList<String> a = new ArrayList<>(Arrays.asList(s1));
a.remove(i);
strings = new String[a.size()];
s1= a.toArray(strings);
notifyDataSetChanged(); //refresh your listview based on new data
}
#Override
public int getCount() {
return s1.length;
}
#Override
public String getItem(int position) {
return s1[position];
}
View visibility to GONE will not delete your view from memory as well. Please be specific, do you want to retain your view for future?
If you are working in a listview you should call notifyDataSetChanged() the adapter. your getView() method can be like:
action on button can be like this
Button bt=(Button)v.findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
s1 = ArrayUtils.remove(s1, position);
s2 = ArrayUtils.remove(s2, position);
notifyDataSetChanged();
}
});
If you want the view to disappear:
yourView.setVisibility(View.GONE);
EDIT: To get the parent view of the button: Android: Making changes to a button's parent view

Adding ItemListener in Android ListView

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();
}

how to use a custom button in a list to redirect the user to another activity

I've written a big code and I've used a list view within one of my task layouts :
I want to know how I can make a listener for each update button in this case in order to move to another activity , I know how to use more than one activity and moves between them But I want to know how to make a listener only .
The custom adapter code :
package com.example.task_9;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class TempLyaout extends ArrayAdapter<String> {
Context context;
ArrayList<String> sa;
public TempLyaout(Context context , ArrayList<String> sa) {
super(context,R.layout.temp,sa);
this.context=context;
this.sa = sa;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater l = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View rowView = l.inflate(R.layout.temp, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.textView1);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
Button update = (Button) rowView.findViewById(R.id.button1);
// myDb.open();
// Cursor cursor= myDb.getRowByName(sa.get(position));
// myDb.updateRowByName(cursor.getInt(DBAdapter.COL_ROWID), sa.get(position), cursor.getString(DBAdapter.COL_PASSWD), cursor.getInt(DBAdapter.COL_AGE), isAdmin);
// myDb.close();
textView.setText(sa.get(position));
return rowView;
}
}
This is the related class :
package com.example.task_9;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class UsersActivity extends Activity {
Intent redirect;
ListView list;
ArrayList<String> sa;
DBAdapter myDb;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
#Override
protected void onStart() {
super.onStart();
setContentView(R.layout.users);
list= (ListView) findViewById(R.id.usersList);
redirect = getIntent();
openDB();
/*********************************************/
Cursor cursor = myDb.getAllRows();
cursor.moveToFirst();
ArrayList<String> ha = new ArrayList<String>();
while(cursor.moveToNext()) {
ha.add(cursor.getString(DBAdapter.COL_NAME));
}
cursor.close();
#SuppressWarnings("rawtypes")
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,ha);
list.setAdapter(new TempLyaout(UsersActivity.this,ha));
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();
}
});
} // end onStart method
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
#Override
protected void onDestroy() {
super.onDestroy();
closeDB();
}// end onDestroy method
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
private void openDB() {
myDb = new DBAdapter(this);
myDb.open();
} // end openDB method
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
private void closeDB() {
myDb.close();
} // end closeDB method
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
}// end main class
In your getView
Button update = (Button) rowView.findViewById(R.id.button1);
update.setOnClickListener(mClickListener);
Then
private OnClickListener mClickListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(ListActivtiy.this,SecondActivity.class);
startActivity(intent);
}
};
Also use a viewholder for smooth scrolling and performance
http://developer.android.com/training/improving-layouts/smooth-scrolling.html

ANDROID: make listview elements cliccable if it have an url

i have this code:
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Bookmarks extends ListActivity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.bookmarks);
Database info=new Database(this);
info.open();
String data=info.getData();
String[] data_array = data.split(",");
info.close();
ListView listView1 = (ListView) findViewById(R.id.list_mia);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, data_array);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(){
public void onItemClick(AdapterView<?> a, View v, int position, long id){
if(data_array[position].startsWith("http://")){
//do this
}else{
//do this
}
}
}
}
}
which is working properly.
how can i make the item cliccable if it have an url?
i've tried various help aroud the web, buit i can't make it work!
thanks
try this
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(data_array[position].startsWith("http://")){
//do this
}
else{
//do this
}
}
});

Categories

Resources