I am very new to programming. I have an app that has several views. The Main view shows a list such as Breakfast, Lunch & Dinner. When a an item is selected, example Lunch, a list of lunch menu items is displayed such as Hamburger, Cheeseburger, French Fries... (this list is created from the string-array lunch_menu that is stored in \values\lunch.xml) as the user selects the items they want, it is stored in a new array called myNewList and is displayed whe the users presses the lunchList button. All of the items are displayed that the user selected. So far, So good. I created a android:onClick="shareMyList" in the selecteditems.xml and the button works, but does not populate my list. I think what i need is to some how convert it to a string, this is where I need help.
Here is my Problem now.... I have my Share button, that when pressed, I would like it to automatically open the default Messaging app and populate the list from the selected items ListView.
package com.mycompany.lunch;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class LunchListMenu extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maincoarse);
final ListView lv=(ListView)findViewById(R.id.listView1);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.lunch_menu,android.R.layout.simple_list_item_1);
lv.setAdapter(adapter);
final ArrayList<String> myNewList = new ArrayList<String>();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String item=lv.getItemAtPosition(arg2).toString();
String itemordered;
itemordered = item + " added to list";
Toast.makeText(getApplicationContext(), itemordered, Toast.LENGTH_SHORT).show();
myNewList.add(item);
}
});
// List View Button
Button btnLunchList = (Button) findViewById(R.id.lrList);
btnLunchList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.selecteditems);
ListView selecteditems = (ListView) findViewById(android.R.id.list);
ArrayAdapter<String> newadapter = new ArrayAdapter<String>(LunchListMenu.this, android.R.layout.simple_list_item_1, myNewList);
selecteditems.setAdapter(newadapter);
}
});
}
public void shareMyList(View v){
// Share Selected Items Button
Button btnShareItems = (Button) findViewById(R.id.shareMyList);
btnShareItems.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, "I'm being sent!!");
startActivity(Intent.createChooser(share, "Share Text"));
}
});
}
}
Here is the Lunch Menu Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/main_background"
android:paddingLeft="10.0dip"
android:paddingTop="0.0dip"
android:paddingRight="10.0dip"
android:paddingBottom="10.0dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/lrList"
android:layout_width="72dip"
android:layout_height="72dip"
android:layout_gravity="right"
android:background="#drawable/list" />
<ImageView
android:id="#+id/LunchMenuTitle"
android:contentDescription="#string/LunchMenu"
android:layout_width="0dip"
android:layout_height="72dip"
android:layout_weight="0.96"
android:background="#drawable/lunch"
android:paddingLeft="10.0dip"
android:paddingRight="10.0dip" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="10.0dip"
android:background="#drawable/head"
android:orientation="horizontal" />
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#FFCC00"
android:dividerHeight="2dp" >
</ListView>
<LinearLayout
android:orientation="horizontal"
android:background="#drawable/head"
android:layout_width="fill_parent"
android:layout_height="10.0dip" />
</LinearLayout>
And Here is my selecteditems.xml Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#drawable/main_background"
android:paddingLeft="10.0dip"
android:paddingTop="0.0dip"
android:paddingRight="10.0dip"
android:paddingBottom="10.0dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/shareMyList"
android:layout_width="72dip"
android:layout_height="72dip"
android:layout_gravity="right"
android:onClick="shareMyList"
android:background="#drawable/share" />
<ImageView
android:id="#+id/selectedItemsTitle"
android:contentDescription="#string/LunchTitle"
android:layout_width="0dip"
android:layout_height="72dip"
android:layout_weight="0.96"
android:background="#drawable/title"
android:paddingLeft="10.0dip"
android:paddingRight="10.0dip" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="10.0dip"
android:background="#drawable/head"
android:orientation="horizontal" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#FFCC00"
android:dividerHeight="2dp"
android:padding="10dip"
android:textColor="#ffffff"
android:textSize="20dip"
android:textStyle="bold" />
</LinearLayout>
This may be the most kludgiest way of doing this, but it works.
package com.mycompany.lunch;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class LunchListMenu extends Activity {
String itemsordered;
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maincoarse);
final ListView lv=(ListView)findViewById(R.id.listView1);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.lunch_menu,android.R.layout.simple_list_item_1);
lv.setAdapter(adapter);
final ArrayList<String> myNewList = new ArrayList<String>();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String item=lv.getItemAtPosition(arg2).toString();
String itemordered;
itemordered = item + " added to list";
Toast.makeText(getApplicationContext(), itemordered, Toast.LENGTH_SHORT).show();
myNewList.add(item);
}
});
// List View Button
Button btnLunchList = (Button) findViewById(R.id.lrList);
btnLunchList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.selecteditems);
ListView selecteditems = (ListView) findViewById(android.R.id.list);
ArrayAdapter<String> newadapter = new ArrayAdapter<String>(LunchListMenu.this, android.R.layout.simple_list_item_1, myNewList);
selecteditems.setAdapter(newadapter);
// Get sdCard location so we can Create Dir and File
File sdCard = Environment.getExternalStorageDirectory();
File lunch = new File(sdCard,"Lunch");
lunch.mkdirs();
File file = new File(lunch, "Lunch.txt");
PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter(file));
} catch (IOException e) {
e.printStackTrace();
}
// Write each string in the array
StringBuilder text = new StringBuilder();
for (String s : myNewList) {
out.println(s);
}
out.close();
// read File
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append(',');
text.append(' ');
}
}
catch (IOException e) {
}
itemsordered = text;
}
});
}
public void shareMyList(View v){
// Share Selected Items Button
Button btnShareItems = (Button) findViewById(R.id.shareMyList);
btnShareItems.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", itemsordered);
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
}
});
}
}
Related
The Register Button works well, but the debugger doesn't run into the setOnItemClickListener onItemClick. If You click on a item, the activity for editing the record should be launched.
package at.bomsbg.MindX;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import at.bomsbg.MindX.sqlite.helper.MindXopenHelper;
import at.bomsbg.MindX.sqlite.helper.MindXtableAdapter;
public class Activity_cardmanager extends Activity {
MindXtableAdapter mindxtableadapt;
MindXopenHelper openHelper;
ListView nameList;
Button registerBtn;
Cursor cursor;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cardmanager);
nameList = (ListView) findViewById(R.id.title);
registerBtn = (Button) findViewById(R.id.btn_register);
mindxtableadapt = new MindXtableAdapter(this);
String[] from = { MindXopenHelper.title, MindXopenHelper.detail,
MindXopenHelper.active };
int[] to = { R.id.tv_title, R.id.tv_detail, R.id.tv_active };
cursor = mindxtableadapt.queryName();
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this,
R.layout.row, cursor, from, to);
nameList.setAdapter(cursorAdapter);
nameList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> tobjParent, View tobjView,
int tintPosition, long tlngid) {
Bundle passdata = new Bundle();
Cursor listCursor = (Cursor) tobjParent
.getItemAtPosition(tintPosition);
int nameId = listCursor.getInt(listCursor
.getColumnIndex(MindXopenHelper.KEY_ID));
passdata.putInt(MindXopenHelper.KEY_ID, nameId);
Intent passIntent = new Intent(Activity_cardmanager.this,
Activity_edit_mindxtables.class);
Toast.makeText(getApplicationContext(),
Integer.toString(nameId), 500).show();
passIntent.putExtras(passdata);
startActivity(passIntent);
}
});
registerBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent registerIntent = new Intent(Activity_cardmanager.this,
Activity_MindXtablesRecord.class);
startActivity(registerIntent);
}
});
}
#Override
public void onResume() {
super.onResume();
cursor.requery();
}
}
activity_Cardmanager.html
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
<Button
android:id="#+id/btn_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/register" />
</LinearLayout>
row.xml
I add the item clickable for the textfield. I think that is not realy necesarry.
<?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:clickable="true"
android:orientation="horizontal" >
<TextView
android:id="#+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:width="100dp" />
<TextView
android:id="#+id/tv_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:width="100dp" />
<CheckBox
android:id="#+id/tv_active"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp" />
</LinearLayout>
You have focusable/clickable items in your list view item - the checkbox.
Option 1 - Set the android:descendantFocusability="blocksDescendants" flag to your ListView and you should have fired the onItemClickListener.
Option 2 - In your Adapter you can set an onClickListener on the entire list view item object, and handle it from there.
the Problem of my solution was on row.xml
again thanX on all:
row.xml
<LinearLayout
android:clickable="true" (removed)
<TextView
android:id="#+id/tv_title"
android:onClick="Title" (added)
android:clickable="true" (added)
android:focusable="true" (added)
on my Activity_cardmanager I inserted following code after nameList.setAdapter...
nameList.setClickable(true); // 2015-04-14
nameList.setFocusable(true); // 2015-04-14
nameList.setItemsCanFocus(true); // 2015-04-14
setViewItemListener();
.
.
.
public void setViewItemListener() {
nameList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> tobjParent, View tobjView, int tintPosition, long tlngid) {
Bundle passdata = new Bundle();
// Cursor listCursor = (Cursor) tobjParent.getItemAtPosition(tintPosition);
Cursor listCursor = (Cursor) nameList.getItemAtPosition(tintPosition); //2015-04-15
int nameId = listCursor.getInt(listCursor
.getColumnIndex(MindXopenHelper.KEY_ID));
passdata.putInt(MindXopenHelper.KEY_ID, nameId);
Intent passIntent = new Intent(Activity_cardmanager.this,
Activity_edit_mindxtables.class);
Toast.makeText(getApplicationContext(),
Integer.toString(nameId), Toast.LENGTH_LONG).show();
passIntent.putExtras(passdata);
startActivity(passIntent);
}
});
}
the returnValue is true, but that is another story.
I am a beginner and I want the user to select three fields from a spinner, and using conditional statements to test all the three fields with an if condition and the and operator to decide which activity to be started using a button and intent.
activity level_menu
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/choose_txt"
android:textSize="30sp"
android:textStyle="bold"
android:layout_gravity="center"
android:textColor="#ff000000"
android:layout_marginBottom="30dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/prog"
android:textSize="32sp"
android:textStyle="bold"
android:textColor="#ff000000"
/>
<Spinner
android:id="#+id/program_spinner"
android:layout_width="match_parent"
android:layout_height="40dp"
android:entries="#array/program_list"
android:prompt="#string/programm_prompt"
android:layout_marginBottom="30sp"
android:backgroundTint="#e9ffc1"
android:background="#e9ffc1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lev"
android:textSize="32sp"
android:textStyle="bold"
android:textColor="#ff000000"
/>
<Spinner
android:id="#+id/level_spinner"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="30sp"
android:backgroundTint="#e9ffc1"
android:background="#e9ffc1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sem"
android:textSize="32sp"
android:textStyle="bold"
android:textColor="#ff000000"/>
<Spinner
android:id="#+id/sem_spinner"
android:layout_width="match_parent"
android:layout_height="40dp"
android:backgroundTint="#e9ffc1"
android:background="#e9ffc1"
android:layout_marginBottom="20dp"></Spinner>
<Button
android:id="#+id/btnEnter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/enter"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#ff000000"
/>
level_menu.java
package inc.zibit.com.conware;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class Level_menu extends ActionBarActivity implements OnItemSelectedListener {
private Spinner program_spinner;
private Spinner level_spinner;
private Spinner sem_spinner;
private Button btnEnter;
ArrayAdapter<String> program_adapter;
List<String> program_list = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.program_list)));
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_menu);
addItemsOnSpinner2();
addItemsOnSpinner3();
addListenerOnButton();
// addListenerOnSpinnerItemSelection();
program_spinner = (Spinner) findViewById(R.id.program_spinner);
level_spinner = (Spinner) findViewById(R.id.level_spinner);
sem_spinner = (Spinner) findViewById(R.id.sem_spinner);
program_spinner.setOnItemSelectedListener(this);
level_spinner.setOnItemSelectedListener(this);
sem_spinner.setOnItemSelectedListener(this);
program_adapter=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_spinner_item, program_list);
program_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
program_spinner.setAdapter(program_adapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (!(program_spinner.getSelectedItem().toString().trim().equals("Bsc. Computer Science"))) {
if (level_spinner.getSelectedItem().toString().trim().equals("Level 100")) {
if (sem_spinner.getSelectedItem().toString().trim().equals("1st Semester")) {
startActivity(new Intent(Level_menu.this, CompScience.class));
}
}
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
// add items into spinner dynamically
public void addItemsOnSpinner2() {
level_spinner = (Spinner)findViewById(R.id.level_spinner);
List<String> list = new ArrayList<String>();
list.add("Level 100");
list.add("Level 200");
list.add("Level 300");
list.add("Level 400");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
level_spinner.setAdapter(dataAdapter);
}
public void addItemsOnSpinner3() {
sem_spinner = (Spinner)findViewById(R.id.sem_spinner);
List<String> list = new ArrayList<String>();
list.add("1st Semester");
list.add("2nd Semester");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sem_spinner.setAdapter(dataAdapter);
}
public void addListenerOnButton() {
btnEnter = (Button) findViewById(R.id.btnEnter);
btnEnter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ( v.getId()==R.id.btnEnter){
Intent intent = new Intent(Level_menu.this,CompScience.class);
startActivity(intent);
}
}
});
}
}
When i run it crashes.. what am I not doing right?
Without any logs provided, my best guess would be that NullPointerException causes your crash inside onItemSelected() callback. If any of those 3 spinners have nothing selected, you will get NPE.
I suggest you first make sure that spinner.getSelectedItem() is not null, before comparing strings.
I just want to know if this is possible since first. I have created a custom listView based on the tutorial I read from Sai Geetha. Well it works perfectly on my app except that it needs to extend ListActivity instead of FragmentActivity. Now I'm having a hard time configuring and adding a dialog for this since I need to apply a fragment dialog and I can't use the getFragmentManager() since I'm not working with the FragmentActivity. Is there's another way I can do to work on this without sacrificing the ListActivity? Thanks!
Here's my code so far
XML:
conversation_list_view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#id/android:list"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="20dp"/>
</LinearLayout>
group_screen
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="48dp"
android:background="#drawable/action_bar_separator"
android:id="#+id/relativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Group Name"
android:id="#+id/txt_group_name"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#color/dark_gray"
android:shadowColor="#color/dark_shadow"
android:shadowRadius="1"
android:shadowDy="1"/>
<Button
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/btn_back"
android:background="#drawable/btn_navigate_back"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"/>
<Button
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/btn_information"
android:background="#drawable/btn_information"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"/>
</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Conversations"
android:id="#+id/textView2"
android:textColor="#color/holo_light_blue"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="340dp"
>
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.mark.exercise.ListViewFragment"
android:id="#+id/fragment"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="42dp"
android:text="Ask something"
android:id="#+id/btn_ask_question"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:textSize="15dp"/>
</LinearLayout>
</LinearLayout>
Java
package com.mark.exercise;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
/**
* Created by pc on 9/24/13.
*/
public class GroupActivity extends FragmentActivity {
Button information, back, new_topic;
ListView conversations;
TextView group_name;
String name, group_description, group_administrator,image_id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.group_screen);
Intent intent = getIntent();
name = intent.getStringExtra("group_name");
group_description = intent.getStringExtra("group_description");
group_administrator = intent.getStringExtra("group_administrator");
image_id = intent.getStringExtra("image_id");
information = (Button)findViewById(R.id.btn_information);
back = (Button)findViewById(R.id.btn_back);
new_topic = (Button)findViewById(R.id.btn_ask_question);
group_name = (TextView)findViewById(R.id.txt_group_name);
group_name.setText(name);
information.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(GroupActivity.this, GroupInformationActivity.class);
intent.putExtra("group_name",name);
intent.putExtra("group_description",group_description);
intent.putExtra("group_administrator",group_administrator);
intent.putExtra("image_id",image_id);
startActivity(intent);
GroupActivity.this.overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
}
});
new_topic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showCreateNewTopicDialog();
}
});
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
}
private void showCreateNewTopicDialog() {
FragmentManager fm = getSupportFragmentManager();
DialogFragmentCreateGroup createGroup = new DialogFragmentCreateGroup();
createGroup.show(fm, "create_group");
}
#Override
public void onBackPressed(){
super.onBackPressed();
overridePendingTransition(R.anim.in_from_right,R.anim.out_to_left);
}
}
List Fragment
package com.mark.exercise;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
/**
* Created by pc on 9/27/13.
*/
public class ListViewFragment extends ListFragment {
final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.conversations_list_view,
container, false);
setListView set_list = new setListView();
set_list.start();
return view;
}
public void onListItemClick(ListView l, View v, int position, long id) {
//super.onListItemClick(l, v, position, id);
Intent intent = new Intent(getActivity(), ConversationActivity.class);
startActivity(intent);
getActivity().overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
}
private class setListView extends Thread {
public void run() {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
setConversations();
}
});
}
}
private void setConversations(){
list.clear();
SimpleAdapter adapter = new SimpleAdapter(
getActivity(),
list,
R.layout.custom_list_main_conversations,
new String[] {"message","date", "reply_count", "stars_count"},
new int[] {R.id.txt_conversation_message,R.id.txt_topic_date, R.id.txt_no_of_reply, R.id.txt_no_of_stars}
);
for(int ctr=0;ctr<=5;ctr++){
Random randomGenerator = new Random();
HashMap<String,String> item_list = new HashMap<String,String>();
item_list.put("message", "This is the conversation number "+(ctr+1)+" and this topic is just a dummy data.");
item_list.put("date", "0"+(ctr+1)+"/0"+(ctr+2)+"/2013 "+(ctr+1)+":00:am");
item_list.put("reply_count", String.valueOf(ctr+randomGenerator.nextInt(10)));
item_list.put("stars_count", String.valueOf(ctr+randomGenerator.nextInt(10)));
list.add(item_list);
}
android.app.ListFragment lf = new android.app.ListFragment();
lf.setListAdapter(adapter);
}
}
You can use ListFragment inside FragmentActivity instead of using a ListActivity.
I am trying to make a TO DO LIST. I have a EditText, Button, and ListView. On button click I want to add, what I typed into the EditText into a ListView.
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter task"
android:textSize="24dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/addTaskBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add Task"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Task"
android:id="#+id/header"
android:layout_below="#+id/addTaskBtn"
android:background="#5e5e5e"
android:textColor="#FFFFFF"
android:textSize="14dp"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/list"
android:layout_below="#+id/header"
android:layout_centerHorizontal="true" />
Main_ToDoList.java
package com.example.todolist;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class Main_ToDoList extends Activity implements OnClickListener
{
private Button btnAdd;
private EditText et;
private ListView lv;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
btnAdd = (Button)findViewById(R.id.addTaskBtn);
btnAdd.setOnClickListener(this);
et = (EditText)findViewById(R.id.editText);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);
lv.setAdapter(adapter);
}
public void onClick(View v)
{
String input = et.getText().toString();
if(input.length() > 0)
{
list.add(input);
adapter.notifyDataSetChanged();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main__to_do_list, menu);
return true;
}
}
The code doesn't work, new to android developing, and just trying to create a simple To Do List. Thanks for your help!
This should do it.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
btnAdd = (Button)findViewById(R.id.addTaskBtn);
btnAdd.setOnClickListener(this);
et = (EditText)findViewById(R.id.editText);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);
// set the lv variable to your list in the xml
lv=(ListView)findViewById(R.id.list);
lv.setAdapter(adapter);
}
public void onClick(View v)
{
String input = et.getText().toString();
if(input.length() > 0)
{
// add string to the adapter, not the listview
adapter.add(input);
// no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
}
}
you need to find your listview and then set the adapter:
lv=(ListView)findViewById(android.R.id.yourlistview);
lv.setAdapter(adapter);
I was adding records to my android sqlite databasr which are shown on my listview, when I click the first ones,they change their color(selected) , which is fine. but when I click the middle ones, the
last ones are marked and when I click the last ones the app "stopped unexpectedly". I guess the problem is that when i click the last ones there three records after that and thats why it jumps, but how can I solve it?
here's the catlog:
08-04 18:13:42.976: E/Database(1316): close() was never explicitly called on database '/data/data/com.example.lamder/databases/REGISTRATION_DB'
08-04 18:13:42.976: E/Database(1316): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
08-04 18:13:42.976: E/Database(1316): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1810)
08-04 18:13:42.976: E/Database(1316): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
08-04 18:13:42.976: E/Database(1316): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
and here's my code
package com.example.lamder;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class GetActivity extends Activity {
RegistrationAdapter adapter_ob;
RegistrationOpenHelper helper_ob;
SQLiteDatabase db_ob;
ListView nameList;
Button registerBtn;
Cursor cursor;
static boolean[] check=new boolean[100];
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getact);
nameList = (ListView) findViewById(R.id.lv_name);
registerBtn = (Button) findViewById(R.id.btn_register);
adapter_ob = new RegistrationAdapter(this);
cursor=adapter_ob.queryName();
Context context = getApplicationContext();
CharSequence text = "select questions by clicking on them";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
for(int i=0;i<check.length;i++)
check[i]=false;
String[] from = { helper_ob.FNAME, helper_ob.LNAME };
int[] to = { R.id.tv_fname, R.id.tv_lname };
cursor = adapter_ob.queryName();
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
R.layout.row, cursor, from, to);
nameList.setAdapter(cursorAdapter);
nameList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
if(check[position]==false)
{
check[position]=true;
parent.getChildAt(position).setBackgroundColor(Color.parseColor("#40FF00"));
}
else
{
check[position]=false;
parent.getChildAt(position).setBackgroundColor(Color.TRANSPARENT);
}
cursor.close();
}
});
registerBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{ String ques[]=new String[cursor.getCount()];
int i=0;
cursor.moveToFirst();
while(!cursor.isAfterLast() )
{
ques[i]= cursor.getString(cursor.getColumnIndex(helper_ob.FNAME));
i++;
cursor.moveToNext();
}
i=0;
String ans[]=new String[cursor.getCount()];
cursor.moveToFirst();
while(!cursor.isAfterLast() )
{
ans[i]= cursor.getString(cursor.getColumnIndex(helper_ob.LNAME));
i++;
cursor.moveToNext();
}
int counter=0;
for(int t=0;t<check.length;t++)
{
if(check[t]==true)
counter++;
}
String[] ques1=new String[counter];
String[] ans1=new String[counter];
int count=0;
for(int t=0;t<ans.length;t++)
{
if(check[t]==true)
{
ques1[count]=ques[t];
ans1[count]=ans[t];
count++;
}
}
if(counter>0)
{
Intent intent=new Intent(GetActivity.this,ThirdActivity.class);
intent.putExtra("Answer",ans1);
intent.putExtra("Question",ques1);
intent.putExtra("truecount",0);
intent.putExtra("falsecount",0);
startActivity(intent);
}
else
{
Context context = getApplicationContext();
CharSequence text = "select questions by clicking on them";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
});
}
#Override
public void onResume()
{
super.onResume();
cursor.requery();
}
}
and xml file:
<?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:background = "#drawable/backnote"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/lv_name"
android:layout_width="wrap_content"
android:layout_height="337dp"
android:layout_weight="0.63" >
</ListView>
<Button
android:id="#+id/btn_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/start" />
</LinearLayout>
and the row.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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="75dp" >
<TextView
android:id="#+id/tv_fname"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/imageView1"
android:text="ans" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/tv_lname"
android:layout_marginLeft="44dp"
android:layout_toRightOf="#+id/tv_fname" >
</RelativeLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/blackbar" />
<TextView
android:id="#+id/tv_lname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/tv_fname"
android:text="ques" />
</RelativeLayout>
</LinearLayout>
That is a database error. Go to the function where you are querying your data. then before returning the result, close your database.
It is default error of list view in android buddy. if you want to still to do so, you have to make custom list view.
see following link for that
Tutorial for Create custom list view