Android Dragging ListView and ContextMenu - java

I'm still working with dragging listview (example from google https://www.youtube.com/watch?v=_BZIvjMgH-Q) - long click by listview item - goes dragging and I want to implement context menu on short click, but my changes do not seem towork. After short click on listview item goes dragging effect..
here is my code for activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Utils.changeTheme(this,getApplicationContext());
Utils.setLang(this,getApplicationContext());
setContentView(R.layout.activity_list);
listView = (DynamicListView) findViewById(R.id.listview);
adapter = new StableArrayAdapter(this, R.layout.text_view, productsArray);
listView.setList(productsArray);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
registerForContextMenu(listView);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
view.showContextMenu();
}
});
}
With simple listview, short click working without any problems...
Could anybody explain what I'm doing wrong?
Thx in advance!

The best solution in this case is use popup menu instead of context menu.

Related

Check if setOnItemClickListener is selected

I wrote a simple program that will display an ArrayList and the user can select an item and delete it with a button.
If the user does not select an item but continues to hit the delete button, they will remove the first item on the ArrayList. How do I prevent this from happening? The delete button should only run when an item is selected, so I need to find a way to check for that
int positionID;
ListView listview;
ArrayList<String> valuesArray = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
valuesArray.add("a");
valuesArray.add("b");
valuesArray.add("c");
valuesArray.add("d");
valuesArray.add("e");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, valuesArray);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
positionID = (int) id;
}
});
}
public void onClickDelete(View v) {
//need to check if an item is selected. if so, run code below
valuesArray.remove(positionID);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, valuesArray);
listview.setAdapter(adapter);
}
Based on the code you've provided, this is likely happening because the default initialized value for positionID is set to 0. To fix this, you could initialize int positionID = -1 and then in your onClickDelete method, do a check to make sure the positionID is not invalid. Like this:
public void onClickDelete(View v) {
if (positionID < 0) return;
// continue your code here
}
By the way, a few other things you should fix. You should actually interact with adapter directly, so instead of valuesArray.remove(positionID) you should do adapter.remove(positionID). This will automatically update the adapter and refresh the ListView for you so you can get rid of the last two lines of your onClickDelete method.
You can just make valuesArray a local variable in your onCreate method, unless you plan on manipulating it directly elsewhere in your code. If you choose to do that, you can call adapter.notifyDataSetChanged() to make the adapter refresh your ListView.

Click an item on listview and have a hidden/new item appear

What I'm trying to do is hide the next item on listview, when we click previous items, the next hidden item will appear:
Example: Click A (item on listview), Apple (hidden next item on listview) will appear along with the sound. (ex: Apple)
[]
My code on activity :
public class SecondActivity extends AppCompatActivity {
ArrayAdapter<String> adapter;
ListView GrelistView;
String[] greetings = {"A",
"Apple",
"B",
"Ball"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
GrelistView = (ListView) findViewById(R.id.GrelistView);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, greetings);
GrelistView.setAdapter(adapter);
GrelistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + "is selected", Toast.LENGTH_LONG).show();
}
});
}
I think #Chirag.T is right, what you are looking for is ExpendableListView , in which a down arrow for the subcategories will appear , when you click on that down arrow, your sub categories will show up.
Refer this below link -
https://developer.android.com/reference/android/widget/ExpandableListView.html

listview item click event not working in android

I have a ListViewin my ListView show ImageButton, ImageView, Button, TextView
I set ListView.OnItemClickListner. When I run my project, it shows my ListView.
But when i click on listview it's not working
what is wrong? and I want button, ImageButon,ImageView click events should call Activity or dialog ? Help me?
This is the code:
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
Toast.makeText(getActivity(), "sdadad",1).show();
}
});
Add one tag in parent layout of your custom layout
android:descendantFocusability="blocksDescendants"

Deleted item still showing on the Result page.

I'm able to edit or delete single item without any problem but when I click delete button after edit an item, the item is still showing on the ListView even though it has been deleted from the database.
ListViewActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_requests);
requestsList = new ArrayList<HashMap<String, String>>();
new LoadAllRequests().execute();
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String request_id = ((TextView) view.findViewById(R.id.request_id)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
ViewRequestActivity.class);
// sending rid to next activity
in.putExtra(TAG_ID, request_id);
// starting new activity and expecting some response back
startActivityForResult(in, 55);
}
});
}
Instead of closing and reopening the activity, you should use invalidateViews
yourListView.invalidateViews();
You have to sign the adapter to the listview again
You can also try refreshing the activity. I believe that it is ListViewActivity.restart();

Changing an image in a dialog box before showing it

I have an activity which consists of a GridView with multiple (different) images. When the user clicks on one image, a customized dialog box shows up in which an enlarged version of the image appears as well as some text below.
What I want accomplished: the image shown in the dialog box must correspond to the chosen image in the GridView. I thought that I could change the image resource in the onItemClick method, but the application crashes instead (only when I use the setImageResource command).
Here is the code:
public class Releases extends OptionsActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.releases);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ReleasesImgAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
//Toast.makeText(Releases.this, "" + position, Toast.LENGTH_SHORT).show();
if (position == 0)
{
CustomizeDialog cdWCE = new CustomizeDialog(Releases.this, R.layout.releases_popup);
ImageView wce = (ImageView)findViewById(R.id.rlsImg);
wce.setImageResource(R.drawable.image2);
cdWCE.show();
}
}
});
}
}
releases.xml simply consists of a GridView.
I can't debug the program because my Eclipse debugging haven't been working for a while now.
Try finding the ImageView in the actual dialog where you set the layout (e.g.
ImageView wce = (ImageView)cdWCE.findViewById(R.id.rlsImg);

Categories