i am using listview... i try open the image when itemclicked on the list and is diaply in another activity...but its not working... help me if freinds you know... thanks....
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> mainListView, View view, int pos,
long id) {
//Object obj = getFilesDir().getPath();
//String value= obj.toString();
Intent intent= new Intent(SimpleListViewActivity.this,Nextclass.class);
final String root = Environment.getExternalStorageDirectory().getAbsolutePath();
intent.setData(Uri.parse(root + "/mnt/sdcard/dcim/100andro/koala_copy.jpg"));
startActivity(intent);
}
private ArrayAdapter<String> getListAdapter() {
// TODO Auto-generated method stub
return null;
}
my main.xml is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/mainListView">
</ListView>
</LinearLayout>
We'd need to see NextClass's code as well, since that's what is receiving that Intent you're launching. It should be a subclass of Activity. Then in its onCreate or onResume you call getIntent(), check if the intent has the extras/data you need, and load the image into an ImageView or something else.
Related
How could I use onClick event from the base ListView in android without using the Adapter. I what to now witch item is selected and in base of that i want to open another layout.
listview.item="Something"
{
setContentView(R.id.layout.Something);
}
Please help me. I tried with the simple onClick but it doesn't work.
You Need To create layout XML file
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/view1" >
</ListView>
</RelativeLayout>
In Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView mListView = (ListView)findViewById(R.id.listview);
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
}
I am having a simple listview with an ImageView and a textView.
Now whenever I click on my listItem I need to send the ImageView to another activity.
I have read many SO answers but couldn't find a better solution for this.
This is my lisview_items.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/photo"
android:layout_width="150dip"
android:layout_height="100dip"
android:paddingRight="15dp"
android:paddingLeft="15dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="160dp"
android:layout_marginTop="10dp"
android:descendantFocusability="blocksDescendants">
<TextView android:id="#+id/name"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
<RelativeLayout>
CustomAdapter:
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.item_details_view, null);
holder = new ViewHolder();
holder.txt_itemName = (TextView) convertView.findViewById(R.id.name);
holder.itemImage = (ImageView) convertView.findViewById(R.id.photo);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txt_itemName.setText(itemDetailsrrayList.get(position).getName());
holder.itemImage.setImageResource(imgid[itemDetailsrrayList.get(position).getImageNumber() - 1]);
}
MyListView OnClick:
ArrayList<ItemDetails> image_details = GetSearchResults();
final ListView lv1 = (ListView) findViewById(R.id.list);
lv1.setAdapter(new ItemListBaseAdapter(this, image_details));
OnItemClickListener itemClickListener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
ItemDetails obj_itemDetails = (ItemDetails)o;
Toast.makeText(ListViewImagesActivity.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
ProductName =obj_itemDetails.getName();
}
};
I am using sharedpreferences for sending product name as shown below and it works absolutely fine
SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
data.edit().putString("SelectedProduct", ProductName).commit();
In the similar way how do I send the image as it is in ArrayList as shown above..Can anyone shed me some light on this usage
You can store the image path on the ItemDetails object like you are doing with the name. Then you can pass the path via an Intent to the new Activity.
If the picture is available at compile time, you can pass a reference to its id, like Gabe said, instead of the path to its location.
If you are actually switching to that activity, you can pass the resource id of the image in the intent extras and then use getResources().getDrawable(ID) there.
You should the following :
final ArrayList<ItemDetails> image_details = GetSearchResults();
final ListView lv1 = (ListView) findViewById(R.id.list);
lv1.setAdapter(new ItemListBaseAdapter(this, image_details));
OnItemClickListener itemClickListener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
ItemDetails selectedItem = image_details.get(position);
//To-DO get you data from the ItemDetails Getter
// selectedItem.getImage() or selectedItem.getName() .. etc
// the send the data using intent when opening another activity
Intent intent = new Intent(CurrentActivity.this, DisplayImageActivity.class);
intent.putExtra("image",selectedItem.getImage());
intent.putExtra("name",selectedItem.getName());
startActivty(intent);
}
};
Then in your DisplayImageActivity in the OnCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
String image = i.getStringExtra("image");
String name = i.getStringExtra("name");
}
Does somebody know why ListActivity won't work and when I change it to Activity base class, with slight changes it works, but onClick() method still doesn't. I'm trying only to put some strings into the list... Here's the code:
public class TestDataBaseActivity extends Activity implements OnClickListener {
private CommentsDataSource datasource;
ListView m_list;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datasource = new CommentsDataSource(this);
datasource.open();
List<Comment> values = datasource.getAllComments();
m_list = (ListView) findViewById(R.id.listView1);
// Use the SimpleCursorAdapter to show the
// elements in a ListView
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
m_list.setAdapter(adapter);
Button add = (Button)findViewById(R.id.button_add);
Button delete = (Button)findViewById(R.id.button_delete);
add.setOnClickListener(this);
delete.setOnClickListener(this);
}
// Will be called via the onClick attribute
// of the buttons in main.xml
#Override
public void onClick(View view) {
#SuppressWarnings("unchecked")
ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) m_list.getAdapter();
Comment comment = null;
switch (view.getId()) {
case R.id.button_add:
String[] comments = new String[] {
"Cool", "Very nice", "Hate it"
};
int nextInt = new Random().nextInt(3);
// Save the new comment to the database
comment = datasource.createComment(comments[nextInt]);
adapter.add(comment);
break;
case R.id.button_delete:
if (m_list.getAdapter().getCount() > 0) {
comment = (Comment) m_list.getAdapter().getItem(0);
datasource.deleteComment(comment);
adapter.remove(comment);
}
break;
}
adapter.notifyDataSetChanged();
}
#Override
protected void onResume() {
datasource.open();
super.onResume();
}
#Override
protected void onPause() {
datasource.close();
super.onPause();
}
}
Here is XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TestDataBaseActivity" >
<Button
android:id="#+id/button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_new" />
<Button
android:id="#+id/button_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/button_add"
android:text="#string/delete_first" />
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/button_add"
android:padding="15dp" >
</ListView>
</RelativeLayout>
I edited now to extend Activity, now "only" onClick() dont work...
Thanks in advance!
For a ListActivity, you should override onListItemClick() instead:
#Override
protected void onListItemClick(ListView lv, View v, int position, long id){
}
Since you are extending ListActivity you need to have an Listview with android:id="#android:id/list".
if you want to use #+id/lst_id then dont extend ListActivity, just extended Activity.
And whenever you are using Listview you need to implement OnItemClickListener not OnClickListener.
and override the default OnItemClick method
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
you should implement OnClickListener
public class TestDatabaseActivity extends ListActivity implements
OnClickListener{
try following code a simple list activity
public class MainActivity extends ListActivity implements OnItemClickListener {
ListView listView;
static final String[] SPORTS = {"Shuttle Badminton", "Tennis", "FootBall",
"Basket Ball","Table Tennis", "Chess","Hockey"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView = getListView();
//setting adapter
listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(),R.layout.list_item,SPORTS));
listView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
TextView tv = (TextView)view.findViewById(R.id.text1);
Toast.makeText(getApplicationContext(),"Position is: "+position,Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),"Text is: "+tv.getText().toString(),Toast.LENGTH_SHORT).show();
}
}
here list_item is an xml having following code
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#android:color/black"
android:padding="10dp" />
Use this code for list activity and modify row xml according to your needs.
hope this helps..
This is what's troubling you:
ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) m_list.getAdapter();
Build a global object for your adapter instead. ( Declare it before onCreate() )
I am developing first android apps and I have a fragment class which extends SherlockListFragment. When I set setOnItemClickListener in onCreateView and implement onItemClick I'm getting no response when I click on a row(list). list display properly in my apps. I want when user click on row form listview my another activity will call. I goggled lot but not got proper answer.
I can't understand where is the problem. Can anyone tell me what am I doing wrong in following code?
following is the my code
public class ResidentialFragActivity extends SherlockListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle saved) {
View view = inflater.inflate(R.layout.project_list,container, false);
resListviewId = (ListView)view.findViewById(android.R.id.list);
linearProgress = (LinearLayout)view.findViewById(R.id.linlaHeaderProgress);
projectList = new ArrayList<HashMap<String,String>>();
new LoadProjects().execute();
resListviewId.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
Intent href = new Intent(getSherlockActivity(), ProjectDetailActivity.class);
// send project id to project detail activity to get list of Phases under that project
String proj_id = ((TextView) view.findViewById(R.id.projectId)).getText().toString();
href.putExtra("proj_id", proj_id);
getSherlockActivity().startActivity(href);
}
});
return view;
}
//inner class for network operation
private class LoadProjects extends AsyncTask<String, String, String> {
protected void onPreExecute() {
linearProgress.setVisibility(View.VISIBLE);
}
#Override
protected String doInBackground(String... params) {
//call webservice
}
#Override
protected void onPostExecute(String result) {
linearProgress.setVisibility(View.GONE);
ListAdapter projAdapter = new SimpleAdapter(getSherlockActivity(),
projectList, R.layout.project_list_item,
new String[] {PROJ_ID,PROJ_NAME,EDIT_RIGHTS}, new int[]
{R.id.projectId,R.id.projectName,R.id.projectSubName});
//updating the UI
setListAdapter(projAdapter);
}
}
}
following is the layout for listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10dp"
android:background="#color/White"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:divider="#drawable/divider"
android:focusableInTouchMode="true"
android:smoothScrollbar="true"
android:dividerHeight="1.5dp"
android:scrollbarThumbVertical="#drawable/divider"
android:drawSelectorOnTop="false"
android:listSelector="#drawable/list_selector">
</ListView>
</LinearLayout>
Thanks in advance..
You should override onListItemClick instead of instead of resListviewId.setOnItemClickListener .. as you are setting the adapter to the default one in setListAdapter(projAdapter);
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Intent href = new Intent(getSherlockActivity(), ProjectDetailActivity.class);
// send project id to project detail activity to get list of Phases under that project
String proj_id = ((TextView) view.findViewById(R.id.projectId)).getText().toString();
href.putExtra("proj_id", proj_id);
startActivity(href);
}
I have followed this tutorial
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
and like to add an on click for the listview.
now here is my main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#drawable/list_divider"
android:dividerHeight="1px"
android:cacheColorHint="#00000000"/>
</LinearLayout>
and here is my code:
setContentView(R.layout.main);
steden = new ArrayList<voorDeLijst>();
this.m_adapter = new StedenAdapter(this, R.layout.list_item, steden);
setListAdapter(this.m_adapter);
ListView lv = (ListView)findViewById(R.id.List);
lv.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
AlertDialog.Builder adb = new AlertDialog.Builder(HelloAndroid.this);
adb.setTitle("LVSelectedItemExample");
adb.setMessage("Selected Item is = ");
adb.setPositiveButton("Ok", null);
adb.show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
The thing is that I am a beginner and with the code above I get an error because It cannot locate the listview. So I can't attach a OnItemClick Listener to it.
but when I change <ListView android:id="#+id/android:list" to <ListView android:id="#+id/List"
Then I can find the listview. but it gives an exception at the line: setContentView(R.layout.main);
So how do I attach an onClick/onItemClick to a Listview which has a custom adapter to bind objects to the listitems?
Found it, because my class extended the ListActivity I could do this:
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
...
}
I found it at http://www.anddev.org/viewtopic.php?t=22
Have you tried findViewById(android.R.id.list)? Really though, if your activity is basically one big list view, then you should be using a ListActivity for your activity, and that has an accessor that gives you direct access to the ListView.