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);
};
Related
how can I make WebView can open the fragment listview? this is my code, and i got error code in :
lv.setOnItemClickListener(new OnItemClickListener() : The method setOnItemClickListener(AdapterView.OnItemClickListener) in the type AdapterView is not applicable for the arguments (new OnItemClickListener(){})
package info.androidhive.slidingmenu;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.content.Intent;
import java.util.ArrayList;
import java.util.HashMap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class FindPeopleFragment extends Fragment {
public FindPeopleFragment(){}
protected ListView lv;
protected ListAdapter adapter;
public static final String MOVIE_DETAIL_KEY = "movie";
SimpleAdapter Adapter;
HashMap<String, String> map;
ArrayList<HashMap<String, String>> mylist;
String[] Pil;
String[] Ltn;
String[] Gbr;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_pulau, container,false);
ListView lv = (ListView) rootView.findViewById(R.id.lv);
Pil = new String[] {"Pulau Gusung", "Binatang Laut Khas"};
Ltn = new String[] {"Baca Selengkapnya...", "Baca Selengkapnya..."};
Gbr = new String[] {Integer.toString(R.drawable.ic_photos),
Integer.toString(R.drawable.ic_photos),
};
mylist = new ArrayList<HashMap<String,String>>();
for (int i = 0; i < Pil.length; i++){
map = new HashMap<String, String>();
map.put("list", Pil[i]);
map.put("latin", Ltn[i]);
map.put("gbr", Gbr[i]);
mylist.add(map);
}
Adapter = new SimpleAdapter(getActivity(), mylist, R.layout.item_kepulauan,
new String[] {"list", "latin", "gbr"}, new int[] {R.id.tv_nama, R.id.tv_des, R.id.imV});
lv.setAdapter(Adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) lv
.getItemAtPosition(position);
if (position == 0) {
Intent myIntent = new Intent(getApplicationContext(),
Story.class);
startActivity(myIntent);
}else if (position == 1) {
Intent myIntent = new Intent(getApplicationContext(),
Story.class);
startActivity(myIntent);
}
// Show Alert
Toast.makeText(
getApplicationContext(),
"Position :" + itemPosition + " ListItem : "
+ itemValue, Toast.LENGTH_LONG).show();
}
});
return rootView;
}
}
The issue is because you're using a new instance of OnItemClickListener when it should be AdapterView.OnItemClickListener. You've imported AdapterView, but not the inner interface, and there is no standalone OnItemClickListener interface so the types do not line up.
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
}
}
});
Hi below is some code ive been playing with but when i debug it never gets to the onitemclicklisten routine can anyone help?
package sanderson.swords.mobilesales;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
public class OrderProductSearch extends Activity {
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> item = new HashMap<String,String>();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
setContentView(R.layout.orderproducts);
}
catch (Exception e) {
//
String shaw="";
shaw = e.getMessage();
}
//Create view of the list where content will be stored
final ListView listContent = (ListView)findViewById(R.id.orderproductlistview);
//Set for fast scrolling
listContent.setFastScrollEnabled(true);
//Create instance of the database
final DbAdapter db = new DbAdapter(this);
//Open the Database and read from it
db.openToRead();
//Routine to call all product sub groups from the database
final Cursor cursor = db.getAllSubGroupProduct();
//Manages the cursor
startManagingCursor(cursor);
int i=0;
cursor.moveToFirst();
while (cursor.getPosition() < cursor.getCount()) {
item.put("ProdName",cursor.getString(2));
item.put("ProdSize", cursor.getString(3));
item.put("ProdPack",cursor.getString(4));
item.put("OrdQty","0");
//list.add(item);
list.add(i, item);
item = new HashMap<String,String>();
cursor.moveToNext();
i = i + 1;
}
String[] from = new String[] {"ProdName", "ProdSize", "ProdPack", "OrdQty"};
int[] to = new int[] { R.id.productlinerow, R.id.productlinerow2, R.id.productlinerow3, R.id.productlinerow4};
SimpleAdapter notes = new SimpleAdapter(OrderProductSearch.this,list,R.layout.productlinerow,from,to);
listContent.setAdapter(notes);
//Close the database
db.close();
listContent.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String neil = "test";
neil = neil + "test";
}
});
}
}
An easier way to verify the click listener is to add some logcat prints in it, e.g.
listContent.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.d(TAG, "Click!");
}
});
If you set a breakpoint at
listContent.setOnItemClickListener(new OnItemClickListener() {
it may not break when you click.
Try setting a breakpoint at
String neil = "test";
and debugging.
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();
}
}
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]);