add image in multi listview - android

does anyone know how to add an image into multi listview? What I want is adding an image into map1.put("Icon", "InsertImage");
Code below is my java class
public class MultiList extends ListActivity {
ListView lv;
SimpleAdapter sd;
#Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
lv=getListView();
ArrayList<HashMap<String,String>> alist=new ArrayList<HashMap<String,String>>();
HashMap<String, String>map1 = new HashMap<String, String>();
map1.put("Class","Class 1");
map1.put("Icon", "InsertImage");
map1.put("Description", "Vehicles with 2 axles and 3 or 4 wheels excluding taxis");
alist.add(map1);
sd=new SimpleAdapter(this,alist,R.layout.row,new String[]{"Class","Icon","Description"},new int[]{R.id.c1,R.id.i2,R.id.d3});
lv.setAdapter(sd);
}
catch (Exception e) {
Log.w("Sravan", e.toString());
}
}
}
in my xml:
<?xml version="1.0" encoding="UTF-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/c1"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_weight="0.44"
android:text="text1"
android:textSize="18sp" />
<TextView
android:id="#+id/i2"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_weight="0.7"
android:text="text2"
android:textSize="16sp" />
<TextView
android:id="#+id/d3"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_weight="0.7"
android:text="text3"
android:textSize="16sp" />
</TableRow>

You need to change the data type of what you mapping to. Instead of HashMap<String, String> try HashMap<String, Object>.
See this question for more assistance.

Related

Custom list view add text view to new line of row

I Create this list view
With this code
Xml Code
<?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:weightSum="5"
android:background="#drawable/border"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textStyle="bold"
android:textColor="#000000"
android:id="#+id/lblDate"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textStyle="bold"
android:id="#+id/lblDescription"
android:singleLine="true"
android:textColor="#000000"
android:ellipsize="end"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textStyle="bold"
android:textColor="#000000"
android:id="#+id/lblAmount"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textStyle="bold"
android:textColor="#000000"
android:id="#+id/lblPrice"/>
</LinearLayout>
Java Code
public class FillList extends AsyncTask<String, String, String> {
List<Map<String, String>> prolist = new ArrayList<Map<String, String>>();
#Override
protected void onPreExecute() {
}
#Override
protected void onPostExecute(String r) {
String[] from = {"D", "C", "B","A"};
int[] views = { R.id.lblPrice,R.id.lblAmount,R.id.lblDescription,R.id.lblDate};
final SimpleAdapter ADA = new SimpleAdapter(Products.this,
prolist, R.layout.lsttemplate, from,
views);
lstSoratHesab.setAdapter(ADA);
}
#Override
protected String doInBackground(String... params) {
Map<String, String> datanum = new HashMap<String, String>();
datanum.put("D", "price");
datanum.put("C", "amount");
datanum.put("B", "description");
datanum.put("A", "date");
prolist.add(datanum);
while (rs.next()) {
datanum = new HashMap<String, String>();
datanum.put("D", rs.getString("Bes"));
datanum.put("C", rs.getString("Bed"));
datanum.put("B", rs.getString("Sharh"));
datanum.put("A", rs.getString("Date"));
prolist.add(datanum);
}
}
}
[1]: https://i.stack.imgur.com/kFE56.png
Because description is too long i want change list view to
Question:How can change listview and set description in new line in list view?
The simplest way to achieve this layout is to use two LinearLayouts in your design. The parent LinearLayout should have a vertical orientation and the child LinearLayout should have a horizontal orientation. You can see this in the code below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:width="match_parent"
android:height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:width="match_parent"
android:height="wrap_content"
android:orientation="horizontal" >
<!-- Most of your TextViews go there -->
</LinearLayout>
<!-- The description TextView -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lblDescription" />
</LinearLayout>

How do i get the state of a pair of RadioButtons, in a RadioGroup, in a ListView?

I have a list-view with a custom list row. In each list-view's row, there is a Radio-group with 2 Radio-buttons.
I need to know the state of the RadioButtons, for each row of the ListView (checked or not), dynamically, as the user click on one of the RadioGroup's options.
My main xml:
<?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="#fff3f2f2"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#00ffffff"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ffffff"
android:gravity="center_horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="#string/titulo_cabecalho"
android:textColor="#ff000000"
android:textSize="36sp" >
</TextView>
</RelativeLayout>
<!-- Footer aligned to top -->
<include layout="#layout/footer" />
<RelativeLayout
android:id="#+id/selectEscola"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/footer"
android:layout_below="#id/header"
android:layout_centerInParent="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="4dp"
android:background="#00ffffff"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Chamada"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#c0000000" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="80dp"
android:layout_marginBottom="80dp"
android:scrollbars="none"
android:background="#drawable/border" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="#+id/listChamada"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:padding="2dp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
</RelativeLayout>
My custom row is:
<?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"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:padding="2dip"
android:background="#drawable/border2" >
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="34dip"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip"
android:orientation="horizontal" >
<ImageView
android:id="#+id/listEdit"
android:layout_width="24dip"
android:layout_height="24dip"
android:layout_marginTop="2dip"
android:src="#drawable/list" />
<ImageView
android:id="#+id/list_image"
android:layout_width="24dip"
android:layout_height="24dip"
android:layout_marginLeft="20dip"
android:layout_marginTop="2dip"
android:src="#drawable/boy" />
</LinearLayout>
<TextView
android:id="#+id/nomealuno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/matriculaaluno"
android:layout_alignTop="#+id/thumbnail"
android:text="Nome:"
android:textColor="#040404"
android:textSize="18sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/matriculaaluno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/nomealuno"
android:layout_marginTop="2dp"
android:layout_toRightOf="#+id/thumbnail"
android:text="MatrĂ­cula: "
android:textColor="#343434"
android:textSize="10sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="4dp"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/radiogroupChamada"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:textColor="#343434"
android:textSize="12sp"
android:textStyle="bold" >
<RadioButton
android:id="#+id/radiobuttonPresente"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
android:drawableRight="#drawable/btn_check"
android:paddingLeft="22dip"
android:text="Presente" >
</RadioButton>
<RadioButton
android:id="#+id/radiobuttonAusente"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
android:drawableRight="#drawable/btn_check"
android:paddingLeft="22dip"
android:text="Ausente" >
</RadioButton>
</RadioGroup>
</LinearLayout>
</RelativeLayout>
This is how I populate the ListView:
1 - Code to put the data received from server received in Json format, in arrays and hashmap (I use the arrays to have a reference to the data present in the ListView and hashmap to populate it) :
if (success2 == 1) // If data from server was sent without errors
{
int lenght = jArray.length();
optionsMatriculas = new ArrayList<String>(lenght);
optionsNomesAlunos = new ArrayList<String>(lenght);
optionsSexoAlunos = new ArrayList<String>(lenght);
for (int i = 0; i < jArray.length(); i++)
{
try
{
JSONObject oneObject = jArray.getJSONObject(i);
resultDataCodigoAluno = oneObject.getString("cdaluno");
resultDataNomes = oneObject.getString("nome");
resultDataSexo = oneObject.getString("sexo");
optionsMatriculas.add(resultDataCodigoAluno);
optionsNomesAlunos.add(resultDataNomes);
optionsSexoAlunos.add(resultDataSexo);
HashMap<String, String> hm = new HashMap<String, String>();
if (resultDataSexo.equals("M"))
{
hm.put("sexo", Integer.toString(R.drawable.boy));
}
else
{
hm.put("sexo", Integer.toString(R.drawable.girl));
}
hm.put("matricula", resultDataCodigoAluno);
hm.put("nome", resultDataNomes);
alunosLista.add(hm);
}
catch (JSONException e)
{
Log.e("log_tag", "Error in parsing JSon: " + e.toString());
}
}
Message msg = myHandler.obtainMessage();
myHandler.sendMessage(msg);
}
This is the code to populate the listview:
final Handler myHandler = new Handler()
{
#Override
public void handleMessage(Message msg)
{
try
{
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), alunosLista, R.layout.list_row, from, to);
listaChamada.setAdapter(adapter);
int lenght = adapter.getCount();
int totalHeight = 0;
for (int i = 0; i < lenght; i++)
{
View listItem = adapter.getView(i, null, listaChamada);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listaChamada.getLayoutParams();
params.height = totalHeight + 4 + (listaChamada.getDividerHeight() * (adapter.getCount() - 1));
listaChamada.setLayoutParams(params);
listaChamada.requestLayout();
}
catch (Exception e)
{
Log.e("log_tag", "Erro no ListView: " + e.toString());
}
}//handleMessage
};//myHandler
I have searched, in Google, but still no luck.
Thanks for any clue about this.
Actually, Android adapters is not that simple. SimpleAdapter can be applied only in some very simple/trivial cases. Usually, it's preferred to create own BaseAdapter: it gives much more flexibility and doesn't require lot of code.
In your case I would suggest to use smth like the following (You failed to provide any information about that is from and to in your SimpleAdapter constructor, so I guessed):
Main part is proper adapter, which should store selected radio button id and provide it back to the activity/receiver.
MyAdapter.java
public class MyActivity extends Activity implements MyListAdapter.RadioButtonsListener {
private static final String TAG = "MyActivity";
ArrayList<HashMap<String, String>> mAlunosLista = new ArrayList<HashMap<String, String>>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView listaChamada = (ListView) findViewById(R.id.listChamada);
// Fill list with data
Random sexGen = new Random();
for (int i = 0; i < 20; i++) {
addTextData(sexGen.nextBoolean() ? "M" : "F", Integer.toString(i + 100), "Name" + i + " Surname" + i);
}
final MyListAdapter adapter = new MyListAdapter(mAlunosLista);
listaChamada.setAdapter(adapter);
}
#Override
protected void onResume() {
super.onResume();
final MyListAdapter adapter = (MyListAdapter) ((ListView) findViewById(R.id.listChamada)).getAdapter();
adapter.setRadioListener(this);
}
#Override
protected void onPause() {
super.onPause();
final MyListAdapter adapter = (MyListAdapter) ((ListView) findViewById(R.id.listChamada)).getAdapter();
adapter.setRadioListener(null);
}
/**
* Adds test data to adapter array
*/
private void addTextData(final String resultDataSexo, final String resultDataCodigoAluno, final String resultDataNomes) {
HashMap<String, String> hm = new HashMap<String, String>();
// NOTE: not sure why you convert ids to strings
if (resultDataSexo.equals("M")) {
hm.put("sexo", Integer.toString(R.drawable.boy));
} else {
hm.put("sexo", Integer.toString(R.drawable.girl));
}
hm.put("matricula", resultDataCodigoAluno);
hm.put("nome", resultDataNomes);
mAlunosLista.add(hm);
}
#Override
public void onRadioButtonClicked(final int position, final HashMap<String, String> itemData, final int clickedId) {
Log.d(TAG, "Clicked item at " + position + ", checked id: " + (clickedId == R.id.radiobuttonPresente ? "Presente" : "Ausente"));
}
}
Below other files I've changed to have compilable example (please refer help topic on how to ask questions about code questions body).
main.xml
<?xml version="1.0" encoding="utf-8"?>
<!--suppress AndroidLintUselessParent, AndroidLintContentDescription -->
<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="#fff3f2f2"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#00ffffff"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ffffff"
android:gravity="center_horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Title"
android:textColor="#ff000000"
android:textSize="36sp" >
</TextView>
</RelativeLayout>
<!-- Footer aligned to top
<include layout="#layout/footer" />
-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/footer"
android:layout_alignParentBottom="true" />
<RelativeLayout
android:id="#+id/selectEscola"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="#id/footer"
android:layout_below="#id/header"
android:layout_centerInParent="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="4dp"
android:background="#00ffffff"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Chamada"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#c0000000" />
<ListView
android:id="#+id/listChamada"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#android:drawable/list_selector_background"
android:padding="2dp" />
</RelativeLayout>
</RelativeLayout>
list_row.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="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:padding="2dip"
android:background="#android:color/darker_gray" >
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="5dip"
android:padding="3dip"
android:orientation="horizontal" >
<ImageView
android:id="#+id/listEdit"
android:layout_width="24dip"
android:layout_height="24dip"
android:layout_marginTop="2dip"
android:src="#android:drawable/ic_menu_add" />
<ImageView
android:id="#+id/list_image"
android:layout_width="24dip"
android:layout_height="24dip"
android:layout_marginLeft="20dip"
android:layout_marginTop="2dip"
android:src="#android:drawable/ic_delete" />
</LinearLayout>
<TextView
android:id="#+id/nomealuno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/matriculaaluno"
android:layout_alignTop="#+id/thumbnail"
android:text="Nome:"
android:textColor="#040404"
android:textSize="18sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/matriculaaluno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/nomealuno"
android:layout_marginTop="2dp"
android:layout_toRightOf="#+id/thumbnail"
android:text="MatrĂ­cula: "
android:textColor="#343434"
android:textSize="10sp" />
<RadioGroup
android:id="#+id/radiogroupChamada"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/thumbnail"
android:orientation="vertical"
android:textColor="#343434"
android:textSize="12sp"
android:textStyle="bold" >
<RadioButton
android:id="#+id/radiobuttonPresente"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="22dip"
android:text="Presente" />
<!--android:drawableRight="#drawable/btn_check"-->
<RadioButton
android:id="#+id/radiobuttonAusente"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="22dip"
android:text="Ausente" />
<!--android:drawableRight="#drawable/btn_check"-->
</RadioGroup>
</RelativeLayout>
MyActivity.java
public class MyActivity extends Activity implements MyListAdapter.RadioButtonsListener {
private static final String TAG = "MyActivity";
ArrayList<HashMap<String, String>> mAlunosLista = new ArrayList<HashMap<String, String>>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView listaChamada = (ListView) findViewById(R.id.listChamada);
// Fill list with data
Random sexGen = new Random();
for (int i = 0; i < 20; i++) {
addTextData(sexGen.nextBoolean() ? "M" : "F", Integer.toString(i + 100), "Name" + i + " Surname" + i);
}
final MyListAdapter adapter = new MyListAdapter(mAlunosLista);
listaChamada.setAdapter(adapter);
}
#Override
protected void onResume() {
super.onResume();
final MyListAdapter adapter = (MyListAdapter) ((ListView) findViewById(R.id.listChamada)).getAdapter();
adapter.setRadioListener(this);
}
#Override
protected void onPause() {
super.onPause();
final MyListAdapter adapter = (MyListAdapter) ((ListView) findViewById(R.id.listChamada)).getAdapter();
adapter.setRadioListener(null);
}
/**
* Adds test data to adapter array
*/
private void addTextData(final String resultDataSexo, final String resultDataCodigoAluno, final String resultDataNomes) {
HashMap<String, String> hm = new HashMap<String, String>();
// NOTE: not sure why you convert ids to strings
if (resultDataSexo.equals("M")) {
hm.put("sexo", Integer.toString(R.drawable.boy));
} else {
hm.put("sexo", Integer.toString(R.drawable.girl));
}
hm.put("matricula", resultDataCodigoAluno);
hm.put("nome", resultDataNomes);
mAlunosLista.add(hm);
}
#Override
public void onRadioButtonClicked(final int position, final HashMap<String, String> itemData, final int clickedId) {
Log.d(TAG, "Clicked item at " + position + ", checked id: " + (clickedId == R.id.radiobuttonPresente ? "Presente" : "Ausente"));
}
}
PS Also, it looks like You're trying to make some hack to ListView. I have no idea why do You need it here, but it's better to avoid until completely necessary.

How to separate header/footer and data in listview?

my app needs to display data in a list, I walked through some tutorials and figured out to use a header.xml and row.xml. It works fine but now I would like to add more stuff on my header.xml and I found out that the entire header.xml is scrolling with the row.xml as well, which I dont really want.
Any solution that doesn't require me to rewrite and change my code style completely?
Activity:
public class HistoryActivity extends ListActivity
{
private static final String TAG = "HistoryActivity";
ListView lv;
SimpleAdapter sd;
RecordDAO dao = new RecordDAO(HistoryActivity.this);
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
lv = getListView();
lv.addHeaderView(getLayoutInflater().inflate(
R.layout.header, null, false));
}
#Override
protected void onResume()
{
super.onResume();
ArrayList<Record> Records = new ArrayList<Record>();
Records = (ArrayList<Record>) dao.findAll();
ArrayList<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for (int x = Records.size()-1; x >=0; x--)
{
map = new HashMap<String, String>();
map.put(....// all my data
aList.add(map);
}
sd = new SimpleAdapter(this, aList, R.layout.row,
new String[]
{ "date", "name", "time",
"rating" }, new int[]
{ R.id.date, R.id.name, R.id.time,
R.id.rating});
lv.setAdapter(sd);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3)
{
TextView tx = (TextView) view.findViewById(R.id.date);
String s = tx.getText().toString();
Intent intent = new Intent(HistoryActivity.this, EditRecordActivity.class);
intent.putExtra("date", s);
startActivity(intent);
}
});
}
private void insertNewRecord()
{
dao.add(newRecord);
}
}
header.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Date"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="name"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Time"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Rating"
android:textColor="#FFFFFF"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
row.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:id="#+id/date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/Rating"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF" />
</LinearLayout>
Don't use ListActivity. I think it is a bad practice. Use a regular Activity. Just insert a ListView with an id "#android:id/list" into header.xml.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Date"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="name"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Time"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Rating"
android:textColor="#FFFFFF"
android:textSize="16dp" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
And you can get the list reference by doing:
ListView lv = (ListView)findViewById(R.id.list);
Setting the Header of a ListView means that it will be the first item of your list, but still will scroll with the list itself.
In your case, you can create some kind of layout (similar to header.xml) and place it above the ListView.
Instead of ListActivity, extend from Activity
Create a main_layout.xml that define some views and a ListView.
main_layout
<include header.xml/>
<listview>
If you don't want your header and footer views to scroll with your ListView, then make sure that you use the addHeaderView() and addFooterView() BEFORE you call your setAdapter() method. I would recommend moving the setAdapter() method to your onCreate() method.

how to create attribute name on each of the column of a listview?

I have populated a listview(4columns) using simpleadapter and hashmap. Now I would like to have each column of the listview a name.
My listview is displayed inside a linear layout with 4 textviews.
Now, in order to have attribute name of each column, I try to have 2 linear layouts, and the top linear layout have 4 textviews with the attribute names, the bottom linear layout with the data from simpleadapter. And put two linear layouts inside a another linear layout.
And this didn't work the way i want...
I am fairly new to android, please help.
Edit:
Here is my code:
public class MultiList extends ListActivity {
ListView lv;
SimpleAdapter sd;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Calendar c = Calendar.getInstance();
lv = getListView();
ArrayList<HashMap<String, String>> aList = new ArrayList<HashMap<String,String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("Date", ""+c.get(Calendar.DATE));
map.put("Month", ""+c.get(Calendar.MONTH)+1);
map.put("Time", "" + new Date().toString());
aList.add(map);
map = new HashMap<String, String>();
map.put("Date", ""+c.get(Calendar.DATE));
map.put("Month", ""+c.get(Calendar.MONTH)+1);
map.put("Time", "" + new Date().toString());
aList.add(map);
map = new HashMap<String, String>();
map.put("Date", ""+c.get(Calendar.DATE));
map.put("Month", ""+c.get(Calendar.MONTH)+1);
map.put("Time", "" + new Date().toString());
aList.add(map);
sd= new SimpleAdapter(this, aList, R.layout.main, new String[]{"Date","Month","Time"}, new int[]{R.id.studentID,R.id.studentAge,R.id.studentName});
lv.setAdapter(sd);
// insertData();
}
My main.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
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:layout_width="90dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="student ID" />
<TextView
android:layout_width="90dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="student Age" />
<TextView
android:layout_width="90dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="student Name" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:id="#+id/studentID"
android:layout_width="90dip"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="#+id/studentAge"
android:layout_width="90dip"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="#+id/studentName"
android:layout_width="90dip"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
The problem is, each of the attribute name (student ID, student Age, Student Name) is repeated on every row. How do I fix that?
Break your layout into two, the first one will have the column headers. Let's call it header.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:layout_width="90dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="student ID" />
<TextView
android:layout_width="90dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="student Age" />
<TextView
android:layout_width="90dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="student Name" />
</LinearLayout>
The second one is simply for each row, called row.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:id="#+id/studentID"
android:layout_width="90dip"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="#+id/studentAge"
android:layout_width="90dip"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="#+id/studentName"
android:layout_width="90dip"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
(You can give them better names later.)
Now your onCreate() should have this:
// Set up your header
lv.addHeaderView(getLayoutInflater().inflate(R.layout.header, null, false));
// Change the resource id to R.layout.row
sd = new SimpleAdapter(this, aList, R.layout.row, new String[]{"Date","Month","Time"}, new int[]{R.id.studentID,R.id.studentAge,R.id.studentName});
lv.setAdapter(sd);

Why my ListView is adding all items from xml?

I couldn't find an answer for my problem, I have a Main XML with some TextViews and 1 ListView, the problem is when my List appear on my Main xml it adds the Texviews that are outside the listview, so I get in each Item a TOTAL textview and the list go in all the XML layout, not at the bottom, so I am not sure what am doing wrong
thx in advanced for your help
Regards
here are my files
<?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" >
<TableLayout
android:id="#+id/TableLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:stretchColumns="1" >
<TableRow>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Expenses $"
android:textColor="#android:color/white"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvTotalGastos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textColor="#android:color/white"
android:textSize="25sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/TableLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/TableLayout01"
android=""
android:orientation="horizontal" >
<TableRow>
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="1"
android:padding="10dip"
android:text="Date"
android:textColor="#android:color/white" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="1"
android:padding="10dip"
android:text="Description"
android:textColor="#android:color/white" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="1"
android:padding="10dip"
android:text="Amount"
android:textColor="#android:color/white" />
</TableRow>
</TableLayout>
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
My Row 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="wrap_content"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:id="#+id/tvDate"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="1"
android:padding="10dip"
android:text="Date" />
<TextView
android:id="#+id/tvDescription"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="1"
android:padding="10dip"
android:text="Description" />
<TextView
android:id="#+id/tvAmount"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="1"
android:padding="10dip"
android:text="Amount" />
</LinearLayout>
and my Java class
public class GastosListActivity extends ListActivity {
private DatabaseHandler dbh;
private List<Datos> datos;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.gastoslist);
//ListView list2= (ListView) findViewById(R.id.listview);
// new obj databasehandler
dbh = new DatabaseHandler(this);
datos = dbh.getAllData();
ArrayList<Map<String, String>> list = buildData();
String[] from = { "Date", "Description","Amount" };
int[] to = { R.id.tvDate, R.id.tvDescription,R.id.tvAmount };
SimpleAdapter adapter = new SimpleAdapter(this, list,
R.layout.gastoslist, from, to);
setListAdapter(adapter);
}
private ArrayList<Map<String, String>> buildData() {
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
for (Datos dn : datos) {
list.add(putData(dn.get_fecha(), dn.get_concepto(), dn.get_cantidad()));
}
return list;
}
private HashMap<String, String> putData(String date, String description, String amount) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("Date", date);
item.put("Description", description);
item.put("Amount", amount);
return item;
}
}
Problem Solve, I change the code using Cursor instead of arraylist
dbh = new DatabaseHandler(this);
Cursor cursor = dbh.obtenerDatos();
String[] from = new String[] { dbh.COLUMN_FECHA,
dbh.COLUMN_CONCEPTO, dbh.COLUMN_CANTIDAD };
int[] to = new int[] { R.id.tvDate, R.id.tvDescription,
R.id.tvAmount };
cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor,
from, to);
listContent.setAdapter(cursorAdapter);

Categories

Resources