onlistitemclick in AppCompatActivity - android

I followed steps mentioned in this answer to set a toolbar for a listview [listview image][1].
Now the list items are not clickable. When using ListActivity list items were clickable, when clicking on any item it will open another activity with item title and its content.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Note note = posts.get(position);
Intent intent = new Intent(this, EditNoteActivity.class);
intent.putExtra("noteId", note.getId());
intent.putExtra("noteTitle", note.getTitle());
intent.putExtra("noteContent", note.getContent());
startActivity(intent);
}

Do this
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Note note = posts.get(position);
Intent intent = new Intent(this, EditNoteActivity.class);
intent.putExtra("noteId", note.getId());
intent.putExtra("noteTitle", note.getTitle());
intent.putExtra("noteContent", note.getContent());
startActivity(intent);
}
});

Related

How to pass item Click Position in Listview

This is My Main Listview `
lvComments.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(), Subcategories.class);
intent.putExtra("position", i);
startActivity(intent);
}
});
This is code i am using for item position`This is my 2nd Listview its open when i am clicking on main ListView But This opens in all position and i want to open this 2nd listview on 0th position of main(1ST) Listview
lvComments.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(), Subcategories.class);
if(i == 0){
intent.putExtra("position", i);
startActivity(intent);
}else {// do whatever your stuff
}
}
});
In Second Activity
int position = getIntent().getIntExtra("position", 0); // 0 will be default value if there is no value in bundle
If you want to open second listview only when you click on 0th position then use following code.
lvComments.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(), Subcategories.class);
if(i == 0){
intent.putExtra("position", i);
startActivity(intent);
} else {
//do your stuff for other position.
}
}
});

How to transfer the clicked item in listview to editText in another activity in android

I want to get the item on list view to edit Text in another activity.
When clicked on list view item, I want to transfer the item in another activity in edit Text.
You have to make onItemClickListner of listview like that.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);
}
});
Then in the new Activity, retrieve those values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
And finally set Value to editText like this
editText.setText(value);
Hope this will help you.
lstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getActivity(), NewActivity.class);
intent.putExtra("text", text want to transfer);
startActivity(intent);
}
});
You can make use of SharedPreferences. And when you pass the content of the ListView to the next activity, you can use editText.setText("Your Text").
You can also pass your data through intents from which you are calling your new activity.
create onClick method like this.
ListView list = (ListView) findViewById(R.id.newsList);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long offset) {
NewsItem item = (NewsItem) adapter.getItem(position);
Intent intent = new Intent(getApplicationContext(), NewsDetailsActivity.class);
intent.putExtra(KEY, item.getHeadline());
startActivity(intent);
}
});
In next activity
Intent intent = getIntent();
headline = intent.getStringExtra(KEY);
have a look here

How to get data and View from a ListView

Hi everyone:
I hope someone can help me here,The Cursor generated by a Query populate the ListView with the Layout defined by simple_list_item_2.
To create the Intent i need the first string of the clicked View but in the String to Go i have something like
android.widget.TextView#411fbfb8
Now the code.
I can't understand where is the error.
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, columns, ids, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
final ListView myList=(ListView)findViewById(R.id.listView1);
myList.setAdapter(adapter);
myList.setClickable(true);
/*
* Click Listener
*/
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> l, View v, int position, long id)
{
Log.v("io", "start");
Intent intent = new Intent(MainActivity.this, WorkActivity.class);
View buff = myList.getChildAt(position);
String toGo = buff.findViewById(android.R.id.text1).toString();
Log.v("io",toGo);
intent.putExtra("dname", toGo);
startActivity(intent);
}
} );
try:
String toGo = ((TextView)buff.findViewById(android.R.id.text1)).getText().toString()
inside your onItemClick you can retrieve data linked to your adapter through the AdapterView argument. Calling getItemAtPosition(int) you can access data associated with the selected item
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> l, View v, int position, long id)
{
Log.v("io", "start");
Intent intent = new Intent(MainActivity.this, WorkActivity.class);
View buff = myList.getChildAt(position);
String toGo = l.getItemAtPosition(position);
Log.v("io",toGo);
intent.putExtra("dname", toGo);
startActivity(intent);
}
} );
//
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> l, View v, int position, long id)
{
Log.v("io", "start");
Intent intent = new Intent(MainActivity.this, WorkActivity.class);
//View buff = myList.getChildAt(position);
// String toGo = buff.findViewById(android.R.id.text1).toString();
String toGo = (TextView)v.getText();
Log.v("io",toGo);
intent.putExtra("dname", toGo);
startActivity(intent);
}
} );
To get the view do as following:
LinearLayout row = (LinearLayout) ((LinearLayout)v);
Or RelativeLayout instead of LinearLayout if that your case
then apply the follwing code to get the text of TextView based on its order:
TextView column = (TextView)row.getChildAt(0);
String text = column.getText().toString();
Hope this help

Android: How to start new activity for OnItemClick of List View that uses content provider

By using the content provider I have inflated SMS in Inbox List View of my application. Now On item Click I want to show SMS text in another activity. I've implemented custom list view. Now I am not getting how to pick each single list item and show in new activity on click. In a Stack flow answer somebody suggested this:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Uri mailUri = ContentUris.withAppendedId(getIntent().getData(), id);
startActivity(new Intent(Intent.ACTION_VIEW, mailUri));
}
If this code is correct then how I will configure next activity to act upon this ACTION_VIEW?
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int pos, long arg3) {
Intent i= new Intent(currentClass.this,secondActivity.class);
i.putExtra("string",Yourlist.get(pos).sms);
startActivity(i);
finish();
}
});
& on Another Activity You can receive through this:-
String msg=getIntent().getExtras().getString("string");
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position)
{ case 0:
Intent firstIntent = new Intent(AndroidListViewActivity.this, SingleListItem.class);
startActivity(firstIntent);
break;
case 1:
Intent secondintent = new Intent(AndroidListViewActivity.this,jokes.class);
startActivity(secondintent);
break;

Android: ListActivity with ListView start new intent and this intent don't have parent

I've a ListActivity with a ListView, onItemClick i start an Intent,
Oncreate of this Intent i make a getParent but it's null. if i do this.isChild() it's false.
mPostList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent IntentDiscution = new Intent(parent.getContext(), EcrireMessage.class);
....
IntentDiscution.putExtras(objetbunble);
parentClass.startActivity(IntentDiscution);
}
As far as I can tell, the only thing that can have a child Activity is an ActivityGroup.
Take a look at this discussion thread for more details: http://groups.google.com/group/android-beginners/browse_thread/thread/1ed4648486b6af64/
You probably looking for
YourParentClassName.this
code
mPostList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent IntentDiscution = new Intent(YourParentClassName.this, EcrireMessage.class);
....
IntentDiscution.putExtras(objetbunble);
parentClass.startActivity(IntentDiscution);
}

Categories

Resources