I am adding the View to the Header of ListView in the ListActivity, after inflating which has AutoCompleteTextView and not showing the suggestions on entering text. Though it works absolutely fine in normal case but not working fine when added to Header of ListView.
header_route.xml :
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/lblride_From"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtFrom"
android:layout_alignParentTop="true"
android:text="#string/lblride_Route"
/>
<AutoCompleteTextView
android:id="#+id/txtFrom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/lblride_From"
android:layout_marginLeft="20dp"
android:layout_marginTop="2dp"
android:completionThreshold="1"
android:ems="10"
android:width="290dp"
android:imeOptions="actionGo"
android:popupBackground="#android:color/black" >
<requestFocus />
</AutoCompleteTextView>
<Button
android:id="#+id/hdbtnOkRoute"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtFrom"
android:layout_below="#+id/txtFrom"
android:layout_marginTop="2dp"
android:minHeight="40dp"
android:text="#string/txtBtnOk"
android:width="290dp" />
</RelativeLayout>
public class BusRouteActivity extends ListActivity implements TextWatcher {
AutoCompleteTextView actvFrom;
Button buttonOK;
TextView txtViewRouteItem;
ListView lstViewRoute;
ArrayAdapter<String> listAdapter;
DataBaseHelper databaseHelper;
TextView tvFooter;
Context appContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.nameBusRouteActivity);
appContext = getApplicationContext();
lstViewRoute = getListView();
LayoutInflater inflater = (LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.header_route, null);
DataBaseHelper dbh = new DataBaseHelper(getApplicationContext());
try {
dbh.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dbh.openDataBase();
databaseHelper = dbh;
actvFrom = (AutoCompleteTextView) view.findViewById(R.id.txtFrom);
actvFrom.addTextChangedListener(this);
actvFrom.setOnEditorActionListener(actionListener);
ArrayAdapter<String> aroutes = new ArrayAdapter<String>(appContext, R.layout.routeitem, dbh.GetRoutes());
actvFrom.setAdapter(aroutes);
buttonOK = (Button)view.findViewById(R.id.hdbtnOkRoute);
buttonOK.setOnClickListener(OkOnClickListener);
tvFooter = new TextView(this);
tvFooter.setTextAppearance(getApplicationContext(), R.style.footerStyle);
String text = getResources().getString(R.string.footer);
tvFooter.setText(Html.fromHtml(text));
listAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.routeitem);
lstViewRoute.addHeaderView(view, null, false);
lstViewRoute.addFooterView(tvFooter, null, true);
lstViewRoute.setClickable(true);
lstViewRoute.setSelected(true);
lstViewRoute.setAdapter(listAdapter);
}
Please let me know if I am missing any setting? Thanks in advance.
Here is one code that might help you!!
http://www.javatpoint.com/android-autocompletetextview-example
Related
I am using a custom CursorAdapter and have values in DB but the problem is that the values are not displaying in the custom ListView. I searched in SO but could not find my answer.
By debugging I found out that the 2 methods in cursor adapter bindView() and the newView() are not executing but the constructor is executing.I am not sure what is happening overall. So my question is why are the ListView items not getting displayed ?
Here is my code and I am only posting relevant code so if there is any additional code needed please comment so that I will edit accordingly.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//a listview object
notesView = (ListView)findViewById(R.id.listView);
//an object of SQLiteOpenHelper class
dbhelper = new DataBaseHelper(this);
//cursor object
passCursor = dbhelper.fetchAllNotes();
// the custom cursor adapter class object
dataCursor = new CustomCursor(this,passCursor);
notesView.setAdapter(dataCursor);
notesView.setOnItemClickListener(this);
bar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(bar);
}
Here is the CursorAdapter source code:
public class CustomCursor extends CursorAdapter {
private static final String NOTE_TITLE = "title";
private static final String RECORD_ID = "_id";
private static final String RECORD_DATE = "date";
private static final String DELETE_FLAG="deleteflag";
LayoutInflater inflater;
TextView tv, recordID, dateET;
LinearLayout ll;
String getText, existsRecordID;
long datevalue;
SimpleDateFormat dateFormatter;
String listViewHeight;
Context cont;
int getDeleteFlag;
String listHeightValue;
LinearLayout.LayoutParams params;
Cursor getCursor;
CustomCursor(Context context, Cursor c) {
super(context, c, 0);
cont= context;
getCursor= c;
//inflater = LayoutInflater.from(context);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
dateFormatter = new SimpleDateFormat("dd-MM-yyyy HH:mm");
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.customlistview, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
getDeleteFlag = cursor.getInt(cursor.getColumnIndex(DELETE_FLAG));
ll = (LinearLayout)view.findViewById(R.id.listViewLayout);
setListViewHeight(ll,context);
getText = cursor.getString(cursor.getColumnIndex(NOTE_TITLE));
existsRecordID = cursor.getString(cursor.getColumnIndex(RECORD_ID));
datevalue = cursor.getLong(cursor.getColumnIndex(RECORD_DATE));
Date newdate = new Date(datevalue);
recordID = (TextView) view.findViewById(R.id.recordID);
tv = (TextView) view.findViewById(R.id.content);
dateET = (TextView) view.findViewById(R.id.date);
tv.setText(getText.trim());
recordID.setText(existsRecordID);
dateET.setText(dateFormatter.format(newdate));
}
}
EDIT 1
Here is the main layout,
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragplacement"
xmlns:android="http://schemas.android.com/apk/res/android">
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:gravity="top|left"
android:descendantFocusability="blocksDescendants"
tools:context="com.random.simplenotes.MainActivity">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"
>
</android.support.v7.widget.Toolbar>
<ListView
android:layout_width="match_parent"
android:clickable="true"
android:layout_height="match_parent"
android:id="#+id/listView"
android:scrollbars="vertical"
/>
</LinearLayout>
</FrameLayout>
Here is the layout for the listView item,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:background="#color/PeachPuff"
android:id="#+id/listViewLayout"
android:layout_margin="7dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Here is the content.."
android:gravity="center"
android:id="#+id/content"
android:textColor="#color/black"
android:focusable="false"
android:ellipsize="end"
android:lines="1"
android:maxLines="1"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="12/09/15"
android:layout_margin="10dp"
android:focusable="false"
android:id="#+id/date" />
<TextView
android:layout_width="1dp"
android:layout_height="1dp"
android:visibility="gone"
android:focusable="false"
android:id="#+id/recordID" />
</LinearLayout>
Code for the method setListViewHeight()
private void setListViewHeight(LinearLayout ll, Context con) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(con);
listHeightValue = sp.getString(con.getResources().getString(R.string.listViewHeightkey),Constants.DEFAULT_VALUE_LISTVIEW_HEIGHT);
switch (listHeightValue)
{
case "Tiny":
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,200);
ll.setLayoutParams(params);
break;
case "Medium":
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,220);
ll.setLayoutParams(params);
break;
case "Large":
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,250);
ll.setLayoutParams(params);
break;
default:
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,150);
ll.setLayoutParams(params);
}
}
The problem is your haven't set the orientation of the LinearLayout in your main layout, so it defaults to horizontal.
This means the ListView is placed next to the Toolbar, but the width of the Toolbar is set to match_parent, so there is no room left for the ListView.
Add the following attribute to the LinearLayout in activity_main.xml, so the ListView will be placed below the Toolbar:
android:orientation="vertical"
Also, the following call might need to be removed from bindView():
setListViewHeight(ll,context);
The height of the ListView is already set properly in XML, this call might mess it up (i can only assume, since you haven't posted the implementation of setListViewHeight()).
Check if your Cursor is empty... if you are sure that it is not empty then for patching call the following code in the last line in out Activity -> onCreate() ...
dataCursor.notifyDataSetChanged();
so I have an app with 3 fragments. One of the fragments involves adding characters to a listview and then giving them weapons and abilities. Weapons and abilities are given to a characters via a custom dialog that appears when the Add button is clicked.
This dialog contains a spinner with a predetermined list of names of characters and then 3 edit-texts that require the user to enter the number of abilities that the characters is supposed to have.
My problem is that the spinner in this dialog is not displaying its contents.
Everytime I click on the dialog I get this :
W/InputEventReceiver﹕ Attempted to finish an input event but the input event receiver has already been disposed.
Rewards.java
public class Rewards extends Fragment {
private Button button;
Context mContext;
int lesserCount;
int greaterCount;
int exaltedCount;
Dialog addCharDialog;
Dialog lesserDialog;
Dialog greaterDialog;
Dialog exaltedDialog;
Spinner SpnLesser1;
Spinner SpnLesser2;
Spinner SpnLesser3;
Spinner SpnGreater1;
Spinner SpnGreater2;
Spinner SpnExalted;
Spinner SpnChar;
ArrayList<Character> chars = new ArrayList();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_rewards, container, false);
View addCharDialogView = inflater.inflate(R.layout.character_add, container, false);
button = (Button) rootView.findViewById(R.id.btnadd);
mContext = getActivity();
// custom dialogs
createAddDialog(addCharDialogView);
createLesserDialog();
createGreaterDialog();
createExaltedDialog();
SpnLesser1 = (Spinner) rootView.findViewById(R.id.lesserspinner1);
SpnLesser2 = (Spinner) rootView.findViewById(R.id.lesserspinner2);
SpnLesser3 = (Spinner) rootView.findViewById(R.id.lesserspinner3);
SpnGreater1 = (Spinner) rootView.findViewById(R.id.greaterspinner1);
SpnGreater2 = (Spinner) rootView.findViewById(R.id.greaterspinner2);
SpnExalted = (Spinner) rootView.findViewById(R.id.exaltedspinner);
return rootView;
}
public View createAddDialog(View addCharDialogView) {
LayoutInflater inflater = LayoutInflater.from(this.getActivity());
addCharDialog = new Dialog(mContext);
addCharDialog.setContentView(R.layout.character_add);
addCharDialog.setTitle("Add a character");
SpnChar = (Spinner) addCharDialogView.findViewById(R.id.dialog_spinner);
ArrayAdapter<CharSequence> dataAdapter = ArrayAdapter.createFromResource(getActivity().getBaseContext(),R.array.item_array, R.layout.spinner_layout);
//ArrayAdapter<CharSequence> dataAdapter = ArrayAdapter.createFromResource(addCharDialogView.getContext(),R.array.item_array, R.layout.spinner_layout);
dataAdapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
SpnChar.setAdapter(dataAdapter);
//spinner is populated but it's not displaying and won't do anything when clicked on
SpnChar.setSelection(2);
String Text = SpnChar.getSelectedItem().toString();
Log.d("2", Text);
Button dialogOkButton = (Button) addCharDialog.findViewById(R.id.dialogOK);
// if button is clicked, close the custom dialog
dialogOkButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
lesserCount = R.id.lessertext;
greaterCount = R.id.greatertext;
exaltedCount = R.id.exaltedtext;
Character c;
if (lesserCount > 0) {
lesserDialog.show();
}
if (greaterCount > 0) {
greaterDialog.show();
}
if (exaltedCount > 0) {
exaltedDialog.show();
}
}
});
Button dialogCancelButton = (Button) addCharDialog.findViewById(R.id.dialogCancel);
// if button is clicked, close the custom dialog
dialogCancelButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
addCharDialog.dismiss();
}
});
// add button listener
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
addCharDialog.show();
}
});
return addCharDialogView;
}
}
}
character_add.xml
<?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">
<!--<TextView-->
<!--android:id="#+id/dialog_label"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_width="fill_parent"-->
<!--android:hint="Please select an option"-->
<!--/>-->
<Spinner
android:id="#+id/dialog_spinner"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<Button
android:id="#+id/dialogOK"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="OK"
android:layout_marginTop="113dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/dialogCancel"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_alignTop="#+id/dialogOK"
android:layout_toRightOf="#+id/dialogOK" />
<EditText
android:layout_width="60dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/lessertext"
android:layout_below="#+id/dialog_spinner"
android:layout_toRightOf="#+id/editText8"
android:text="0" />
<EditText
android:layout_width="60dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/greatertext"
android:layout_below="#+id/dialog_spinner"
android:layout_toRightOf="#+id/editText9"
android:text="0" />
<EditText
android:layout_width="60dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/exaltedtext"
android:layout_below="#+id/dialog_spinner"
android:layout_toRightOf="#+id/editText10"
android:text="0" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText8"
android:layout_below="#+id/dialog_spinner"
android:layout_alignParentStart="true"
android:text="L" />
<EditText
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="#+id/editText9"
android:layout_below="#+id/dialog_spinner"
android:layout_toRightOf="#+id/lessertext"
android:text="G" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText10"
android:layout_below="#+id/dialog_spinner"
android:layout_toRightOf="#+id/greatertext"
android:text="E" />
If I understand correctly, you want a spinner that can display the input the user types in? If so, you could use an Arraylist and create a method that will destroy your spinner and recreate it every time the contents changes. You could set it up as follows:
ArrayList<String> thing = new ArrayList<String>();
ViewGroup layout = null;
layout = (ViewGroup) spinner.getParent();
public void addSpinner(ArrayList thing){
layout.removeView(spinner);
layout.addView(spinnersy2);
spinner =(Spinner) findViewById(R.id.spinner1);
ArrayAdapter <String>adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, thing);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
Create a second method that will add the user input into the array.
This is my first app. I had been setting a preference in an activity (with a fragment), but I want to move the fragment to a DialogPreference. It migrated okay, except my text colors have gone weird. See picture. The text should just be black, not white and gray. Any idea what I got wrong?
(Sorry, I know this is pretty sloppy code)
How it looks now:
My xml...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relLay_dialog_semester_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<TextView
android:id="#+id/select_semester_prompt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/select_semester_prompt" />
<LinearLayout
android:id="#+id/linLayGradeSum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/select_semester_prompt"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" >
<Spinner
android:id="#+id/semester_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="#string/hint_editSemester" />
<Spinner
android:id="#+id/year_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4" />
</LinearLayout>
<TextView
android:id="#+id/active_semesters_str"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linLayGradeSum"
android:layout_below="#+id/linLayGradeSum"
android:text="#string/active_semester_str"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:id="#+id/active_semesters_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/active_semesters_str"
android:layout_below="#+id/active_semesters_str"
android:layout_marginTop="10dp"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
My extended DialogPreference class
public class SemesterDialogPreference extends DialogPreference {
SharedPreferences settings;
public static int YEAR_DEFAULT_VALUE = Activity_AddCourse.thisYear - Activity_AddCourse.baseYear;
public static int SEASON_DEFAULT_VALUE = Semester.getDefaultSemester().getSeasonInt();
public static int SEMESTER_DEFAULT_ID = 1;
String semester_id_key;
DBAdapter db;
// this is where I do everything view-related
#Override
public void onBindDialogView(View view){
settings = getSharedPreferences();
int curSemesterId = settings.getInt(semester_id_key, 1);
db = new DBAdapter(this.getContext().getApplicationContext());
db.open();
Semester curSemester = db.getSemester(curSemesterId);
db.close();
ArrayList<String> years = new ArrayList<String>();
for (int i = Activity_AddCourse.baseYear; i <= Activity_AddCourse.maxYear; i++)
{
years.add(Integer.toString(i));
}
ArrayAdapter<String> adapter_year = new ArrayAdapter<String>(this.getContext().getApplicationContext(),
android.R.layout.simple_spinner_item, years);
Spinner spinYear = (Spinner)view.findViewById(R.id.year_spinner);
adapter_year.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinYear.setAdapter(adapter_year);
spinYear.setSelection(curSemester.getYear() - Activity_AddCourse.baseYear);
Spinner spinner = (Spinner) view.findViewById(R.id.semester_spinner);
ArrayAdapter<CharSequence> adapter_spinner = ArrayAdapter.createFromResource(this.getContext().getApplicationContext(),
R.array.semester_spinner, android.R.layout.simple_spinner_item);
adapter_spinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter_spinner);
spinner.setSelection(Semester.seasonStringToInt(curSemester.getSeason()));
db = new DBAdapter(this.getContext().getApplicationContext());
db.open();
Semester[] s = db.getAllSemesters();
db.close();
String activeSem;
LinearLayout semesterList = (LinearLayout) view.findViewById(R.id.active_semesters_list);
if(s != null){
for(int i = 0; i < s.length; i++){
activeSem = s[i].getSemesterString();
TextView tv = (TextView) new TextView(this.getContext().getApplicationContext());
tv.setText(activeSem);
semesterList.addView(tv);
System.out.println(s[i].getSemesterString());
}
}
super.onBindDialogView(view);
}
public SemesterDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.dialog_set_semester);
}
#Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
persistBoolean(positiveResult);
}
}
UPDATE:
The DialogPreference pops up from my settings activity, as shown below.
public class SettingsActivity extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
Also, I was able to get the spinner text to black by making my own xml layouts for them, just as Huy Tran suggested. However, I had to make one for each the spinner_item and the spinner_dropdown_item. See below.
ArrayAdapter<String> adapter_year = new ArrayAdapter<String>(this.getContext().getApplicationContext(),
R.layout.my_spinner_item, years);
Spinner spinYear = (Spinner)view.findViewById(R.id.year_spinner);
adapter_year.setDropDownViewResource(R.layout.my_spinner_dropdown_item);
Why did I have to do this? I don't know. Also, dropdown items are each now about 50% taller. I copied exactly from source code into my own xml and it still renders differently. Very weird.
Create a layout like below and use that layout in setDropDownViewResource instead of android.R.layout.simple_spinner_dropdown_item.
Create my_spinner_dropdown_item.xml in res/layout:
EDITED:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textColor="#color/black"
/>
my_spinner_item
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:textColor="#android:color/transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
Try using margin to change the size of each item.
And then in your SemesterDialogPreference:
setDropDownViewResource(R.layout.my_spinner_dropdown_item);
When I comment out the code for "nameAdapter = ..." or "numAdapter = ..." and leave the other one uncommented it will display the data in the listview fine. So it is able to display both separately, however, when I leave both uncommented it will only display the data involved with numAdapter. Probably a simple mistake but I can't figure out where I went wrong.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
lv = (ListView) findViewById(R.id.listView);
new Logo().execute();
new statistics().execute();
nameAdapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, R.id.stat_name,statName);
numAdapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, R.id.stat_number,statNum);
}
private class statistics extends AsyncTask<String, Void, String> {
//final ListView listview = (ListView) findViewById(R.id.listView);
/*String[] values = new String[]{ "Ball Control", "Crossing", "Curve",
"Dribbling", "Finishing", "Free Kick Accuracy", "Heading Accuracy", "Long Passing",
"Long Shots", "Marking", "Penalties", "Short Passing", "Shot Power", "Sliding Tackle",
"Standing Tackle", "Volleys"};*/
//String[] stats = new String[16];
#Override
protected String doInBackground(String... arg) {
Document document;
try {
document = Jsoup.connect(url).get();
num = document.select("div#stats-base p");
statNum.clear();
for (Element element : num) {
statNum.add(element.ownText());
}
name = document.select("div#stats-base span");
statName.clear();
for (Element element : name) {
statName.add(element.ownText());
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String result) {
lv.setAdapter(nameAdapter);
lv.setAdapter(numAdapter);
}
}
player.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/playerPic"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_below="#+id/playerPic"
android:layout_centerHorizontal="true"
android:layout_marginTop="104dp"/>
</RelativeLayout>
simple_list_item_1.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="match_parent"
android:orientation="vertical" >
<TextView android:id="#+id/stat_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"/>
<TextView
android:id="#+id/stat_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"
android:layout_below="#id/stat_name"/>
</LinearLayout>
I think there's no mystery in that behavior. The last adapter you set to lv is the one that survives.
If what you're looking for is to add both statName and statNum, I'd set one of them and afterwards use .addAll(...) on the other. For instance:
lv = lv.setAdapter(nameAdapter);
lv.addAll(statNum);
nameAdapter.notifyDataSetChanged();
Don't forget the last line to let your ListView render the new data.
Thsi is my code:
EditText editCategoryName, editBrandName;
private ArrayAdapter<String> myAdapter1, myAdapter2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// mylist = new ArrayList<HashMap<String, String>>();
// mylist2 = new ArrayList<HashMap<String, String>>();
myList1 = new ArrayList<String>();
myList2 = new ArrayList<String>();
editCategoryName = (EditText)findViewById(R.id.editCategoryName);
editBrandName = (EditText)findViewById(R.id.editBrandName);
editCategoryName.setText("Google is your friend.", TextView.BufferType.EDITABLE);
<EditText
android:id="#+id/editCategoryName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="請輸入分類名稱"
android:singleLine="true" >
</EditText>
<TextView
android:id="#+id/textSpinnerBrand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="hint" />
<Spinner
android:id="#+id/spinnerBrand"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/editBrandName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="hint"
android:singleLine="true" >
</EditText>
The null pointer error shows that in line:
editCategoryName.setText("Google is your friend.", TextView.BufferType.EDITABLE);
I don't know why. Please help..
you haven't define setContetnView(R.layout.xyz);
in oncreate method.
After super.onCreate(savedInstanceState);
this line you forget setContentView(R.layout.<xml file name>)
you forgot to define setContentView(R.layout.you_xml_name); in onCreate method.
EditText editCategoryName, editBrandName;
private ArrayAdapter<String> myAdapter1, myAdapter2;
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.you_xml_name);
super.onCreate(savedInstanceState);
myList1 = new ArrayList<String>();
myList2 = new ArrayList<String>();
editCategoryName = (EditText)findViewById(R.id.editCategoryName);
editBrandName = (EditText)findViewById(R.id.editBrandName);
editCategoryName.setText("Google is your friend.", TextView.BufferType.EDITABLE);
Give the setContentView("id of your layout file"); before you use the id's of the edit text and spinners inside the xml file. Else it will show null pointer as you are trying to use an id which is a part of an XML layout which you haven't used.