how can i get my alertdialog to display listview info? - android

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

Related

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 )

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

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 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