How set onclick with LinearLayout (android) - 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"

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.

android, cant resolve getListAdapter and setOnItemLongClickListener inside fragment

In here I am trying to make OnItemLongClick, I tried doing it in ListActivity before and that's working.
Now I am trying to make one inside a Fragment, but I get an error here and there, I need some advice/suggestion/answer to solve the error, or the correct way to implement the method.
In the ListActivity (working):
public class ViewData extends ListActivity implements OnItemLongClickListener {
//init controller
private DBDataSource dataSource;
//init arraylist
private ArrayList<Barang> values;
private Button delButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewdata);
dataSource = new DBDataSource(this);
// open controller
dataSource.open();
// get values
values = dataSource.getAllBarang();
// insert data to array adapter
ArrayAdapter<Barang> adapter = new ArrayAdapter<Barang>(this,
android.R.layout.simple_list_item_1, values);
// set adapter in list
setListAdapter(adapter);
// listview for set onItemLongClickListener
ListView lv = (ListView) findViewById(android.R.id.list);
lv.setOnItemLongClickListener(this);
}
//if user longclick
#Override
public boolean onItemLongClick(final AdapterView<?> adapter, View v, int pos,
final long id) {
//tampilkan alert dialog
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_view);
dialog.setTitle("Pilih Aksi");
dialog.show();
final Barang b = (Barang) getListAdapter().getItem(pos);
delButton = (Button) dialog.findViewById(R.id.button_delete_data);
//apabila tombol delete di klik
delButton.setOnClickListener(
new OnClickListener()
{
#Override
public void onClick(View v) {
// Delete barang
dataSource.deleteBarang(b.getId());
dialog.dismiss();
finish();
startActivity(getIntent());
}
}
);
return true;
}
}
The Fragment version (still giving an error) :
public class Menu_Riwayat extends Fragment {
//init controller
private DBDataSource dataSource;
//init arraylist
private ArrayList<Investasi_DB> values;
static ListView lv;
private Button button_hapus;
static LinearLayout mLinear;
#Nullable
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
mLinear = (LinearLayout) inflater.inflate(R.layout.viewdata, container, false);
lv = (ListView) mLinear.findViewById(R.id.list);
dataSource = new DBDataSource(getActivity());
// open controller
dataSource.open();
// get values
values = dataSource.getAllInvestasi_DB();
// insert data to array adapter
ArrayAdapter<Investasi_DB> adapter = new ArrayAdapter<Investasi_DB>(getActivity(),
android.R.layout.simple_list_item_1, values);
// set adapter in list
lv.setAdapter(adapter);
return mLinear;
// ERROR IN THIS PART BELOW, not sure what to change "this" with
lv.setOnItemClickListener(this);
}
public boolean onItemLongClick(final AdapterView<?> adapter, View v, int pos,
final long id) {
//tampilkan alert dialog
final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.dialog_view);
dialog.setTitle("Pilihan");
dialog.show();
// ERROR IN THIS PART BELOW, not sure how to implement "getListAdapter()" inside fragment
final Investasi_DB b = (Investasi_DB) getListAdapter().getItem(pos);
button_hapus = (Button) dialog.findViewById(R.id.button_hapus);
button_hapus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dataSource.deleteInvestasi_DB(b.getId());
dialog.dismiss();
getActivity().finish();
// ERROR IN THIS PART BELOW, the "getIntent()" part
startActivity(getIntent());
}
});
return true;
}
}
I need help to solve the error (correct way to implement it):
getListAdapter(), getIntent(), and this in lv.setOnItemClickListener(this);
EDITED PART AFTER SOME SUGGESTION :
so now i make it like this
public class Menu_Riwayat extends Fragment {
//inisialisasi kontroller
private DBDataSource dataSource;
//inisialisasi arraylist
private ArrayList<Investasi_DB> values;
static ListView lv;
private Button button_hapus;
static LinearLayout mLinear;
#Nullable
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
mLinear = (LinearLayout) inflater.inflate(R.layout.viewdata, container, false);
lv = (ListView) mLinear.findViewById(R.id.list);
dataSource = new DBDataSource(getActivity());
// buka kontroller
dataSource.open();
// ambil semua data barang
values = dataSource.getAllInvestasi_DB();
// masukkan data barang ke array adapter
ArrayAdapter<Investasi_DB> adapter = new ArrayAdapter<Investasi_DB>(getActivity(),
android.R.layout.simple_list_item_1, values);
// set adapter pada list
lv.setAdapter(adapter);
//lv.setOnItemLongClickListener(this);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.dialog_view);
dialog.setTitle("Pilihan");
dialog.show();
final Investasi_DB b = (Investasi_DB) lv.getItemAtPosition(position);
button_hapus = (Button) dialog.findViewById(R.id.button_hapus);
button_hapus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dataSource.deleteInvestasi_DB(b.getId());
dialog.dismiss();
getActivity().finish();
}
});
}
});
return mLinear;
}
its working, but the apps close itself after i click the button (i think because of getActivity().finish();), any idea about that ?
I need help to solve the error (correct way to implement it):
getListAdapter(), getIntent(), and this in
lv.setOnItemClickListener(this);
if with this you want to access a Context, you can use getActivity(). Fragment is not a Context
In setOnItemClickListener(this);, this refers to the current instance. With that line you are implying that this is also implementing the OnItemClickListener interface. Which is not true. You need yo add implement OnItemClickListener to your Fragment class.
getListAdapter():
instead of getListAdapter() you can use the ListView to retrieve the item at position, since you are already keeping a reference as member. Change
final Investasi_DB b = (Investasi_DB) getListAdapter().getItem(pos);
with
final Investasi_DB b = (Investasi_DB) lv.getItemAtPosition(pos);
why do you need this line startActivity(getIntent());?

On Item Click Listener for List View extending ArrayAdapter

What I have tried:
public class EntryAdapter extends ArrayAdapter<Item> {
private Context context;
private ArrayList<Item> items;
private LayoutInflater vi;
public EntryAdapter(Context context,ArrayList<Item> items) {
super(context,0, items);
this.context = context;
this.items = items;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, final View convertView, ViewGroup parent) {
// // // // NON-FUNCTIONING CODE BELOW
AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getContext(), NewsItemActivity.class);
convertView.getContext().startActivity(intent);
}
};
}
The AdapterView.onItemClickListener doesnt yield any errors but doesnt seem to function whatsoever.
What is the proper way of setting this onClick Listener?
Note: I have to set it in this adapter class, not the main class for my own reasons.
You shuldn't implement a listener inside the getView() unless you what set a listener on a particular view inside your row layout.
You should instead use setOnItemClickListener() method on your ListView:
ListView lv = (ListView) findViewById(R.id.your_listview);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(context, NewsItemActivity.class);
context.startActivity(intent);
}
});
EDIT:
If, for each action inside the onclick, you need information that resides in your Objects (Item) then you can get it in this way:
Item item = (Item)listview.getAdapter().getItem(position);
from inside of onItemClick() method
You can use:
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Make what you want
}
});
Or depending on your item view, you can make the OnCLickListener on your global layout of the item or a specific item's layout
Try by this code :-
Spinner cb_tv;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter;
list.add(phone1);
list.add(name1);
dataAdapter = new ArrayAdapter<String>(Display_Tank.this,android.R.layout.simple_spinner_item, list);
cb_tv.setAdapter(dataAdapter);
cb_tv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_SHORT).show();
}
});
at the place of cb_tv Spinner You can use ListView
First of all, the best way to achieve this is to use your own ViewHolders in ListView.
This is an example of a custom ViewHolder
class MyViewHolder extends ViewHolder implements View.OnItemClickListener {
public MyViewHolder(View view) {
/* Implement your views here
* like this: mTextView = (TextView) view.findViewById(R.id.myId);
*/
view.setOnItemClickListener(this);
}
#Override
public void onClick(View view) {
Intent intent = new Intent(getContext(), NewsItemActivity.class);
convertView.getContext().startActivity(intent);
}
}
And on your getView method you create this ViewHolder and populate your Views with your data.
You can try this way... it is working in my case Change your context to
Activity activity = (Activity) context;
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(activity, NewsItemActivity.class);
activity.startActivity(intent);
}
});

Layout containing listview, unable to switch to new activity onitemclick

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.

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