ANDROID: make listview elements cliccable if it have an url - android

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

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

ClassCastException: android.widget.ListView cannot be cast to android.widget.AdapterView.OnItemClickListener

I'm trying to make the items in my list view clickable but I keep running into this error on android studio saying that it cannot resolve symbol OnItemClickListener() on the line new OnItemClickListener and asks if I am talking about ArrayView.onItemClickListener.
The same happens if I try to extend OnItemClickListener and even if I cast new (ListView) OnItemClickListener. When I hover over it it says setOnItemClickListener (android.widget.AdapterView.OnItemClickListener) in adapter view cannot be applied.
This is what I have so far and I tried it another way also which is the second half but, this time, the error was also the this in parenthesis and I keep getting the other attempt error of OnItemClickListener (andorid……
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Create extends ListActivity {
#Override
protected void onCreate(Bundle savedInstancesState)
{
super.onCreate(savedInstancesState);
setContentView(R.layout.create);
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
ArrayAdapter<String> list = new ArrayAdapter<String>(this, R.layout.contacts_list_item);
while(cursor.moveToNext()) {
String name = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
list.add(name);
}
ListView lstView = (ListView) findViewById(android.R.id.list);
lstView.setOnClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int pos, long ld) {
}
});
setListAdapter(list);
}
This is the second way I tried it but still highlights red under this
lstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
TextView tv = (TextView) view.findViewById(R.id.contact_text_list);
String clickText = tv.getText().toString();
Log.d("My Activity", clickText);
}
}
You are using wrong method to assign the listener
Change
lstView.setOnClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int pos, long ld) {
}
});
to
lstview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
}
});

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.

onclick sqlite item

The app works fine up to //***************** in ListViewActivity.java. After this point I want to be able to click on an item and modify that item. I have tried numerous examples to no avail. My first goal is to be able to toast the item clicked.
package bipsnm.android;
import java.util.List;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
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;
import android.widget.TextView;
import android.widget.Toast;
public class ListViewActivity extends Activity{
public void onCreate(Bundle savedInstanceState, String[] outputData, String[]
ListView1){
super.onCreate(savedInstanceState);
Toast.makeText(this, "list/textview", Toast.LENGTH_LONG).show();
final String KEY_ROWID = "_id";
final String KEY_ITEMTYPE = "itemType";
final String KEY_QUANTITY = "quantity";
setContentView(R.layout.sqlview);
// TextView content = (TextView) findViewById(R.id.outputData);
ListView content = (ListView) findViewById(R.id.listView1);
DatabaseControl control = new DatabaseControl(this);
String result = "ItemType\t Quantity \n";
try {
control.open();
result = result + "" + control.fetchItemIdByQty();
control.close();
}catch (SQLiteException e) {
e.printStackTrace();
}
content.setFilterText(result);
//***************************************************************************
setListAdapter(new ArrayAdapter<String>(this, R.layout.sqlview,ListView1));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent,View view,int position,long id){
Toast.makeText(getApplicationContext(),((TextView)view).getText(),Toast.LENGTH_LONG).show();
}
});
}
private ListView getListView() {
// TODO Auto-generated method stub
return getListView();
}
private void setListAdapter(ArrayAdapter<String> arrayAdapter) {
// TODO Auto-generated method stub
}
};
// binding array to ListAdapter
setListAdapter(new ArrayAdapter<String>
(this,android.R.layout.simple_expandable_list_item_1,result));
selection = (TextView)findViewById(R.id.selection);
// listening to single list item on click
protected void onListItemClick(ListView l,View v, int position, long id){
super.onListItemClick(l, v, position, id);
// selected item
String product = result[position];
Intent ii = new Intent(getApplicationContext(),Desire.class);
// sending data to new activity
ii.putExtra("product", product);
startActivity(ii);
};

how can i get my alertdialog to display listview info?

my alertdialog wont display the textview from my listview. when i click on the listview it only displays the first list of the list view string. could someone please help me!
package com.example.taxime;
import android.app.AlertDialog;
import android.app.ListActivity;
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 FindTaxi extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String[] number = getResources().getStringArray(R.array.number);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,number));
final String[] taxi = getResources().getStringArray(R.array.taxi);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, taxi));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(FindTaxi.this);
int selectedPosition = 0;
adb.setTitle(""+taxi[selectedPosition]);
adb.setMessage("Phone Number: "+number[selectedPosition]);
adb.setPositiveButton(TELEPHONY_SERVICE, null);
adb.setNegativeButton("Cancel", null);
adb.show();
}
});
}
}
(Better late than never, right?)
Instead of this: int selectedPosition = 0;
adb.setTitle(""+taxi[selectedPosition]);
adb.setMessage("Phone Number: "+number[selectedPosition]);
Make use of the int position parameter the onItemClick method receives. So, the three lines of code above might then become: int selectedPosition = position;
adb.setTitle(""+taxi[selectedPosition]);
adb.setMessage("Phone Number: "+number[selectedPosition]);

Categories

Resources