Android getView method do not read textView - android

I have ListAdapter there is getView(); method. This is a code for method:
#Override
public View getView(int idx, View view, ViewGroup parent)
{
if(view == null)
{
LayoutInflater vi = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.check, null);
}
return view;
}
And this is check.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
<TextView
android:id="#+id/checkedTextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/checkBox1"
android:layout_alignBottom="#+id/checkBox1"
android:layout_toRightOf="#+id/checkBox1"
android:textColor="#615742"
android:textSize="18sp" />
</RelativeLayout>
The problem is that it reads only checkbox. And it igonres my TextView. If I write in xml file only checkedtextview then text appears but then I can not implement checkbox. What is a problem in my code that it ignores textView and it shows only checkbox?

#Override
public View getView(int idx, View view, ViewGroup parent)
{
if(view == null)
{
LayoutInflater vi = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.check, null);
}
TextView tv = (TextView) view.findViewById(R.id.checkedTextView1);
tv.setText("XXXXXXXX");
return view;
}

Related

How to use butterknife in my case?

In my code, how should i change if i want use butterknife?
Layout list_item is listview, so I can generate butterknife injection, but I don't know how to fix other code, should I use ViewHolder?
public class GangAdapter extends ArrayAdapter<Gang> {
public GangAdapter(Context context, ArrayList<Gang> gangs) {
super(context, 0, gangs);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
Gang currentFeature = getItem(position);
ImageView spotImageView = convertView.findViewById(R.id.Image);
spotImageView.setImageResource(currentFeature.getImageResourceId());
TextView featureTextView = convertView.findViewById(R.id.where);
featureTextView.setText(currentFeature.getFeature());
TextView detailTextView = convertView.findViewById(R.id.about);
detailTextView.setText(currentFeature.getExplanation());
return convertView;
}
Here is my list_item.xml.
I think there is no problem. isn't it?
<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">
<TextView
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:id="#+id/where"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="24sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="center"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/about"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left" />
You can setup the annotations and the pass the inflated view to while binding as
public class GangAdapter extends ArrayAdapter<Gang> {
#BindView(R.id.Image) ImageView spotImageView;
#BindView(R.id.where) TextView featureTextView ;
#BindView(R.id.about) TextView detailTextView ;
//^^^^^^^^^^^ do the setup
public GangAdapter(Context context, ArrayList<Gang> gangs) {
super(context, 0, gangs);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
ButterKnife.bind(this, convertView);
// ^^^^^^^^^^^
// pass the view with which you want to bind the views
}
Gang currentFeature = getItem(position);
spotImageView.setImageResource(currentFeature.getImageResourceId());
featureTextView.setText(currentFeature.getFeature());
detailTextView.setText(currentFeature.getExplanation());
return convertView;
}
}

Interactive with TextView in each row of ListView

I have an list View with two Item in each row is two TextView.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="vertical">
<TextView
android:id="#+id/txtInfo"
android:layout_width="wrap_content"
android:layout_height="30sp"
android:layout_gravity="right"
android:textSize="12sp"
android:visibility="gone"
android:textColor="#android:color/darker_gray" />
<LinearLayout
android:id="#+id/contentWithBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#drawable/my_textview_border"
android:paddingLeft="10dp"
android:paddingBottom="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/txtMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:padding="10dp"
android:maxWidth="240dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Now I want to show the hidden TextView just on the row I click with onItemClick(AdapterView<?> parent, View view, int position, long id) method but how can I set an detail id for each TextView. I mean the TextView just VISIBLE in the row I touch, not all of that TextView in other row.
Extend your class Activity by implements OnItemClickListener
use yourList.setOnItemClickListener(this); in onCreate()
Override onItemClick using this way:
s
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView tv = (TextView) view.findViewById(R.id.txtMessage);
tv.setVisibility(View.VISIBLE);
}
do not forget to override your getView() arrayAdapter method:
a
public View getView(int position, View convertView, ViewGroup list) {
View element;
if (convertView == null) {
element = View.inflate(ctx, R.layout.yourlayout, null);
} else {
element = convertView;
}
///setup your listview element
return element;
}
EDIT
to change all data for textview I will work in such way:
yourarrayadater.setupmydata();
yourarrayadater.notifyDataSetChanged()
after that arrayadapter will redraw using getView.
So, use again
public View getView(int position, View convertView, ViewGroup list) {
View element;
if (convertView == null) {
element = View.inflate(ctx, R.layout.yourlayout, null);
} else {
element = convertView;
}
TextView tv = (TextView) element.findViewById(R.id.txtMessage);
///SETUP YOUR tv for each row
return element;
}

Styling Spinner Action Bar

Hello I have problem to styling Spinner Action Bar. I have Spinner with custom adapter like this :
#Override
protected void onCreate(Bundle savedInstanceState) {
...
// Spinner list
SpinnerMenuForm = new ArrayList<SpinnerNavItem>();
SpinnerMenuForm.add(new SpinnerNavItem("02","Fill Order - HSD Bunker", "HSD"));
SpinnerMenuForm.add(new SpinnerNavItem("14","Fill Order - MFO Bunker", "MFO"));
// title drop down adapter
adapterSpinnerMenuForm = new Adapter_List_Form(getApplicationContext(), SpinnerMenuForm);
...
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate our menu from the resources by using the menu inflater.
getMenuInflater().inflate(R.menu.main, menu);
View view1= (View) MenuItemCompat.getActionView(actionbar_form);
if (view1 instanceof Spinner)
{
final Spinner spinner = (Spinner) view1;
spinner.setAdapter(adapterSpinnerMenuForm);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
TextView txtType= (TextView)spinner.getAdapter().getView(position, null, null).findViewById(R.id.txtType);
Variabel.type= txtType.getText().toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
return true;
}
and this adapter spinner
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item_form, null);
}
txtNumber = (TextView) convertView.findViewById(R.id.txtNumber);
txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
txtType = (TextView) convertView.findViewById(R.id.txtType);
txtNumber.setText(spinnerNavItem.get(position).getNumber());
txtTitle.setText(spinnerNavItem.get(position).getTitle());
txtType.setText(spinnerNavItem.get(position).getType());
txtTitle.setVisibility(View.GONE);
return convertView;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item_form, null);
}
txtNumber = (TextView) convertView.findViewById(R.id.txtNumber);
txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
txtType = (TextView) convertView.findViewById(R.id.txtType);
txtNumber.setText(spinnerNavItem.get(position).getNumber());
txtTitle.setText(spinnerNavItem.get(position).getTitle());
txtType.setText(spinnerNavItem.get(position).getType());
return convertView;
}
and this item spinner layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<TextView android:id="#+id/txtNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
/>
<TextView android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/txtNumber"/>
<TextView android:id="#+id/txtType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
</RelativeLayout>
the result
I use http://jgilfelt.github.io/android-actionbarstylegenerator/ but never change Style Spinner same as at tutorial http://blog.stylingandroid.com/styling-the-actionbar-part-4/ like this
so how to solve it ? sorry for my english
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="20dp"
android:padding="5dp"
android:background="#ff00DDED"> /////clour code thst use
<TextView android:id="#+id/txtNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
/>
<TextView android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/txtNumber"/>
<TextView android:id="#+id/txtType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>

List with buttons - only first button registers

I have implemented a ListView, each element of which contains a checkbox and a button. I have written an adapter for the view as follows:
public class myadapter extends SimpleAdapter
{
LayoutInflater mLayoutInflater;
public myadapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
{
super(context, data, resource, from, to);
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View view = null;
Log.i("xx","getView called: position="+position);
if (convertView != null)
{
view = convertView;
}
else
{
view = mLayoutInflater.inflate(R.layout.custom_row_view, parent, false);
}
Button buttonEdit = (Button) view.findViewById(R.id.editbut);
CheckBox cb = (CheckBox)view.findViewById(R.id.checkbox);
buttonEdit.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Log.i("xx","Button pressed! position ="+position);
}
});
cb.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1)
{
Log.i("xx","Checkbox changed! position ="+position);
}
});
return super.getView(position, convertView, parent);
}
}
At runtime, if I click on a button in the top row of my list I always see "Button pressed! position = 0" in the log output. Similarly, if I click on the checkbox I see "Checkbox changed! position=0". The problem comes if I click on a button on any other element in the list. Sometimes I see the correct log message, but other times I see nothing at all. Any ideas?
EDIT: here's my custom_row_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="142dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView android:id="#+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#FFFF00"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
<TextView android:id="#+id/text2"
android:textSize="12sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
<TextView android:id="#+id/text3"
android:typeface="sans"
android:textSize="14sp"
android:textStyle="italic"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right" >
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="" />
<Button
android:id="#+id/editbut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Edit" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
do like this
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View view = convertView;
Log.i("xx","getView called: position="+position);
if (view == null)
{
view = convertView;
view = mLayoutInflater.inflate(R.layout.custom_row_view, parent, false);
}
Button buttonEdit = (Button) view.findViewById(R.id.editbut);
CheckBox cb = (CheckBox)view.findViewById(R.id.checkbox);
return view;
}
you are returning the superclass value, with convertview as a parameter.
return super.getView(position, convertView, parent);
but you are doing this:
if (convertView != null)
{
view = convertView;
}
else
{
view = mLayoutInflater.inflate(R.layout.custom_row_view, parent, false);
}
If convertView is null, you are inflating a new view, but you are not returning it.
my suggestion is
return view;
instead of
return super.getView(position, convertView, parent);
That should be enough
do this:
Button buttonEdit = (Button) view.findViewById(R.id.editbut);
CheckBox cb = (CheckBox)view.findViewById(R.id.checkbox);
buttonEdit.setTag(String.valueOf(positin));
cb.setTag(String.valueOf(positin));
buttonEdit.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Log.i("xx","Button pressed! position ="+arg0.getTag());
}
});
cb.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1)
{
Log.i("xx","Checkbox changed! position ="+arg0.getTag(););
}
});
you are using OnClickListener() inner as a inner class. for inner class the value of position remains zero. until you setTag() on button and then getTag from button.

getView() method of ArrayAdapter repeats first two or three items in list

I extended an ArrayAdapter with an overridden getView() method to fill my ListView object, but the adapter will grab the first two or three objects from the ArrayList I pass to it, then just repeat those in seemingly random order for the entire length of the list. Furthermore, when I scroll up and down in the ListView, some rows change from one list item to another. The ArrayList<Credit> object gets populated from a JSONObject, and to the best of my knowledge is correct prior to being passed into the ArrayAdapter.
My custom ArrayAdapter
private class CreditArrayAdapter extends ArrayAdapter<Credit> {
private ArrayList<Credit> credits;
private int creditCount;
public CreditArrayAdapter(Context ctx, int textViewResourceId, List<Credit> items) {
super(ctx, textViewResourceId, items);
credits = (ArrayList<Credit>) items;
creditCount = credits.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item_movie_medium, null);
Credit current = credits.get(position);
Log.d(tag, "current credit: " + current.toString());
ImageView creditPicture = (ImageView) v.findViewById(R.id.image_poster_thumbnail);
try {
creditPicture.setImageBitmap(current.getPosterSmall());
} catch(Exception e) {
Log.d(tag, "failed to set cast member picture");
e.printStackTrace();
}
TextView castMemberName = (TextView) v.findViewById(R.id.text_credit_info);
castMemberName.setText(current.toString());
}
return v;
}
#Override
public int getCount() {
return creditCount;
}
}
being called by:
appearsIn = (ListView) findViewById(R.id.listview_appears_in);
List<Credit> credits = searcher.getCreditsByPersonId(personId);
CreditArrayAdapter creditAdapter = new CreditArrayAdapter(getBaseContext(), R.layout.list_item_movie_medium, credits);
creditAdapter.notifyDataSetChanged();
appearsIn.setAdapter(creditAdapter);
The layout containing the ListView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:weightSum="2">
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView android:id="#+id/image_person_info"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:contentDescription="person image"/>
<TextView android:id="#+id/text_person_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"/>
<TextView android:id="#+id/text_person_birth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"/>
<TextView android:id="#+id/text_person_death"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"/>
<TextView android:id="#+id/text_person_bio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"/>
</LinearLayout>
</ScrollView>
<ListView android:id="#+id/listview_appears_in"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
My list item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView android:id="#+id/image_poster_thumbnail"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView android:id="#+id/text_credit_info"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
change the getView like this:
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item_movie_medium, null);
}
Credit current = credits.get(position);
Log.d(tag, "current credit: " + current.toString());
ImageView creditPicture = (ImageView) v.findViewById(R.id.image_poster_thumbnail);
try {
creditPicture.setImageBitmap(current.getPosterSmall());
} catch(Exception e) {
Log.d(tag, "failed to set cast member picture");
e.printStackTrace();
}
TextView castMemberName = (TextView) v.findViewById(R.id.text_credit_info);
castMemberName.setText(current.toString());
return v;
}
Your problem was you were not doing all the code you needed to set the View object correctly when convertView was != null.
Use this snippet:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item_movie_medium, null);
}
Credit current = credits.get(position);
Log.d(tag, "current credit: " + current.toString());
ImageView creditPicture = (ImageView) v.findViewById(R.id.image_poster_thumbnail);
try {
creditPicture.setImageBitmap(current.getPosterSmall());
} catch(Exception e) {
Log.d(tag, "failed to set cast member picture");
e.printStackTrace();
}
TextView castMemberName = (TextView) v.findViewById(R.id.text_credit_info);
castMemberName.setText(current.toString());
return v;
}
try adjust your xml layout on anyone place that references the height, because the android will do a measure to draw eachtime and will redraw the cell up again.
Try use on listview match_parent and on cell at row an exactly height.
sorry my bad english.

Categories

Resources