code adds one item to listview and then stops - android

I have the following code that is a dynamic listview where you type in the textbox and click the add button and its added to the listview below. Listview is a custom listview with 2 textviews. Somehow the code adds the first item and then does not add the rest. The arraylist gets the item, i call adapter.notifyDataSetChanged(); and yet still the listview does not get updated. What am i doing wrong?
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class ManageComplaintsActivity extends DashboardActivity {
EditText txtAddComplaint;
ListView lvComplaintsList;
Button btnAddComplaint;
private SimpleAdapter adapter;
private int count = 1;
private ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage_complaints);
setTitleFromActivityLabel (R.id.title_text);
txtAddComplaint= (EditText) findViewById(R.id.txtAddComplaint);
lvComplaintsList= (ListView) findViewById(R.id.lvComplaintsList);
btnAddComplaint = (Button)findViewById(R.id.btnAddComplaint);
String[] from = { "complaint", "complaintid" };
int[] to = { R.id.lblC, R.id.lblCID };
adapter = new SimpleAdapter (this, list, R.layout.activity_manage_complaints_row, from, to);
lvComplaintsList.setAdapter(adapter);
btnAddComplaint.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("complaint", txtAddComplaint.getText().toString());
item.put("complaintid", Integer.toString(count));
list.add(item);
adapter.notifyDataSetChanged();
count++;
txtAddComplaint.setText("");
Toast.makeText(getApplicationContext(),Integer.toString(list.size()), Toast.LENGTH_SHORT).show();
}
});
lvComplaintsList.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView)view.findViewById(R.id.lblC)).getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
This is the 2 xml files
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dip"
android:shrinkColumns="1"
android:stretchColumns="1" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/txtAddComplaint"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3" />
<Button
android:id="#+id/btnAddComplaint"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/txtComplaintLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List of Complaints : "
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingBottom="20dp"/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/lvComplaintsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_span="2" />
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
This is the listviewrow xml
<?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" >
<TextView
android:id="#+id/lblC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
<TextView
android:id="#+id/lblCID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
/>
</RelativeLayout>

As you can see in documentation to SimppleAdapter it is
An easy adapter to map static data to views defined in an XML file.
I believe that you can find any tricky way to add data dynamically. But I'm not sure that you have to do it like this. Try to use ArrayAdapter instead. By default it binds T.toString() values to single TextView, but you can override getView(int, View, ViewGroup) to return the type of view you want.

I changed the main xml file as follows
<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:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/txtAddComplaint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</EditText>
<Button
android:id="#+id/btnAddComplaint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add to Listview" >
</Button>
</LinearLayout>
<ListView
android:id="#+id/lvComplaintsList"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
I had used a table layout and the listview was in one of the rows.
Hope this helps someone .

Related

ScrollView not working with GridLayout in android studio

Hi I want to create a responsive scrollable grids. As I search, I came across some codes. I tweaked this code to allow scrolling of the grids. The code creates a 2 column grids and 4 rows with cardViews. The cardViews shrinks to allow more cards into the viewport rather than scrolling. Am open to any ideas and alternative methods to make this work.Thanks in advance.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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"
tools:context=".MainActivity"
android:scrollbars="vertical"
android:fillViewport="true">
<LinearLayout
android:orientation="vertical"
android:weightSum="10"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:id="#+id/textGrid"
android:text="Games"
android:textSize="34sp"
android:textColor="#android:color/black"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<android.support.v7.widget.GridLayout
android:id="#+id/gridView"
android:layout_weight="8"
app:columnCount="2"
app:rowCount="2"
android:padding="14dp"
app:alignmentMode="alignMargins"
app:columnOrderPreserved="false"
android:layout_width="match_parent"
android:layout_height="0dp"
>
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:layout_columnWeight="1"
app:layout_rowWeight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/sampleimage" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Grid One
android:textAlignment="center"
android:textColor="#000"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView>
</android.support.v7.widget.GridLayout>
</LinearLayout>
</ScrollView>
I have a pattern like that in one of my apps. Tickets get added to the 3-wide grid and you can scroll the grid. Here is the code I use.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/your_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="vertical">
<ScrollView
android:id="#+id/your_scrollview"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="2dip"
android:layout_gravity="center_horizontal">
<LinearLayout
android:id="#+id/your_grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:visibility="visible">
<GridView
android:id="#+id/gridViewTickets"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:numColumns="3"
android:padding="8dp"
android:horizontalSpacing="8dp"
android:verticalSpacing="8dp"
android:minHeight="250dip"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal">
</GridView>
</LinearLayout>
</ScrollView
</LinearLayout>
After a lot of research I came across RecyclerView. It was a little confusing but it worked fine. I had to use two(2) layouts (activity_main.xml and row_data.xml) with MainActivity.java.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context="MainActivity">
<GridView
android:id="#+id/gridview"
android:numColumns="2"
android:verticalSpacing="1dp"
android:background="#e5e5e5"
android:horizontalSpacing="1dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</GridView>
</RelativeLayout>
row_data.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="#+id/gridviewdata"
android:layout_margin="12dp"
app:cardCornerRadius="12dp"
app:cardElevation="6dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_centerInParent="true"
>
<LinearLayout
android:orientation="vertical"
android:padding="16dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/images"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/apple" />
<TextView
android:id="#+id/fruits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Apple something"
android:textStyle="normal|italic"
android:textSize="25dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
MainActivity.java
package com.example.gridrecycler;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
GridView gridView;
String[] fruitNames = {"Apple","Orange","strawberry","Melon","Kiwi","Banana"};
int[] fruitImages = {R.drawable.apple,R.drawable.oranges,R.drawable.strawberry,R.drawable.watermelon,R.drawable.kiwi,R.drawable.banana};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//finding listview
gridView = findViewById(R.id.gridview);
CustomAdapter customAdapter = new CustomAdapter();
gridView.setAdapter(customAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// Toast.makeText(getApplicationContext(),fruitNames[i],Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(),GridItemActivity.class);
intent.putExtra("name",fruitNames[i]);
intent.putExtra("image",fruitImages[i]);
startActivity(intent);
}
});
}
private class CustomAdapter extends BaseAdapter {
#Override
public int getCount() {
return fruitImages.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View view1 = getLayoutInflater().inflate(R.layout.row_data,null);
//getting view in row_data
TextView name = view1.findViewById(R.id.fruits);
ImageView image = view1.findViewById(R.id.images);
name.setText(fruitNames[i]);
image.setImageResource(fruitImages[i]);
return view1;
}
}
}
I watched diff tuts to come up with this. Am now doubting whether it comforms to best practices. Thanks to you all for your suggestions.

How to create a Vertical Dropdown with Menu with Gridview?

I want to know how to develop a custom dropdown icons Menu with GridView options from Menu.
This is my code for custom down icons
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#c2ec97"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="viralandroid.com.androidxmluserinterfacetutorial.MainActivity">
<Button
android:id="#+id/dropdown_custom_icon_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#5bace6"
android:drawableRight="#android:drawable/arrow_down_float"
android:onClick="verticalDropDownIconMenu"
android:padding="16dp"
android:text="DropDown\t"
android:textColor="#eee" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/dropdown_custom_icon_menu"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Custom Vertical Dropdown Menu IconsAndroid Custom Vertical. Break line after Icon in Menu Item
Android.
Dropdown Menu IconsAndroid Custom Vertical Dropdown. popup menu with icon in android example.
Android Custom Vertical Dropdown Menu Icons. Android Custom Vertical Dropdown Menu Icons" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Android Custom Vertical Dropdown Menu Icons" />
</LinearLayout>
<LinearLayout
android:id="#+id/vertical_dropdown_icon_menu_items"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/dropdown_custom_icon_menu"
android:background="#333"
android:orientation="vertical"
android:padding="3dp"
android:visibility="invisible">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_send"
android:text="Android Vertical Custom DropDown Menu" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_attach"
android:text="Android Vertical Custom DropDown Menu" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_mail"
android:text="Android Vertical Custom DropDown Menu" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_refresh"
android:text="Android Vertical Custom DropDown Menu" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_attach"
android:text="Android Vertical Custom DropDown Menu" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_mail"
android:text="Android Vertical Custom DropDown Menu" />
</LinearLayout>
</RelativeLayout>
This is what i have :
This is what i want :
You need to use the spinner and that will lead to use an adapter and a couple of layouts.
First in your activity_main.xml:
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Spinner
android:id="#+id/sSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
</RelativeLayout>
then create a new layout under /res/layout folder and just name it anything like custom_spinner_layout.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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/myImageView"
android:layout_width="70dp"
android:layout_height="70dp"
android:padding="10dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="test"
android:textColor="#000" />
</LinearLayout>
then in you MainActivity.java you need to do your code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
String[] carNames={"Chevy","Dodge","Mazda","Honda","BMW","Toyota"};
int icons[] = {R.drawable.Chevy, R.drawable.Dodge, R.drawable.Mazda, R.drawable.Honda, R.drawable.BMW, R.drawable.Toyota};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spin = (Spinner) findViewById(R.id.sSpinner);
spin.setOnItemSelectedListener(this);
CustomAdapter customAdapter=new CustomAdapter(getApplicationContext(),icons,carNames);
spin.setAdapter(customAdapter);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
//your code goes here///
Toast.makeText(getApplicationContext(), carNames[position], Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// something happens here
}
}
You will need to create a new java class file and name it CustomAdapter.java and thats gonna be extended fro BaseAdapter:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter {
Context context;
int icons[];
String[] carNames;
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, int[] icons, String[] carNames) {
this.context = applicationContext;
this.icons = icons;
this.carNames = carNames;
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return icons.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.custom_spinner_layout, null);
ImageView icon = (ImageView) view.findViewById(R.id.imageView);
TextView names = (TextView) view.findViewById(R.id.textView);
icon.setImageResource(icons[i]);
names.setText(carNames[i]);
return view;
}
}
you can get the working code in :
https://github.com/edgebasis/spinnerExample

Android ListView only Showing One Item

Im trying to create a listview containing all currently installed User Apps.
The problem im facing is that the ListView is Only showing one entry and i cant figure out what im doing wrong.
I use the same code elsewhere (With a JSON Result) and it works fine.
Here is my Activity XML
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#color/colorPrimary"
tools:context=".AppsList">
<ListView
android:id="#+id/applistview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_alignParentTop="true" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Close" />
</RelativeLayout>
My List item XML
<?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:background="#color/colorPrimary" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/colorPrimary"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="6" >
<ImageView
android:id="#+id/app_icon"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_weight="1"
android:src="#drawable/frissonhead" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="4"
android:orientation="vertical"
android:weightSum="2">
<TextView
android:id="#+id/app_name"
android:layout_width="258dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Soon To Clear"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/colorAccent"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/app_package"
android:layout_width="258dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Soon To Clear"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/colorAccent"
android:textSize="12dp" />
</LinearLayout>
<CheckBox
android:id="#+id/app_check"
android:layout_width="25dp"
android:layout_height="match_parent"
android:layout_weight="1.02"
android:checked="false"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
And my Activity Java
package com.fixmypcscotland.childsafe;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class AppsList extends AppCompatActivity {
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
ListView list;
ArrayList<HashMap<String, String>> oslist;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
setContentView(R.layout.activity_apps_list);
sharedPreferences = getSharedPreferences("CHILD_MODE", MODE_PRIVATE);
editor = sharedPreferences.edit();
list = (ListView)findViewById(R.id.applistview);
oslist = new ArrayList<HashMap<String, String>>();
final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages =
pm.getInstalledApplications(PackageManager.GET_META_DATA);
HashMap<String, String> map = new HashMap<String, String>();
for (ApplicationInfo packageInfo : packages) {
String appkg = packageInfo.packageName;
String apn = pm.getApplicationLabel(packageInfo).toString();
map.put("APPPKG", appkg);
map.put("APPNAME", apn);
}
oslist.add(map);
ListAdapter adapter = new SimpleAdapter(AppsList.this, oslist, R.layout.activity_apps_list_item, new String[]{"APPPKG", "APPNAME"}, new int[]{R.id.app_package, R.id.app_name});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
editor.putString("bid", oslist.get(+position).get("APPPKG"));
editor.commit();
Toast.makeText(getApplicationContext(), oslist.get(+position).get("APPPKG"), Toast.LENGTH_LONG).show();
}
});
}
}
Any help is appreciated. Its driving me crazy.
Change your List Item with this one.
<?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="wrap_content"
android:paddingBottom="7dp"
android:background="#color/colorPrimary" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/colorPrimary"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="6" >
<ImageView
android:id="#+id/app_icon"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_weight="1"
android:src="#drawable/frissonhead" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="4"
android:orientation="vertical"
android:weightSum="2">
<TextView
android:id="#+id/app_name"
android:layout_width="258dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Soon To Clear"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/colorAccent"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/app_package"
android:layout_width="258dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Soon To Clear"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/colorAccent"
android:textSize="12dp" />
</LinearLayout>
<CheckBox
android:id="#+id/app_check"
android:layout_width="25dp"
android:layout_height="match_parent"
android:layout_weight="1.02"
android:checked="false"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
And Learn about Adapter click here you may get hint from here.
for (ApplicationInfo packageInfo : packages) {
String appkg = packageInfo.packageName;
String apn = pm.getApplicationLabel(packageInfo).toString();
map.put("APPPKG", appkg);
map.put("APPNAME", apn);
**oslist.add(map);**
}
You are passing the wrong data to your adapter, so you have to change this line:
ListAdapter adapter = new SimpleAdapter(AppsList.this, oslist, R.layout.activity_apps_list_item, oslist, new int[]{R.id.app_package, R.id.app_name});
Also, I must to warn you to see the documentation about the SimpleAdapter to pass the right parameters to it:
The first parameter is a Context where the View associated with this SimpleAdapter is running
The second parameter is a Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to".
The third is a list of column names that will be added to the Map associated with each item.
And the last one is The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.
And then, fix your for loop:
for (ApplicationInfo packageInfo : packages) {
String appkg = packageInfo.packageName;
String apn = pm.getApplicationLabel(packageInfo).toString();
map.put("APPPKG", appkg);
map.put("APPNAME", apn);
oslist.add(map);
}

Android: How to add column title in custom listview

I have a listview and it is properly arranged. I need to add column title. Below i have posted the display.xml file which handles the listview arrangments. But there is no title.
Display.xml
<?xml version="1.0" encoding="utf-8"?>
<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:background="#F3CAE5"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="8dp" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000"
android:text="Ser No."/>
<TextView
android:id="#+id/id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000" />
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textColor="#000" />
<TextView
android:id="#+id/add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textColor="#000" />
</LinearLayout>
ListView.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/header"/>
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
ShowData.java
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
/**
* Created by Akash on 3/13/2016.
*/
public class ShowData extends ListActivity {
private VivzHelper helper;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
VivzHelper empClick = new VivzHelper(getApplicationContext());
Cursor cursor = empClick.getDetails();
if (cursor != null)
getListView().setAdapter(
new android.support.v4.widget.SimpleCursorAdapter(this, R.layout.display, cursor, new String[]{
"_id", "Name", "Address"
}, new int[]{R.id.id, R.id.name, R.id.add}, 0));
}
}
If you want to show a title on top of your listView you could use a header. Instead of including the header layout above your listView, remove it. Leave it in another xml resource (header.xml).
And then when you set the adapter, add also the header to your list like this
ListView listView = getListView();
View header = getLayoutInflater().inflate(R.layout.header, null);
listView.addHeaderView(header);
listView.setAdapter(new android.support.v4.widget.SimpleCursorAdapter(this, R.layout.display, cursor, new String[]{
"_id", "Name", "Address"
}, new int[]{R.id.id, R.id.name, R.id.add}, 0));
(I suppose that in your method getListView() you are doing something like
private ListView getListView(){
return ListView listView = (ListView) findViewById(R.id.list);
}
This should be your list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
And the header.xml will be another layout where you can define whatever you want to appear on top of your list. If you want three columns
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Street"
android:layout_gravity="left"
android:background="#88FF0000"/>
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="number"
android:layout_gravity="right"
android:background="#8800FF00"/>
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="name"
android:layout_gravity="right"
android:background="#8800FF00"/>
</LinearLayout
You can do that with RecyclerView.Adapter.
RecyclerView.Adapter has getItemViewType() function.
Example
#Override
public int getItemViewType(int position)
{
if(position == 0)
return LIST_HEADER;
else if(position > 0)
return LIST_ITEM;
return -1;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
if (viewType == LIST_HEADER)
{
View view = LayoutInflater.from(mContext).inflate(R.layout.your_header_layout, parent, false);
return view;
}
else if (viewType == LIST_ITEM)
{
View view = LayoutInflater.from(mContext).inflate(R.layout.your_item_layout, parent, false);
return view;
}
return null;
}
change your Display.xml name to header.xml............
then add layout using include..
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/header"/>
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>

Not able to customize ListView

I'm working on developing a simple todo list app with a custom ListView. I want to be able to change the colors (such as white text rather then black on the text view) and perhaps add a checkbox for each item in the ListView.
When using a default list view I need to use the layout.xml id '#android:id/list', however, to customize the view, I am using the id '#+id/task_list'. When I do this I receive the error:
"your content must have a listview whose id attribute is 'android.r.id.list'"
What should I do? And insight will be appreciated.
Below is the code that I am using.
MainActivity.java
package com.example.todo;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ListActivity {
ArrayList<TextView> listItems=new ArrayList<TextView>();
ArrayAdapter<TextView> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter=new ArrayAdapter<TextView>(this,
R.layout.task_row_item,
listItems);
setListAdapter(adapter);
}
public void addItems(View view) {
EditText newItem = (EditText) findViewById(R.id.textField);
TextView listItem = (TextView) findViewById(R.id.task_description);
listItems.add(listItem);
adapter.notifyDataSetChanged();
newItem.setText("");
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#color/black"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/textField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="Enter a task..."
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/addItems"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="addItems"
android:text="Add" />
</LinearLayout>
<ListView
android:id="#+id/task_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
task_row_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="horizontal" >
<TextView
android:id="#+id/task_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
Either stop using ListActivity or replace
<ListView
android:id="#+id/task_list"
with
<ListView
android:id="#android:id/list"
Get familiar with: http://developer.android.com/reference/android/app/ListActivity.html

Categories

Resources