Getting text from custom ListView - java

As the title says really.. I have a list view with 3 text view's in it, see below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="20dp" >
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="italic"
android:typeface="sans" />
</LinearLayout>
This is how I call it.
public void displayResultList(ArrayList<HashMap<String, String>> results2) {
// TODO Auto-generated method stub
lv = (ListView) findViewById(android.R.id.list);
SimpleAdapter adapter = new SimpleAdapter(this, results2,
R.layout.custom_row_view,
new String[] { "Ear", "Breed", "Sex" }, new int[] { R.id.text1,
R.id.text2, R.id.text3 });
lv.setAdapter(adapter);
}
and this is how I try to retrieve the text, but this doesn't work..
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String item = (String) ((TextView) v).getText();
Toast toast3 = Toast.makeText(this, "items = " + item,
Toast.LENGTH_SHORT);
toast3.show();
// Intent mainIntent3 = new Intent(this,CowDetailsActivity.class);
// mainIntent3.putExtra("cow", item.substring(10, 16));
// startActivity(mainIntent3);
}
Can anybody tell me how I can get the first line of text?
Thanks in advance

Can anybody tell me how I can get the first line of text?
((HashMap<String, String>)l.getItemAtPosition(position) will give you your HashMap. I don't know what "the first line of text" is.
Or, if you still have results2 held somewhere, results2.get(position) will also return to you your HashMap.

Related

Listview adapter not displaying list columns after closing of Dialog

Setup
In my app I have a section that creates rows of data to be saved in a database. Each row is built from a textbox, number box then "set" into a listview for display. Later on the user can open a "plan" created before and review the data.
I created a listview, a custom layout for the display line and figured out how to add/display each new row. However, when I perform the open, I create a dialog to display the list for plans, user selects on and the rows should display. They do not. I trace through the code and I see the arraylist for the adapter being populated and I call the notifydatasetchanged, but the main listview remains empty.
I am not using a customer adapter class, but just the Simple adapter.
This is the beginning of the Activity. I am still learning android coding so I tend to use generic names as I learn.
public class TrackItEqMainActivity extends AppCompatActivity {
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
public int rowCount = 1;
private static final String eTAG = "Exception";
final Context context = this;
// global variables needed for processing the add gaits display
**private ListView lstGaits;
private ListAdapter adapter;
private ArrayList<HashMap<String, String>> mylist;**
// *****
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(eTAG, "onCreate Build");
setTitle(R.string.activityManagePlans);
setContentView(R.layout.activity_track_it_eq_build);
Toolbar mySecondaryToolbar = (Toolbar) findViewById(R.id.my_secondaryToolbar);
setSupportActionBar(mySecondaryToolbar);
setActivityBuildListeners();
// set the objects needed to handle the dynamic gaits list
ListView lstGaits = (ListView) findViewById(R.id.lstMyGaits);
mylist = new ArrayList<HashMap<String, String>>();
try {
adapter = new SimpleAdapter(this, mylist, R.layout.gaits_detail,
new String[] { "keyGait", "keyTime" }, new int[] {
R.id.txtdisplayGait, R.id.txtdisplayTime });
lstGaits.setAdapter(adapter);
} catch (Exception e) {
Log.i(eTAG, e.getMessage());
}
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
SO when a user taps a new button a set of controls appear allowing to select a gait and enter a time. They then tap on the set button and in the set onClick, I add the data to the internal array and display the custom layout on the screen.
private Button.OnClickListener onClick_btnSet = new OnClickListener() {
#Override
public void onClick(View v) {
Spinner spinner = (Spinner) findViewById(R.id.spinner_gaits);
TextView textGait = (TextView) spinner.getSelectedView();
String gtResult = textGait.getText().toString();
TextView txtTime = (TextView) findViewById(R.id.txtSelected);
String tmeResult = txtTime.getText().toString();
buildDisplayLine(true,gtResult,tmeResult);
txtTime.setText("");
spinner.requestFocus();
}
};
The function that builds the line and adds to the array is this:
private void buildDisplayLine(boolean isNew, String gait, String time) {
HashMap<String, String> map2 = new HashMap<String, String>();
map2.put("keyGait",gait); // put the col data in
map2.put("keyTime", time); // put the col data in
mylist.add(map2);
if (isNew) {
((BaseAdapter) (adapter)).notifyDataSetChanged();
}
}
All that works although a strange side effect is that the list will not display if the number keypad is visible. When I clear it off the list appears.
Ifa use wants to look at or change a plan they tap the open button. IT display a dialog box that shows a list of plans. Tap on a plan and all the elements should display:
private final ThreadLocal<OnClickListener> onClick_btnOpen = new ThreadLocal<OnClickListener>() {
#Override
protected OnClickListener initialValue() {
return new OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.activity_track_it_eq_open_plan);
dialog.setTitle("Open Exercise Plan...");
Button diaCancelButton = (Button) dialog.findViewById(R.id.btnCancelOpen);
diaCancelButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
// get a list of files from the local app plans
final eqDatabaseService eqDB = new eqDatabaseService(context, 2);
ArrayList<HashMap<String,String>> allPlans = eqDB.getPlanList();
if (allPlans.isEmpty()) {
Toast toast = new Toast(context);
toast.setGravity(Gravity.TOP, 0, 0);
Toast.makeText(context, "No Files to open, Please create one!", Toast.LENGTH_LONG).show();
return;
}
ListView lvPlan = (ListView) dialog.findViewById(R.id.lvPlans);
try {
adapter = new SimpleAdapter(context, allPlans, R.layout.plans_columns,
new String[] { "keyName", "keyENum" }, new int[] {
R.id.txtPlanName, R.id.txtNumElements });
lvPlan.setAdapter(adapter);
} catch (Exception e) {
}
//********************************************************
lvPlan.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
TextView txtplan = (TextView) v.findViewById(R.id.txtPlanName);
String planName = txtplan.getText().toString();
List<eqSessions_dt> myPlan = eqDB.getCurrentPlan(planName);
clearGrid();
for (eqSessions_dt row : myPlan) {
buildDisplayLine(false, row.get_gait(),Integer.toString(row.get_time()));
}
((BaseAdapter)(adapter)).notifyDataSetChanged();
dialog.dismiss();
}
});
dialog.show();
}
};
}
};
I have played around with this, did look at other similar questions and answers, but didn't see anything that would explain why the notify would not work. I avoided doing a custom adapter, trying to keep this simple at first. What am I missing that stops the list from displaying. I see the arraylist being filled and all related objects are global to the activity. I'll keep playing around, but I hope otehr eyes see what I miss.
This is the activity xml`
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
tools:context="com.newproject.jhull3341.trackiteq.TrackItEqMainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/my_secondaryToolbar"
android:layout_width="fill_parent"
android:layout_height="?attr/actionBarSize"
android:background="#236dc1"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/lyFileAction">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="New"
android:id="#+id/btnNew"
android:layout_row="0"
android:layout_column="0"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="Open"
android:id="#+id/btnOpen"
android:layout_row="0"
android:layout_column="1"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="Save"
android:id="#+id/btnSave"
android:layout_row="0"
android:layout_column="2"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="Delete"
android:id="#+id/btnDelete"
android:layout_row="0"
android:layout_column="3"
android:layout_gravity="center_horizontal" />
</GridLayout>
<GridLayout
android:layout_width="367dp"
android:layout_height="wrap_content"
android:id="#+id/grdEntry">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/label_time"
android:id="#+id/lblTime"
android:layout_row="0"
android:layout_column="1"
android:layout_marginBottom="5dp" />
<Spinner
android:layout_width="147dp"
android:layout_height="wrap_content"
android:id="#+id/spinner_gaits"
android:layout_row="1"
android:layout_column="0"
android:focusable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/label_Gait"
android:id="#+id/lblGait"
android:layout_row="0"
android:layout_column="0"
android:layout_marginBottom="5dp" />
<EditText
android:layout_width="124dp"
android:layout_height="wrap_content"
android:id="#+id/txtSelected"
android:layout_row="1"
android:layout_column="1"
android:layout_gravity="fill_horizontal|center_vertical"
android:singleLine="true"
android:numeric="integer" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="fill_parent"
android:text="Set"
android:id="#+id/btnSet"
android:layout_row="1"
android:layout_column="2"
android:layout_gravity="center_vertical"
android:clickable="true" />
</GridLayout>
<ListView
android:id="#+id/lstMyGaits"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_below="#+id/grdEntry" />
</LinearLayout>
this is the custom layout for each display row.`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtdisplayGait"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="TextView"
android:textSize="18sp" />
<TextView
android:id="#+id/txtdisplayTime"
android:layout_width="80dp"
android:layout_height="44dp"
android:gravity="center_vertical"
android:text="TextView"
android:textSize="18sp" />
<ImageButton
android:id="#+id/btnRemoveLine"
android:layout_width="25dp"
android:layout_height="44dp"
android:layout_weight="0.39"
android:background="#color/wallet_bright_foreground_holo_dark"
android:elevation="1dp"
android:src="#android:drawable/btn_dialog" />
</LinearLayout>`

act on the selected item in ListView

im have a layout named type_of_dairy with below code
<?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:layout_marginTop="20dp"
android:layoutDirection="rtl"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Type"
android:id="#+id/iceCreamType"
android:layout_gravity="center_horizontal"
android:layout_weight="0.05"
android:layout_marginTop="20dp"/>
<ListView
android:layout_width="match_parent"
android:layout_height="35dp"
android:id="#+id/listView"
android:layout_weight="0.26"
android:background="#999900"
android:choiceMode="singleChoice"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Flavour"
android:id="#+id/iceCreamFlavor"
android:layout_gravity="center_horizontal"
android:layout_weight="0.05"
android:layout_marginTop="10dp"/>
<ListView
android:layout_width="match_parent"
android:layout_height="58dp"
android:id="#+id/listView2"
android:layout_gravity="center_horizontal"
android:background="#999900"
android:choiceMode="singleChoice"
/>
<EditText
android:layout_width="89dp"
android:layout_height="wrap_content"
android:inputType="number"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:ems="10"
android:id="#+id/editText"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apply"
android:id="#+id/countOficeCream"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_weight="0.05" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/iceCreamReject"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_weight="0.05" />
</LinearLayout>
as you see i have two ListView, i have filled out the first ListView from my DB, a field of a table of my DB has selected to fill in this ListView(#+id/listView) and there are two buttons,i want that as i selected an item from the first ListView(#+id/listView) , the second ListView (#+id/listView2) fill out with another field of my DB,
public class Dairy extends Activity {
Button returnBack;
Statement statement;
Statement statement2;
ListView dairyType;
ListView dairyFlavour;
ArrayList<String> typeArray;
ArrayList<String> dairyArray;
ArrayAdapter<String> typeAdapter;
ArrayAdapter<String> flavorAapter;
String typeString, flavourString;
private Object view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.type_of_dairy);
dairyType = (ListView) findViewById(R.id.listView);
dairyFlavour = (ListView) findViewById(R.id.listView2);
typeArray = new ArrayList<String>();
typeAdapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_single_choice, typeArray);
flavorAapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_single_choice, dairyArray);
try
{
ConnectionHelper connectionHelper = new ConnectionHelper();
statement = connectionHelper.getConnection().createStatement();
ResultSet resultSet = statement.executeQuery("select Name from tblProductCategory where TypeId=1");
if(resultSet.next()) {
do {
typeString=resultSet.getString(resultSet.findColumn("Name"));
typeArray.add(typeString);
dairyType.setAdapter(typeAdapter);
}while(resultSet.next());
}
}
catch (SQLException e)
{
}
dairyType.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try{
ConnectionHelper connectionHelper = new ConnectionHelper();
statement2 = connectionHelper.getConnection().createStatement();
ResultSet resultSet = statement2.executeQuery("select username from tblUser");
if(resultSet.next())
{
do {
flavourString = resultSet.getString(resultSet.findColumn("UserName"));
dairyArray.add(flavourString);
dairyFlavour.setAdapter(flavorAapter);
} while (resultSet.next());
}
}catch (SQLException e)
{
Toast.makeText(getApplicationContext(), "Problem detected", Toast.LENGTH_LONG).show();
}
}
});
i want that as i select an item from first ListView(dairyType), some data from another table of my DB be selected and shown in second ListView(dairyFlavour), and this part i said this :
dairyType.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try{
ConnectionHelper connectionHelper = new ConnectionHelper();
statement2 = connectionHelper.getConnection().createStatement();
ResultSet resultSet = statement2.executeQuery("select username from tblUser");
if(resultSet.next())
{
do {
flavourString = resultSet.getString(resultSet.findColumn("UserName"));
dairyArray.add(flavourString);
dairyFlavour.setAdapter(flavorAapter);
} while (resultSet.next());
}
}catch (SQLException e)
{
Toast.makeText(getApplicationContext(), "Problem detected", Toast.LENGTH_LONG).show();
}
}
});
but i get myAppclication stopped in my android device and javaNullPointExecption in android studio,
However by this connection to DB i have shown the firstListViewand Loged into the App.
From your given code, It seems you forgot to initialise the "dairyArray".
Initialise it first , then add Strings to it.
You might be getting error on "dairyArray.add(flavourString);" , as dairyArray is not initialised.
I hope it will work.
As my Friend #harish said i didnt assign dairyArray in my code, so that related adapter returns nothing,
But i act on the selected item via this code :
public void onClick(View v) {
String str;
str=flavorAapter.getItem(dairyFlavour.getCheckedItemPosition()).toString();
And then return the str.

NullPointerException when setting the Event Handler for Spinner

I am having some issues with the android Spinner. Please have a look at the below codes.
talk_settings.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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="17dp"
android:text="#string/language_locale"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/language_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_toRightOf="#+id/textView1"
android:layout_alignBaseline="#+id/textView1"
android:entries="#array/locale_arrays"
android:prompt="#string/locate_prompt"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/spinner1"
android:layout_marginTop="37dp"
android:text="#string/pitch"
android:textAppearance="?android:attr/textAppearanceMedium" />
<SeekBar
android:id="#+id/pitchBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_marginTop="16dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/pitchBar"
android:layout_marginTop="27dp"
android:text="#string/speed"
android:textAppearance="?android:attr/textAppearanceMedium" />
<SeekBar
android:id="#+id/speedBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView3"
android:layout_marginTop="18dp" />
</RelativeLayout>
locale_string.xml (string resources for the spinner)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="locate_prompt">Select Language</string>
<string-array name = "locale_arrays">
<item>English</item>
<item>Chinese</item>
<item>French</item>
<item>Germany</item>
<item>Italian</item>
<item>Japanese</item>
<item>Korean</item>
</string-array>
</resources>
Java Code
//Event Handler for the language spinner
private class LanguageSpinnerHandler implements OnItemSelectedListener
{
int result = 0;;
#Override
public void onItemSelected(AdapterView<?> parent, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
if(parent.getItemAtPosition(position).toString()=="English")
{
result = tts.setLanguage(Locale.UK);
}
else if(parent.getItemAtPosition(position).toString()=="Chinese")
{
result = tts.setLanguage(Locale.CHINESE);
}
else if(parent.getItemAtPosition(position).toString()=="French")
{
result = tts.setLanguage(Locale.FRENCH);
}
else if(parent.getItemAtPosition(position).toString()=="Germany")
{
result = tts.setLanguage(Locale.GERMANY);
}
else if(parent.getItemAtPosition(position).toString()=="Italian")
{
result = tts.setLanguage(Locale.ITALIAN);
}
else if(parent.getItemAtPosition(position).toString()=="Japanese")
{
result = tts.setLanguage(Locale.JAPANESE);
}
else
{
result = tts.setLanguage(Locale.KOREAN);
}
if(result==TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
{
Toast.makeText(Talk.this, "This Language is Not Supported in Your Device", Toast.LENGTH_LONG).show();
tts.setLanguage(Locale.UK);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
//show settings
private void showSettings()
{
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.talk_settings);
dialog.setTitle("Settings");
dialog.setCancelable(true);
SeekBar pitchBar = (SeekBar)dialog.findViewById(R.id.pitchBar);
SeekBar speakingSpeedBar = (SeekBar)dialog.findViewById(R.id.speedBar);
pitchBar.setProgress((int) pitchValue);
speakingSpeedBar.setProgress((int)speakingSpeedValue);
pitchBar.setOnSeekBarChangeListener(new PicthBarEvent());
speakingSpeedBar.setOnSeekBarChangeListener(new SpeakingSpeedBarEvent());
Spinner languageSpinner = (Spinner)findViewById(R.id.language_spinner);
languageSpinner.setOnItemSelectedListener(new LanguageSpinnerHandler());
dialog.show();
}
I am getting a NullPointerException right in here
languageSpinner.setOnItemSelectedListener(new LanguageSpinnerHandler());
Where I went wrong?
languageSpinner belongs to talk_settings.xml, so you have to look for it inside the dialog view
Spinner languageSpinner = (Spinner)dialog.findViewById(R.id.language_spinner);
also, String comparison in Java should be performed through the equals or equalsIgnoreCase method
Check if languageSpinner is initialized properly.
Spinner languageSpinner = (Spinner)dialog.findViewById(R.id.language_spinner);
findViewById looks for a a view in the current inflated layout. So use the dialog object to initialize spinner
and use .equals to compare strings
parent.getItemAtPosition(position).toString().equals("English")
Try usion
Spinner languageSpinner = (Spinner)dialog.findViewById(R.id.language_spinner);
instead of
Spinner languageSpinner = (Spinner)findViewById(R.id.language_spinner);.
As your spinner is in the talk_settings XML you will need to find the Spinner id in the view in which the XML is inflated.

android listview is iterating through the footer menus too

This is my activity code:
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
String[] names = new String[] { "Investor Relations", "Social Sharing", "Rate this app", "About Rathbones",
"Disclaimer", "Security", "Credits"};
// Create an ArrayAdapter, that will actually make the Strings above
// appear in the ListView
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.moremenulist,
R.id.label, names));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG).show();
}
And here is the xml file for this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/LinearLayout1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:id="#+id/label"></TextView>
<LinearLayout android:id="#+id/linearLayout2" android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentBottom="true">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton android:layout_width="wrap_content" android:id="#+id/imgbtn1" android:layout_alignParentLeft="true" android:layout_alignBottom="#+id/imgbtn3" android:layout_height="wrap_content" android:src="#drawable/topnews" android:visibility="visible"></ImageButton>
<ImageButton android:layout_width="wrap_content" android:layout_alignParentRight="true" android:id="#+id/imgbtn5" android:layout_alignBottom="#+id/imgbtn4" android:layout_height="wrap_content" android:src="#drawable/more"></ImageButton>
<ImageButton android:layout_width="wrap_content" android:id="#+id/imageButton1" android:layout_height="wrap_content" android:src="#drawable/contact_us" android:layout_alignParentTop="true" android:layout_toRightOf="#+id/imgbtn1"></ImageButton>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
Now, when the activity is loading it is appending the menu with each item in the list. I just want it to appear only in bottom not with each and every list item.
any suggestions?
Edit-1 :
I tried this but getting an error of nullpointer exception
View footerView =
((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer, null, false);
ListView lv = new ListView(this);
lv.addFooterView(footerView);
What do you mean by menu? Do you mean your ImageButtons?
If that's the case then remove those create another layout for those ImageButtons and add them using ListView.addFooterView() instead.
You could create e.g. list_footer_layout.xml, which contains your ImageButtons. Then:
public void onCreate(Bundle bundle) {
...
ListView lv = getListView(); // If your activity is ListActivity
View foot = getLayoutInflater().inflate(R.layout.list_footer_layout, null);
lv.addFooterView(foot);
}

Can't seem to pull the text from my list item

I have this code and I'd really liek to be able to get the text otu of the item in the listview. I've done an example where i can get it to toast in a different type of listview, but not this one that i made. any helpers?
thx in advance :)
private void listChoices(){
mySQLiteAdapterChoices = new SQLiteAdapterChoices(this);
mySQLiteAdapterChoices.openToRead();
Cursor cursor = mySQLiteAdapterChoices.queueAll();
startManagingCursor(cursor);
String choic=SQLiteAdapterChoices.KEY_CHOICE_SUB;
String[] from = new String[]{choic};
int[] to = new int[]{R.id.textUserChoice};
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this, R.layout.userrow, cursor, from, to);
setListAdapter(cursorAdapter);
mySQLiteAdapterChoices.close();
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch(position) {
case 0:
final String text = ("txtUserChoice");
Toast.makeText(getApplicationContext(),
text,
Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),
First.class));
break;
...
XML from createcontrol.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="#+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ListView
android:id="#android:id/list"
android:layout_width="305px"
android:layout_height="270px"
android:layout_x="8px"
android:layout_y="10px"
>
</ListView>
<EditText
android:id="#+id/txtEdt"
android:layout_width="185px"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp"
android:autoText="true"
android:capitalize="words"
android:layout_x="8px"
android:layout_y="293px"
>
</EditText>
<Button
android:id="#+id/btnNewItem"
android:layout_width="107px"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_x="199px"
android:layout_y="295px"
>
</Button>
</AbsoluteLayout>
xml from userrow.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="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="#drawable/icon" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textUserChoice"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
/>
</LinearLayout>
</LinearLayout>
Fixed Code Below.. I moved some of the variables up a couple scopes and changed the code to this
case 0:
int index = cursor.getColumnIndex(choic);
final String text = cursor.getString(index);
Toast.makeText(getApplicationContext(),
text,
Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),
First.class));
break;
case 1:....
I think the problem is that you forgot to fetch the data from cursor. You wrote
final String text = ("txtUserChoice");
and you should write like
int index = cursor.getColumnIndex(choic);
final String text = cursor.getString(index);
and make cursor and choic variable as instance member.
This will help you to extract the string content from the selected item from the ListView:
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String selection = listview.getItemAtPosition(arg2).toString();
Toast.makeText(getApplicationContext(), "you have selected "+selection, 5000).show();
}
});

Categories

Resources