Android custom List SimpleAdapter - chose row icon - java

Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#android:color/black">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/icon"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="#+id/line_a"
android:textSize="14dp"
android:textColor="#android:color/white"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"/>
<TextView android:id="#+id/line_b"
android:textSize="10dp"
android:textColor="#android:color/darker_gray"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
Java:
SimpleAdapter sa;
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> item;
for(int i = 0; i < j; i++){
item = new HashMap<String,String>();
item.put( "line1", ListData[i][0]);
item.put( "line2", ListData[i][1]);
item.put( "type", ListData[i][2]);
list.add( item );
}
sa = new SimpleAdapter(this, list,
R.layout.two_lines_list,
new String[] { "line1","line2" },
new int[] {R.id.line_a, R.id.line_b});
// can you add the setImageView here? If yes, how?
setListAdapter( sa );
}
How can I make the icon to get an image from res/drawable... depending on the type (ListData[i][2] inserted in the HashMap)? eg. if the type is "1" set icon1.png at the beginning of the 2-line-row

You should override SimpleAdapter.getView(int position, View convertView, ViewGroup parent) method, and get the list item with the current position(the first param in that method), then check the value set set the icon you want.
NOTE: you' d better use ViewHolder pattern in the getView method for performance.

Related

SQLite in android, how to iterate through entire database column and apply its values to recycler view

I have a big SQLite database (7000+ items) with 16 columns per row. I would like to populate my custom recycler view, that I already made, with items from the database to make a huge recycler view list.
My question is this: How would you go about iterating through 2 entire columns of all of the rows. I need to fetch a name and an ID of each and every single item in the database and assign those values to 2 text views, that I created through a custom recycler view adapter. I already have a method in the database that returns a number of total rows (aka items) in a database. I would like to make a loop with i < getDbSize(); but I don't have a clear idea on how I would go about on incrementing through 2 particular columns of all rows and then applying that information onto a card in the recycler view.
Here is the xml code where recycler view sits:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".CollegeFragment"
android:background="#color/colorPrimaryDark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="College Search Tool"
android:textSize="28sp"
android:padding="10dp"
android:textColor="#color/colorWhite"
android:fontFamily="sans-serif-medium"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/grad_bg"
android:paddingBottom="10dp"
android:paddingRight="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ipeds id:"
android:textSize="24sp"
android:padding="10dp"
android:textColor="#color/colorWhite"
android:fontFamily="sans-serif-medium"/>
<EditText
android:id="#+id/et_ipeds"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textColor="#color/colorWhite"
android:fontFamily="sans-serif-medium"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:background="#drawable/et_alt1_bg"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name: "
android:textSize="24sp"
android:padding="10dp"
android:textColor="#color/colorWhite"
android:fontFamily="sans-serif-medium"/>
<EditText
android:id="#+id/et_name"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textColor="#color/colorWhite"
android:fontFamily="sans-serif-medium"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:background="#drawable/et_alt1_bg"/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESULT"
android:textSize="28sp"
android:padding="10dp"
android:textColor="#color/colorWhite"
android:fontFamily="sans-serif-medium"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"/>
</LinearLayout>
</FrameLayout>
and Java:
public class CollegeFragment extends Fragment implements ItemClickListener{
private RecyclerView recyclerView;
private CustomRecyclerAdapter customRecyclerAdapter;
private List<CollegeItem> collegeItemList = new ArrayList<>();
private DatabaseCollege dbCollege;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View myFragmentView = inflater.inflate(R.layout.fragment_college, container, false);
dbCollege = new DatabaseCollege(getActivity());
recyclerView = myFragmentView.findViewById(R.id.recycler);
customRecyclerAdapter = new CustomRecyclerAdapter(collegeItemList, getActivity());
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(customRecyclerAdapter);
customRecyclerAdapter.setItemClickListener(this);
//populate collegeItemList with dummy values for testing
String name = "College 1";
String id = "12345647653";
String name2 = "College 2";
String id2 = "12345";
CollegeItem college = new CollegeItem(name, id);
CollegeItem college2 = new CollegeItem(name2, id2);
collegeItemList.add(college);
collegeItemList.add(college2);
return myFragmentView;
}
#Override
public void onClick(View v, int position) {
String name = collegeItemList.get(position).getName();
String id = collegeItemList.get(position).getId();
}
public void populateList() {
int numOfColleges = dbCollege.getNumOfColleges();
for(int i = 0; i < numOfColleges; i++) {
//read in data from database into collegeItemList recyclerView here
}
}
}
I also included a picture with what I am trying to achieve that contains manually inserted variables. I basically want to populate that list with cards that each contain info(ID and name) about each and every item from database.
You need to query the database first to get those two fields like below:
ArrayList<CollegeItem> arr = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
while (cursor.isAfterLast() == false)
{
arr.add(new CollegeItem(cursor.getString(1),
cursor.getString(2));
cursor.moveToNext();
}
cursor.close();
db.close();
return arr;
Then pass this ArrayList(arr) to the list view using Adaptor and map the values of Pair class to the XML components.

How to set parent background on ArrayAdapter

I am building an app and have some difficulties on setting backround on a array adapter, hope someone can help!
public class ListAdapter extends ArrayAdapter {
// List context
private final Context context;
// List values
private final List<RssItem> items;
public ListAdapter(Context context, List<RssItem> items) {
// Set the layout for each item
super(context, R.layout.item, items);
this.context = context;
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Build each element of the list
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.item, parent, false);
// Populate each layout element with the data from the items list
TextView itemPos = (TextView) rowView.findViewById(R.id.position_aa);
itemPos.setText(items.get(position).getPosition());
// Populate each layout element with the data from the items list
TextView itemTitle = (TextView) rowView.findViewById(R.id.textViewTitle);
itemTitle.setText(items.get(position).getTitle());
TextView itemPublishDate = (TextView) rowView.findViewById(R.id.textViewPublishDate);
itemPublishDate.setText(items.get(position).getPublishDate());
// Set the icon base on the post's category
ImageView icon = (ImageView) rowView.findViewById(R.id.imageViewCategoryIcon);
icon.setImageResource(CategoryMapper.getIconIdForCategory(items.get(position).getCategory()));
return rowView;
}
}
And this is the item.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:id="#+id/bg">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#0FFF3300"
android:id="#+id/item">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageViewCategoryIcon"
android:layout_width="100dp"
android:layout_height="100dp"
android:padding="5dp"
android:src="#drawable/ic_launcher" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="1dp">
<TextView
android:id="#+id/textViewTitle"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="[TITLE-GOES-HERE]"
android:textColor="#000"
android:textStyle="bold"/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textViewPublishDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:textColor="#555"
android:text="[PUB-DATE-GOES-HERE]"
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/position_aa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:textColor="#555"
android:text="[AA-GOES-HERE]"
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
Here I deploy some rss data into RowViews Via the list adapter class, but how can I set a parent layout so I can give a fixed upon scroll background?
Thanks in advance
P.S I`m stuck for a day at this......
Set background color to your ListItem like
rowView.setBackgroundResource(R.color.yourcolor);

Can't Get TableLayout to Programatically Be Created

I've created what I want in xml but I need to do it programatically and I can't get it to work. It's a table layout with one row. In that row there is two more table layouts. The first of which has one row containing a picture and the second table layout has two rows containing two textviews and a picture. Is there some kind of xml to java converter? I've tried to write the code now for a couple of hours and can't get the layouts to work. I'd post a picture but I need 10 rep points first. The whole xml file is in a linear layout if it helps.
<TableLayout
android:id="#+id/tablemain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:gravity="center_horizontal" >
<TableRow
android:id="#+id/TableRow1"
android:background="#drawable/border"
android:weightSum="100" >
<TableLayout
android:id="#+id/Tableout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0"
android:background="#drawable/border"
android:gravity="center_horizontal" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:adjustViewBounds="true"
android:maxHeight="100dp"
android:maxWidth="100dp"
android:padding="3dp"
android:src="#drawable/unknown" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/Tableout3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="100"
android:gravity="center_horizontal"
android:weightSum="100" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:background="#drawable/border" >
<TextView
android:id="#+id/Judgename"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="7dp"
android:text="Name"
android:textSize="20sp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/tablerowctroom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:background="#drawable/border"
android:weightSum="100" >
<TextView
android:id="#+id/Courtroom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="70"
android:padding="7dp"
android:text="Room"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp"
android:layout_weight="30"
android:src="#drawable/umid" />
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>
This is the new code I'm trying to use based on Nathan Walter's advice. However, It'll set the first set text but the rest are just the default value. Although the layout looks right. How can I set the textfields for each individual view when the method is called.
public void Display(String name, String ctroom) {
ViewGroup parent = (ViewGroup) findViewById(R.id.container);
view = LayoutInflater.from(this).inflate(R.layout.tablebutton, null);
parent.addView(view);
TextView jn = (TextView) findViewById(R.id.JudgenameCourts);
jn.setText(name);
TextView ct = (TextView) findViewById(R.id.Courtroomnew);
ct.setText(ctroom);
view = LayoutInflater.from(this).inflate(R.layout.tablebutton, null);
parent.addView(view);
jn.setText("Name1");
ct.setText("Courtroom1");
}
I think the code you want looks something like this. Let me know if it makes sense.
// Create an array of things to create views from.
// You will probably be getting your array from somewhere else
YourObjectHere[] things = new YourObjectHere[2];
things[0] = new YourObjectHere();
things[1] = new YourObjectHere();
// Get a reference to a LinearLayout to hold your views
LinearLayout list = (LinearLayout) findViewById(R.id.list);
// Loop through your list of objects
for(int i = 0; i < things.length; i++) {
// Inflate an instance of your layout
View listItem = getLayoutInflater().inflate(R.layout.child, null);
// Set the TextViews to the appropriate things
((TextView) listItem.findViewById(R.id.name)).setText(things[i].getName());
((TextView) listItem.findViewById(R.id.type)).setText(things[i].getType());
// Add the inflated view to the list
item.addView(child);
}

Setting background while using Custom ListView

I am working on a android application project in which I am using a customer simple adapter to repeat a layout called row.xml. Everything is going good except for the fact that I have a background color/image that I am using for my app and when I use this simple adapter method I get white space for the area that is not filled by the list.
Here is the code from my Java class
public class CustomerActivity extends ListActivity {
final static ArrayList<HashMap<String, ?>> arrayListData = new ArrayList<HashMap<String, ?>>();
ListView mCustomerList;
static {
HashMap<String, Object> CurrentRow = new HashMap<String, Object>();
CurrentRow.put("Icon", R.drawable.has_360_inspection_icon);
CurrentRow.put("Icon2", R.drawable.has_solid_fuel_inspection_icon);
CurrentRow.put("Name", "Xue Test");
arrayListData.add(CurrentRow);
CurrentRow = new HashMap<String, Object>();
CurrentRow.put("Icon", R.drawable.has_360_inspection_icon);
CurrentRow.put("Icon2", R.drawable.no_solid_fuel_inspection_icon);
CurrentRow.put("Name", "Ryan Test");
arrayListData.add(CurrentRow);
CurrentRow = new HashMap<String, Object>();
CurrentRow.put("Icon", R.drawable.no_360_inspection_icon);
CurrentRow.put("Icon2", R.drawable.has_solid_fuel_inspection_icon);
CurrentRow.put("Name", "Becca Test");
arrayListData.add(CurrentRow);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SimpleAdapter adapter = new SimpleAdapter(this, arrayListData, R.layout.row, new String[] { "Icon", "Icon2", "Name" },
new int[] { R.id.imageView1, R.id.imageView2, R.id.customer_name_textView });
setListAdapter(adapter);
}
and here is the code from row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/lightBlue"
android:weightSum="6" >
<TextView
android:id="#+id/customer_name_textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="TextView" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/has_360_inspection_icon" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/has_solid_fuel_inspection_icon" />
<View
android:id="#+id/view1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="2" />
</LinearLayout>
Thank you for the help and if you need any more information just let me know.
If you just want the entire list to have a light blue background, set it on the listview itself instead of on each row. That way if there's empty space in the list it will still be blue.
Have you use wrap_content or match_parent for Listview? If match_parent then replace to wrap_content

Android ListView Background repeats background after each entry

I figured it out I was using the same layout for my on create and to show the data so it was repeating everyting.
I have a list of data showing and for some reason the background I have set looks fine but after each two text views the background is squished into a 20px window overlaying the orginal background.
My xml for my view looks like so:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:background="#drawable/patriot_bg2"
android:cacheColorHint="#00000000"
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:drawSelectorOnTop="true"
/>
<TextView
android:id="#+id/item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:background="#00000000"
android:padding="2dp"
android:textSize="20dp" />
<TextView
android:id="#+id/item_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:cacheColorHint="#00000000"
android:padding="2dp"
android:textSize="13dp" />
</LinearLayout>
Below is the Java that places the text into the text fields and adds it to the screen:
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("main_content", XMLfunctions.getValue(e, "content"));
map.put("name", XMLfunctions.getValue(e, "name"));
mylist.add(map);
}
//
ListAdapter adapter = new SimpleAdapter(ShowXMLPAR.this, mylist , R.layout.listplaceholder,
new String[] {"main_content", "name" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
its almost as if there is another listview after each set of textfields but I am not sure how to get rid of it. Please help.
I had to use a seperate layout for the ListView items so only that would repeat and not the main layout.

Categories

Resources