Layout containing listview, unable to switch to new activity onitemclick - android

I am having a listview as part of a mainLayout and Onclickevent on listview is not working. Can anybody give me pointer where I am wrong?
Here's the code snippet:
public class HostListActivity extends ListActivity {
public ArrayList<String> deviceList;
static LinearLayout mainLayout;
public ListView hostListView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
designLayout();
setContentView(mainLayout);
}
public void designLayout() {
mainLayout = new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
int lHeight= LinearLayout.LayoutParams.FILL_PARENT;
int lWidth = LinearLayout.LayoutParams.WRAP_CONTENT;
AddHostListView(lHeight,lWidth);
}
private void AddHostListView(int lHeight, int lWidth) {
deviceList = new ArrayList<String>();
deviceList.add("abc");
deviceList.add("efg");
deviceList.add("hij");
hostListView = new ListView(this);
hostListView.setId(android.R.id.list);
hostListView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, deviceList));
hostListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent myIntent = new Intent(v.getContext(), DirBrowserActivity.class);
startActivity(myIntent);
}
});
mainLayout.addView(hostListView, new LinearLayout.LayoutParams (lHeight,lWidth));
}
// DirBrowserActivity class
public class DirBrowserActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, DirBrowse View");
setContentView(tv);
}
}

You extend ListActivity but you do not use any feature from this class. I have noticed that this correlates with not working OnItemClickListener. Simply changing:
public class HostListActivity extends ListActivity {
to
public class HostListActivity extends Activity {
will help.

You should put this code
registerForContextMenu(getListView());
after setting an adapter to your listview, if you want to add a context menu to your items. If you just want to make smth on an item's click, you need to ovverride this method:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
}
Hope this helps.

Related

Clickable custom array list

How can I make this custom array list clickable to go to the others activities because I tried the intents but it doesn't work
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Names> namesArrayList = new ArrayList<Names>();
namesArrayList.add(new Names(R.drawable.call_centre, "Call Centre"));
namesArrayList.add(new Names(R.drawable.soco_academy_icon, "Academy"));
NamesAdapter NamesListAdapter = new NamesAdapter(this, namesArrayList);
ListView list = (ListView) findViewById(R.id.List_View);
list.setAdapter(NamesListAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}
In onItemClick() you can determine which item was clicked by doing this:
Name selectedName = NamesListAdapter.getItem(position);
Then you can do whatever you want with that.

How set onclick with LinearLayout (android)

I have used the following tutorial "https://www.simplifiedcoding.net/android-volley-tutorial-to-get-json-from-server/ "to display data onto Android using ListView and LinearLayout. I want to go to another screen when I click on an item from the list. I have added this to my MainActivity but it didn't work:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//ListView lv = getListView();
buttonGet = (Button) findViewById(R.id.buttonGet);
buttonGet.setOnClickListener(this);
listView = (ListView) findViewById(R.id.listView);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
sendRequest();
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, EventDetails.class);
MainActivity.this.startActivity(intent);
}
});
}
You'll have to set the LinearLayout to clickable. You can either do this in the XML with adding below in linearlayout tag
android:clickable="true"
Or in code with
linearLayout.setClickable(true);
That tutorial uses a ListView, not a LinearLayout.
You add Item-ClickListeners to ListViews.
final Context ctx = YourActivity.this;
yourListView = (ListView) findViewById...
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Data clicked = adapter.getItem(position);
// Do something with 'clicked'
// startActivity(ctx, ShowDataActivity.class);
}
});
int example_layout = android.R.simple_list_item_1;
adapter = new ArrayAdapter<Data>(ctx, example_layout, new ArrayList<Data>());
yourListView.setAdapter(adapter);
By the way, that tutorial uses a deprecated version of Volley
You can implement a public interface to manage the click events on the adapter elements. To do this, define a public interface in your adapter (in the tutorial is the CustomList class)
public class CustomList extends ArrayAdapter<String> {
public interface OnClickOnItemList
{
public void clickInView(View v,int position);
}
private OnClickOnItemList mListener;
public setOnClickOnItemListener(OnClickOnItemList l) {this.mListener = l}
/*...*/
}
And in the same class go to getView. You should assign this new listener to the event onClick
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.list_view_layout, null, true);
linearLayout = (LinearLayout) listViewItem.findViewById(R.id.linearLayout);
/*Rest of subviews*/
/*Linear Layout now will call the new listener*/
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mListener.clickInView(listViewItem,position);
}
});
return listViewItem;
}
}
Subsequently, the main activity must implement this public interface
public class MainActivity extends AppCompatActivity implements CustomList.OnClickOnItemList
{
/*Code*/
#Override
public void clickInView(View v,int position)
{
Intent intent = new Intent(MainActivity.this, EventDetails.class);
startActivity(intent);
}
private void showJSON(String json){
ParseJSON pj = new ParseJSON(json);
pj.parseJSON();
CustomList cl = new CustomList(this, ParseJSON.ids,ParseJSON.names,ParseJSON.emails);
cl.setOnClickOnItemListener(this);
listView.setAdapter(cl);
}
}
You can add in your xml
<LinearLayout
android:id="#+id/yourid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"

ListView setOnClickLisiner

I press a list item in my listView. How I can go to my another activity from list view item?
Please help me to understand how to solve this problem.
Here is my code if:
package com.example.list;
import java.util.ArrayList;
public class MainActivity extends ListActivity {
ListView listView;
Context context;
ArrayList prgmList;
public static int[] prgmImage = { R.drawable.a_2, R.drawable.a_2,
R.drawable.a_2, R.drawable.a_2, R.drawable.a_2, R.drawable.a_2,
R.drawable.a_2, R.drawable.a_2
};
public static String[] prgmNameList = { "Let Us C", "c++", "JAVA", "Jsp",
"Microsoft .Net", "Android", "PHP", "Jquery", "JavaScript" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 0);
setContentView(R.layout.activity_main);
context = this;
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(new CustomAdapter(this, prgmNameList, prgmImage));
}
}
You have to implement
OnItemClickListener
to your activity and set it to your listView:
public class MainActivity extends Activity implements OnItemClickListener {
...
...
listview.setOnItemClickListener(this);
Then you will get a new method:
#Override
public void onItemClick(AdapterView<?> arg0, View view, int pos, long id) {
Log.e(TAG, "Pos: " + pos);
}
Here you get the position of the licked item
Hi for solving you're problem you need just to call this method onListItemClick.This method show witch row you select and you could set some intents as well.
protected void onListItemClick (ListView l, View v, int position, long id)
{
Toast.makeText(this, "Clicked row " + position, Toast.LENGTH_SHORT).show();
//go to another activity
Intent intent = new Intent();
intent.setAction(this, SecondActivity.class);
startActivity(intent);
}
Just set an OnItemClickListener to the ListView like:
final Context context = this;
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent();
intent.putExtra("position", position);
intent.setAction(context, OtherActivity.class);
startActivity(intent);
}
});

How to pass a jpg id between activities Android

I have a gridView with imageViews in one Activity and I want to load one of them in other activity on a new ImageView when select it, but i have no results
Activity 1:
public class Level extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level);
GridView gridview = (GridView) findViewById(R.id.gridviewLevel);
ImageAdapter ia = new ImageAdapter(this);
gridview.setAdapter(ia);
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(Level.this, "" + position, Toast.LENGTH_SHORT).show();
initImageCheck( v.getId());
}
});
}
public void initImageCheck(int id){
Intent intentIC = new Intent(this, ImageCheck.class);
intentIC.putExtra("ID", id);
startActivity(intentIC);
}
}
Activity 2:
public class ImageCheck extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_check);
Bundle extras = getIntent().getExtras();
int id = extras.getInt("ID");
ImageView imgView = (ImageView) findViewById(R.id.imageViewCheck);
imgView.setImageResource(id);
}
}
Whats the problem??? Thanks.
v.getId() is the ID of the View, not of the image resource.
Have ImageAdapter also hold the id of resource used per item, and also provide a method like getResourceIdForItem(int position).

How do I select a row in a listview not using ListActivity

Here is the code:
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Find your views
final ListView listPro = (ListView) findViewById(R.id.listProperty);
DBAdapter db = new DBAdapter(this);
db.open();
final Cursor c = db.getAllProperties();
startManagingCursor(c);
// Create the adapter
MainAdapter adapter = new MainAdapter(this, c);
listPro.setAdapter(adapter);
// here is where I think I should put the code to select the row but I
// haven't been able to figure out how to do it.
// all I need to get out is the row id number.. please help
Button addPropBut = (Button) findViewById(R.id.mainButton);
addPropBut.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i7 = new Intent(Main.this, Home.class);
startActivity(i7);
}
});
}
}
try this one, hope it will help you out.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
}
you can get the id number by using Log or System.out.println();
Instead of using Button's setOnClickListener use ListView's setOnItemClickListener.
listPro.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
The id parameter will give you the needed id.
Salil.

Categories

Resources