I have a ListView in my Activity whose rows are populated by logins fetched from a database. Each row in my ListView contains one TextView inside a Constraint Layout. When user touches one of the rows of this ListView, another activity should be launched. I used OnItemClickListener to achieve this and everything seems to work fine, but onClickItem() method from Listener receives a reference to object of type ConstraintLayout and I need to obtain text from TextView inside this layout. How can I do this?
Here is my xml of a simple row:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/row"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="TextView"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
And here is my implementation of OnItemClickListener:
private AdapterView.OnItemClickListener myItemClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView currentView = (TextView) view; // Here I get an exception that ConstraintLayout cannot be casted to TextView
String login = currentView.getText().toString();
Intent intent = new Intent(Powitanie.this,UserPanel.class);
intent.putExtra(LOGIN, login);
Powitanie.this.startActivity(intent);
}
};
We can use getChildAt() method. onItemClick() receives reference to a view. We can cast it to ConstraintLayout and get a child from a specified index.
private AdapterView.OnItemClickListener myItemClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ConstraintLayout layout = (ConstraintLayout) view;
TextView currentView = (TextView) layout.getChildAt(0); // In getChildAt we need to specify index of a child. In this case it's 0.
String login = currentView.getText().toString();
Intent intent = new Intent(Powitanie.this,UserPanel.class);
intent.putExtra(LOGIN, login);
Powitanie.this.startActivity(intent);
}
};
Related
I made a custom listView and custom array adapter with 1 image and 2 textviews. What I want to work is when you click on an item in the listview that it opens a new activity with an intent.
this is the mainactivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.?_main);
ListView listView = (ListView) findViewById(R.id.list);
ArrayList<Subjects> obsclassiclist = new ArrayList<Subjects>();
obsclassiclist.add(new Subjects("?", "?", R.mipmap.ic_launcher));
obsclassiclist.add(new Subjects("?", "?", R.mipmap.ic_launcher));
WordAdapter adapter = new WordAdapter(this, obsclassiclist);
listView.setAdapter(adapter);
LinearLayout listViewIntent = (LinearLayout) findViewById(R.id.intent);
listViewIntent.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
if (position==0) {
Intent tutorial = new Intent(view.getContext(),
Cl_01.class);
// Start the new activity
startActivityForResult(tutorial,0);
}
if (position==1) {
Intent requirements = new Intent(view.getContext(),
CL_02.class);
// Start the new activity
startActivityForResult(requirements,1);
}
}
});
}
}
this the custom adapter
public WordAdapter(Activity context, ArrayList<Subjects> subjects) {
// Here, we initialize the ArrayAdapter's internal storage for the context
and the list.
// the second argument is used when the ArrayAdapter is populating a single
TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the
adapter is not
// going to use this second argument, so it can be any value. Here, we used
0.
super(context, 0, subjects);
}
/**
* Provides a view for an AdapterView (ListView, GridView, etc.)
*
* #param position The position in the list of data that should be displayed
in the
* list item view.
* #param convertView The recycled view to populate.
* #param parent The parent ViewGroup that is used for inflation.
* #return The View for the position in the AdapterView.
*/
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
// Get the {#link subject} object located at this position in the list
Subjects currentSubjects = getItem(position);
// Find the TextView in the list_item.xml layout with the ID subject
TextView subjectTextView = (TextView)
listItemView.findViewById(R.id.subject);
// Get the version name from the current wordadapter object and
// set this text on the subject TextView
subjectTextView.setText(currentSubjects.getObsclassicSubject());
// Find the TextView in the list_item.xml layout with the ID description
TextView descriptionTextView = (TextView)
listItemView.findViewById(R.id.description);
// Get the version number from the current wordadapter object and
// set this text on the description TextView
descriptionTextView.setText(currentSubjects.getObsclassicDescription());
// Find the Imageview in the list_item.xml layout with the ID version_number
ImageView pictureImageView = (ImageView)
listItemView.findViewById(R.id.image);
// Get the version number from the current wordadapter and
// set this image on the imageview
pictureImageView.setImageResource(currentSubjects.getObsclassicimageid());
// Return the whole list item layout (containing 2 TextViews and an
ImageView)
// so that it can be shown in the ListView
return listItemView;
}
}
here is the xml custom listview
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="#+id/intent"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/image"
android:scaleType="centerInside"
android:src="#mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/subject"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingBottom="10dp"
android:textSize="18sp"
android:textStyle="italic"
android:id="#+id/description"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
and the xml from the main activity
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list">
</ListView>
if somebody has a solution pls let me know
You are doing wrong, your are making clicklistener on linear layout, instead do it on ListView
Change this
listViewIntent.setOnItemClickListener(new
AdapterView.OnItemClickListener()
To
listView .setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
Toast.makeText(SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT).show();
// Now if you want to startActivity
Intent tutorial = new Intent(MainActivity.this,
Cl_01.class);
// Start the new activity
startActivityForResult(tutorial,position);
}
});
I've created a ListView that can display a list of items and subitems, following this question: android-listview-subitems.
However, when I click on one of the rows, I get the error: E/MessageQueue-JNI: java.lang.ClassCastException: android.widget.TwoLineListItem cannot be cast to android.widget.TextView
The error only started occurring once I added a subitem to each item in the ListView.
The error occurs in the listener for the listView which is this code below:
//listview listener
lvAppFeatures.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//find the selected item and store the value to send to next screen
strSelectedFeature = ((TextView) view).getText().toString();
//move to display screen
Intent switchActivity = new Intent(MainActivity.this,
NextActivity.class);
startActivity(switchActivity);
}
});
Is there a way to have a set of items and a subitems in a ListView but have intent to open another activity work?
It seems you're using android.R.layout.simple_list_item_2
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="2dip"
android:paddingBottom="2dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/listItemFirstLineStyle"/>
<TextView android:id="#android:id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#android:id/text1"
style="?android:attr/listItemSecondLineStyle" />
</TwoLineListItem>
A)
get item directly from adapter
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//find the selected item and store the value to send to next screen
strSelectedFeature = (String)parent.getAdapter().getItem(position);
or B), if you want to have access to textviews
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//find the selected item and store the value to send to next screen
TextView textView1 = view.findViewById(android.R.id.text1);
TextView textView2 = view.findViewById(android.R.id.text2);
//do other things like textView1.getText() etc
I have a class that extends Fragment. I have a ListView. When I click on an item, it return always the same item.
Here the row.xml of the ListView adapter.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="#+id/IdItem"/>
</LinearLayout>
And here the event:
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
myTextView = (TextView)getActivity().findViewById(R.id.IdItem);
String IdItem = myTextView.getText().toString();
Intent intent = new Intent(getActivity(), NextActivity.class);
intent.putExtra("IdItem", IdItem);
startActivity(intent);
}
});
It always returns the same IdItem, and not the choosen one.
Change this:
myTextView = (TextView)getActivity().findViewById(R.id.IdItem);
for this:
myTextView = view.findViewById(R.id.IdItem);
The rows objects of a ListView are reused in order to save memory, so you need to use the View object being sent in the onItemClick method of the ListView. This View object represents the actual clicked row inside the ListView with current child Views and values.
I want to get the text of a TextView that is in a ListView's item.
See the code and you will find what's my question.
Activity.java:
SimpleAdapter adapter = new SimpleAdapter(
rootView.getContext(),
result,
R.layout.article_list_item,
new String[]{"article_title", "article_PubDate", "article_category", "article_articleNo"},
new int[]{R.id.article_title, R.id.article_PubDate, R.id.article_category, R.id.article_articleNo});
ListView articleItem.setAdapter(adapter);
articleItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//I want get the text of R.id.article_articleNo(see article_list_item.xml)?
//What should I do?
}
});
article_list_item.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/article">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文章标题"
android:id="#+id/article_title"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30-Apr-2014"
android:id="#+id/article_PubDate"
android:layout_gravity="center_horizontal"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="personal_finance"
android:id="#+id/article_category"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9312"
android:id="#+id/article_articleNo"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp"/>
</LinearLayout>
</LinearLayout>
That's all!!
I want to get the text of R.id.article_article (see article_list_item.xml).
How can I do it?
Does this work for you?
articleItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = result.get(position).get("article_articleNo");
//^^ does not necessarily need to be a string,
// make this whatever data type you are storing in the map
}
});
The idea is that in your SimpleAdapter, you are populating your ListView with a List<Map<String,Object>> (in this case named result).
Therefore, you can get that specific Map<String,Object> from the List by using .get(position), then once again using .get("article_articleNo") to get the Object from the map.
Edit after seeing your comment: Try this:
articleItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tv = (TextView) view.findViewById(R.id.article_articleNo);
String text = tv.getText().toString();
}
});
First of all, forget about the xml layout. It's just a placeholder for data, which is used to define the layout of the data, and has got nothing to do with storing the data.
Now, your problem statement can be re-stated as: how to fetch data from an array (or list or arraylist or whatever the format of your result is) on clicking list item.
Hmm. Sounds simple now.
Refer to #RougeBaneling's answer for technical details.
I trying to write code to highlight the selected value of the list with "Next" button at the bottom of the layout. But for some reason, after every list item, "next" button also shows up. Can someone please help me resolve this problem?
Here is the layout file:
<LinearLayout 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"
android:orientation="vertical"
android:id="#+id/questionLayout"
>
<TextView android:id="#+id/txtExample"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="#000000"
android:background="#FF0000"
/>
<ListView
android:id="#+id/listExample"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#CCCCCC"
android:choiceMode="singleChoice"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<Button
android:id = "#+id/next"
android:text="Next"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_gravity="center"
android:layout_weight="50"
/>
<Button
android:id = "#+id/submit"
android:text="Submit"
android:layout_width = "0dp"
android:layout_height = "wrap_content"
android:layout_weight="50"
android:layout_gravity="center"
/>
</LinearLayout>
</LinearLayout>
Java Code:
public class updateList extends Activity {
private SelectedAdapter selectedAdapter;
private ArrayList<String> list;
int correct_answer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = new ArrayList<String>();
list.add("Choice One");
list.add("Choice Two");
list.add("Choice Three");
selectedAdapter = new SelectedAdapter(this,0,list);
selectedAdapter.setNotifyOnChange(true);
ListView listview = (ListView) findViewById(R.id.listExample);
listview.setAdapter(selectedAdapter);
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
// user clicked a list item, make it "selected"
selectedAdapter.setSelectedPosition(position);
}
});
}
}
Thanks in advance
SSP
Selected Adaptor class:
public class SelectedAdapter extends ArrayAdapter{
// used to keep selected position in ListView
private int selectedPos = -1; // init value for not-selected
public SelectedAdapter(Context context, int textViewResourceId,
List objects) {
super(context, textViewResourceId, objects);
}
public void setSelectedPosition(int pos){
selectedPos = pos;
// inform the view of this change
notifyDataSetChanged();
}
public int getSelectedPosition(){
return selectedPos;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
// only inflate the view if it's null
if (v == null) {
LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.activity_main, null);
}
// get text view
TextView label = (TextView)v.findViewById(R.id.txtExample);
// change the row color based on selected state
if(selectedPos == position){
label.setBackgroundColor(Color.CYAN);
}else{
label.setBackgroundColor(Color.WHITE);
}
label.setText(this.getItem(position).toString());
/*
// to use something other than .toString()
MyClass myobj = (MyClass)this.getItem(position);
label.setText(myobj.myReturnsString());
*/
return(v);
}
}
change your listview in xml as like this
<ListView
android:id="#+id/listExample"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"//===== set maximum heighthere
android:layout_marginBottom="50dp"// === give some space at bottom so that buttons will appear
android:background="#CCCCCC"
android:choiceMode="singleChoice"
/>
But for some reason, after every list item, "next" button also shows up.
The ListView's row layout is determined by the layout you inflate in getView() or pass to your Adapter's super class if you haven't overridden getView(). Double check this layout and remove the unwanted code.
Addition
The layout for your ListView's items only needs to be one TextView since you only want to display a phrase in each. However you are currently passing your entire main layout, this creates the Buttons, an unused ListView, and everthing else in every row...
Instead use android.R.layout.simple_list_item_1 in getView(), of course you'll need to change the id you pass to findViewById() as well:
if (v == null) {
LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(android.R.layout.simple_list_item_1, null);
}
// get text view
TextView label = (TextView)v.findViewById(android.R.id.text1);
Please watch Android's Romain Guy discuss writing an efficient adapter to speed things up.