I have a listview and inside it is a texview. Sometimes only the first character is displayed. And when I refresh the view, the whole word appears. Has anyone experience this? How do I fix this bug? Thanks in advance
Here is my code:
ListView tasksList = (ListView) findViewById(R.id.tasks_list);
tasksListAdapter = new SimpleAdapter(this, tasksListItems, R.layout.advanced_view_row,
new String[] {LIST_ADAPTER_APPLICATION_ICON, LIST_ADAPTER_KEY_NAME, LIST_ADAPTER_KEY_RECEIVED, LIST_ADAPTER_KEY_SENT},
new int[] {R.id.application_icon, R.id.process_name, R.id.received, R.id.sent}
)
{
#Override
#SuppressWarnings("unchecked")
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = LayoutInflater.from(parent.getContext()).
inflate(R.layout.advanced_view_row, parent, false);
}
HashMap<String, Object> item = (HashMap<String, Object>)this.getItem(position);
((ImageView) convertView.findViewById(R.id.application_icon)).setImageDrawable((Drawable) item.get(LIST_ADAPTER_APPLICATION_ICON));
((TextView) convertView.findViewById(R.id.process_name)).setText((String) item.get(LIST_ADAPTER_KEY_NAME));
((TextView) convertView.findViewById(R.id.received)).setText((String) item.get(LIST_ADAPTER_KEY_RECEIVED));
((TextView) convertView.findViewById(R.id.sent)).setText((String) item.get(LIST_ADAPTER_KEY_SENT));
return convertView;
};
};
tasksList.setAdapter(tasksListAdapter);
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="5dip"
android:paddingBottom="5dip"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TableRow>
<TextView
android:id="#+id/status"
android:layout_height="fill_parent"
android:layout_width="0dip"
android:layout_weight="0.01"/>
<ImageView
android:id="#+id/application_icon"
android:layout_width="32dip"
android:layout_height="32dip"
android:layout_weight="0.09"/>
<TextView
android:id="#+id/process_name"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="0.4"/>
<TextView
android:id="#+id/received"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="0.25"
android:gravity="right"/>
<TextView
android:id="#+id/sent"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="0.25"
android:gravity="right"/>
</TableRow>
</TableLayout>
Change layout_width of TextView with id=application_icon to 0dip (instead of 32dip).
In which TextView you have only one character displayed?
Related
I am using a LayoutInflater to fill out a grid view using a GridAdapter, but when i try to place a textView below another textView within the layout i am inflating, it ends up being above my textView instead of below. When i change it to layout_above instead, it doesnt even appear on the screen.
Layout that i inflate:
<?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="match_parent"
android:id="#+id/menuBookModel">
<ImageView
android:id="#+id/bookImage"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="5dp"/>
<TextView
android:id="#+id/bookName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Text"
android:textSize="20sp"
android:textColor="#color/colorPrimaryDark"
android:layout_centerInParent="true"/>
<TextView
android:id="#+id/personalTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bookName"
android:text="(Personal)"
android:layout_centerHorizontal="true"
android:textColor="#color/colorPrimaryDark"
android:textSize="15sp" />
</RelativeLayout>
My java code where i am inflating:
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(view == null){
view = inflater.inflate(R.layout.menu_book_model, null);
}
TextView bookName = view.findViewById(R.id.bookName);
TextView personalTag = view.findViewById(R.id.personalTag);
ImageView bookImage = view.findViewById(R.id.bookImage);
bookImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String s = String.valueOf(view.getTag());
aListener.chooseBook(s);
}
});
int id = context.getResources().getIdentifier(books[i], "string", context.getPackageName());
//if id == 0 the book is custom, and therefore it has the custom name
if(id == 0){
bookName.setText(books[i]);
} else {
bookName.setText(context.getString(id));
personalTag.setVisibility(View.GONE);
}
bookImage.setBackgroundColor(Color.rgb(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
bookImage.setTag(books[i]);
return view;
}
How it looks in the preview:
Preview
But this is how it look in practice:
In practice
You also need to handle personalTag visibility when the id is 0, because it may not be visible when the view is being recycled:
if(id == 0){
bookName.setText(books[i]);
personalTag.setVisibility(View.VISIBLE);
} else {
bookName.setText(context.getString(id));
personalTag.setVisibility(View.GONE);
}
Also avoid setting your root RelativeLayout with match_parent as height, they usually break in AdapterViews, try wrap_content or any hard values, or adjust your layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/menuBookModel">
<ImageView
android:id="#+id/bookImage"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="5dp"/>
<TextView
android:id="#+id/bookName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Text"
android:layout_centerInParent="true"
android:textSize="20sp"
android:textColor="#color/colorPrimaryDark"/>
<TextView
android:id="#+id/personalTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bookName"
android:layout_centerHorizontal="true"
android:text="(Personal)"
android:textColor="#color/colorPrimaryDark"
android:textSize="15sp" />
</RelativeLayout>
I'm currently struggling with a function I want to have in my application. The users Incomes are shown in a ListView. There are two types of incomes, normal "income" and "other income", this is selected in the application when the user submits the income to a SQLite Database that he or she receives. Now - I want to present the different incomes in a ListView with an image in the ListView relative to the type of category (two types of income category - "income" and "other income".
I cant figure out how I should do this ... can you do a switch-statement of some sort!?
My XML for the Custom ListView looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100">
<ImageView
android:orientation="vertical"
android:layout_width="48dp"
android:layout_height="48dp"
android:gravity="center_vertical"
android:id="#+id/icon1"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="220dp"
android:layout_height="48dp"
android:layout_weight="33.3">
<TextView
android:text="TextView1"
android:textSize="18dp"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:id="#+id/text1"/>
<TextView
android:text="Textview2"
android:textSize="14dp"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:="#id/text2"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="48dp"
android:orientation="vertical">
<TextView
android:text="Textview3"
android:textSize="16dp"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_gravity=""
android:gravity="center_vertical"
android:id="#+id/text3"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="48dp"
android:layout_height="48dp">
<ImageView
android:orientation="vertical"
android:layout_width="48dp"
android:layout_height="48dp"
android:gravity="center_vertical"
android:id="#+id/icon2"/>
</LinearLayout>
</LinearLayout>
And my AdapterClass looks like this:
public class CustomIncomeListAdapter extends ArrayAdapter<IncomeModel> {
private ArrayList<IncomeModel> incomeList;
private Activity context;
private ViewHolder viewHolder;
int[] incomeIcon = {R.drawable.income, R.drawable.otherincome};
int[] forwardIcon = {R.drawable.forward};
public CustomIncomeListAdapter(Activity context, int[] incomeIcon, ArrayList<IncomeModel> incomeList) {
super(context, R.layout.adapter_view_layout, incomeList);
this.incomeList = incomeList;
this.context = context;
this.incomeIcon = incomeIcon;
}
private static class ViewHolder {
TextView incomeReference;
TextView incomeDate;
TextView incomeAmount;
ImageView incomeIcon;
ImageView forwardIcon;
}
public View getView(int position, View convertView, ViewGroup parent) {
IncomeModel incomeModel = getItem(position);
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.adapter_view_layout, null, true);
viewHolder.incomeReference = (TextView) convertView.findViewById(R.id.text1);
viewHolder.incomeDate = (TextView) convertView.findViewById(R.id.text2);
viewHolder.incomeAmount = (TextView) convertView.findViewById(R.id.text3);
viewHolder.incomeIcon = (ImageView) convertView.findViewById(R.id.icon1);
viewHolder.forwardIcon = (ImageView) convertView.findViewById(R.id.icon2);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.incomeReference.setText(incomeModel.getIncomeCategory());
viewHolder.incomeDate.setText(incomeModel.getIncomeDate());
viewHolder.incomeAmount.setText(incomeModel.getIncomeAmount());
switch (...?) {
}
}
viewHolder.incomeAmount.setText(incomeModel.getIncomeAmount());
switch (income) {
//define your income to 1 for other income
case 1:
incomeIcon.setBackgroundResource(R.drawable.otherincome);
break;
default:
incomeIcon.setBackgroundResource(R.drawable.income);
break;
}
you can check type , if type is income means set corresponding image else set other image
Im programming a app which shows meals with a picture,name and a short description. If pressed on one meal it should extend the size of the listitem to
get from this http://puu.sh/lmwKn/b5f6f1d54a.jpg to this http://puu.sh/lmwJo/943f7349d3.jpg.
Now to add meals to the ListView i wrote an MealAdapter, i dont know how to get the view of the list of the comments
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.gericht_list_layout, null);
viewHolder = new ViewHolder();
viewHolder.img = (ImageView) convertView.findViewById(R.id.gericht_image);
viewHolder.name = (TextView) convertView.findViewById(R.id.gericht_title);
viewHolder.descTxt = (TextView) convertView.findViewById(R.id.gericht_sub_title);
viewHolder.comments = (ListView) convertView.findViewById(R.id.gericht_comments);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.name.setText(data.get(position).name);
viewHolder.descTxt.setText(data.get(position).subName);
int id = convertView.getResources().getIdentifier(
data.get(position).img, "drawable",
context.getPackageName());
viewHolder.img.setImageResource(id);
return convertView;
}
Here is the 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="match_parent">
<ImageView
android:id="#+id/gericht_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:src="#drawable/no_image"
/>
<TextView
android:id="#+id/gericht_title"
android:layout_width="fill_parent"
android:layout_height="75dp"
android:layout_toRightOf="#+id/gericht_image"
android:text="Kein Name"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:textColor="#000000"
android:textSize="25sp"
/>
<TextView
android:id="#+id/gericht_sub_title"
android:layout_width="fill_parent"
android:layout_height="25dp"
android:text="Keine genauere Beschreibung"
android:layout_toRightOf="#+id/gericht_image"
android:layout_marginTop="55dp"
android:layout_marginLeft="30dp"
android:gravity="center_vertical"
/>
<ListView
android:id="#+id/gericht_comments"
android:layout_width="fill_parent"
android:layout_marginTop="100dp"
android:layout_marginLeft="50dp"
android:layout_height="0dp"
/>
</RelativeLayout>
So i want to draw the gericht_comments listview but i dont know how
I currently have an expandable list view that is populating just fine. However when the child view is filling it creates individuals views and rows for each entry. I would like to have one view and multiple textviews in the view as well as a button that I will be adding. The data is being populated from a HASHMAP. I have tried many things and cannot get multiple textviews to populate correctly in the listview.
All data comes from this hashmap:
private HashMap<String, List<String>> _deviceDetails;
This is the get child view method of my custom adapter.
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
This is an image of what I have now with what I wish to do.
I need the data highlighted inside of one row so that I can add buttons etc without a bunch of rows. I genuinely cannot figure out how to do this properly.
I'm an idiot. I figured it out. Thank you #Krupal you pointed me in the right direction. If you place anything as an answer i'll select you because you got me thinking the right way.
So what I ended up doing here was I created a custom List Adapter. I won't add the entire class to keep it short. The key was to use set the adapter to only return one child. Then in the child that was returned (XML below) I had all of my needed fields added in. So now when I expanded the list only one child would be visible. Also in my getChildView() I defined all needed entries for the layout for the children. Don't know if this is the perfect way to do it but worked nicely for my needs.
This is where I actually configured the child with my data.
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, 0);
final String childTextSecond = (String) getChild(groupPosition, 1);
final String childTextThird = (String) getChild(groupPosition, 2);
final String deviceName = (String) getChild(groupPosition, 3);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
TextView txtListChildTwo = (TextView) convertView
.findViewById(R.id.lblListItemTwo);
TextView txtListChildThree = (TextView) convertView
.findViewById(R.id.lblListItemThree);
//rest of logic!
}
Setting the children count to 1
#Override
public int getChildrenCount(int groupPosition) {
return 1;
}
XML Layout for the child elements
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<RelativeLayout
android:layout_width="340dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|right"
android:background="#f9fcff">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:background="#4d86ff">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#android:style/TextAppearance.Medium"
android:layout_marginLeft="10dp"
android:text="Large Text"
android:id="#+id/lblListItem"
android:textColor="#ffffff"
android:layout_alignTop="#+id/textView5"
android:layout_toRightOf="#+id/textView5"
android:layout_toEndOf="#+id/textView5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textAppearance="#android:style/TextAppearance.Medium"
android:text="Device ID: "
android:id="#+id/textView5"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#ffffff" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Tags"
android:id="#+id/btnTags"
android:background="#ffffff"
android:singleLine="true"
android:layout_alignTop="#+id/lblListItem"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textAppearance="#android:style/TextAppearance.Medium"
android:text="Status: "
android:id="#+id/textView6"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="#android:style/TextAppearance.Medium"
android:text="Large Text"
android:id="#+id/lblListItemTwo"
android:textColor="#ffffff"
android:layout_alignTop="#+id/textView6"
android:layout_toRightOf="#+id/textView6"
android:layout_toEndOf="#+id/textView6" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginBottom="10dp"
android:textColor="#ffffff"
android:textAppearance="#android:style/TextAppearance.Medium"
android:text="Description: "
android:id="#+id/textView7"
android:layout_marginStart="15dp"
android:layout_alignTop="#+id/lblListItemThree"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="6dp"
android:layout_marginRight="13dp"
android:layout_marginBottom="10dp"
android:textAppearance="#android:style/TextAppearance.Medium"
android:text="Large Text"
android:id="#+id/lblListItemThree"
android:textColor="#ffffff"
android:layout_marginStart="39dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/textView7"
android:layout_toEndOf="#+id/textView7" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
i am a beginer android-developer and i need some help:
There is a Fragment in my Project - TasksFragment, there is a ListView element on it. On ListView, using ArrayAdapter Items are formed by a custom layout for Item and then added to ListView. The problem is, that ListView don't react on click processing events.
Here are the code fragments:
TaskAdapter class...
public class TaskAdapter extends ArrayAdapter<Task> {
public TaskAdapter(ArrayList<Task> tasks) {
super(getActivity(), 0, tasks);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_task, null);
}
Task task = getItem(position);
TextView titleTextView = (TextView) convertView.findViewById(R.id.task_list_item_titleTextView);
titleTextView.setText(task.getTitle());
CheckBox solvedCheckBox = (CheckBox) convertView.findViewById(R.id.task_list_item_solvedCheckBox);
solvedCheckBox.setChecked(task.isSolved());
return convertView;
}
}
ItemClick fragment.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tasks_fragment, null);
mTasks = TaskLab.get(getActivity()).getTasks();
final ListView lvMain = (ListView) view.findViewById(R.id.listViewTasks);
lvMain.setClickable(true);
TaskAdapter adapter = new TaskAdapter(mTasks);
setHasOptionsMenu(true);
lvMain.setAdapter(adapter);
lvMain.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Task task = (Task)lvMain.getItemAtPosition(position);
Toast.makeText(getActivity().getApplicationContext(), task.getTitle() + " was clicked", Toast.LENGTH_LONG).show();
Intent i = new Intent(getActivity(), AddTaskActivity.class);
startActivity(i);
}
});
return view;
}
XML Layout for List_Item_Task
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="#+id/task_list_item_solvedCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentRight="true"
android:enabled="false"
android:padding="4dp"/>
<TextView
android:id="#+id/task_list_item_titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/task_list_item_solvedCheckBox"
android:textStyle="bold"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textSize="22dp"/>
</RelativeLayout>
Xml layout for tasks_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3f638b">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textViewTasksFragmentHeader"
android:text="Задачи"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_centerHorizontal="true"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listViewTasks" />
</LinearLayout>
Yes your listview is not clickable.
Just add this in your checkbox layout
android:focusable="false"
Your items xml should look like this
<?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" >
<CheckBox
android:id="#+id/task_list_item_solvedCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" //Add this
android:layout_alignParentRight="true"
android:gravity="center"
android:padding="4dp" />
<TextView
android:id="#+id/task_list_item_titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/task_list_item_solvedCheckBox"
android:paddingBottom="5dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="5dp"
android:textSize="22dp"
android:textStyle="bold" />
Now it should work. I have tested it :)