I am developing a PreferenceActivity with custom Preference views. My problem is that I created a view with a ListView and it only shows the first element. I post my code and an image:
http://imageshack.us/photo/my-images/545/sc20120307161530.png/
http://img545.imageshack.us/img545/7207/sc20120307161530.png' border='0'/>
xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:key="player_settings"
android:title="#string/settings_player_config" >
<EditTextPreference
android:defaultValue="#string/settings_player_default_name"
android:dialogMessage="#string/settings_player_summary"
android:dialogTitle="#string/settings_playersname"
android:key="player_name"
android:summary="#string/settings_player_summary"
android:title="#string/settings_playersname" />
</PreferenceCategory>
<PreferenceCategory
android:key="volume"
android:title="#string/settings_volume" >
<com.battleship.preferences.SeekBarPreferences
android:defaultValue="50"
android:key="volume"
android:title="#string/settings_volume" />
</PreferenceCategory>
<PreferenceCategory
android:key="shine"
android:title="#string/settings_shine" >
<com.battleship.preferences.SeekBarPreferences
android:defaultValue="50"
android:key="shine"
android:title="#string/settings_shine" />
</PreferenceCategory>
<PreferenceCategory
android:key="themeTitle"
android:title="#string/settings_group_themes" >
<com.battleship.preferences.ListPreferences android:key="theme" />
</PreferenceCategory>
<PreferenceCategory
android:key="fontsTitle"
android:title="#string/settings_group_font_size" >
<com.battleship.preferences.ListPreferences android:key="font" />
</PreferenceCategory>
</PreferenceScreen>
The Custom ListPreference:
package com.battleship.preferences;
import com.battleship.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class ListPreferences extends Preference implements
OnCheckedChangeListener {
public ListPreferences(Context context) {
super(context);
}
public ListPreferences(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ListPreferences(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onClick() {
super.onClick();
Toast t = Toast.makeText(getContext(), "HOLA!", 3);
t.show();
}
#Override
protected View onCreateView(ViewGroup parent) {
String[] contentString = new String[3];
if (getKey().equals("theme")) {
contentString = new String[] {
(getContext().getString(R.string.settings_theme_default)),
(getContext().getString(R.string.settings_theme_black)),
(getContext().getString(R.string.settings_theme_white)) };
} else {
contentString = new String[] {
(getContext().getString(R.string.settings_font_big)),
(getContext().getString(R.string.settings_font_medium)),
(getContext().getString(R.string.settings_font_little)) };
}
ListView listView = new ListView(getContext());
ArrayAdapter<String> array = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_list_item_single_choice,
android.R.id.text1, contentString);
listView.setAdapter(array);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
return listView;
}
private void updatePreference(int intRadio) {
SharedPreferences.Editor editor = getEditor();
editor.putInt(getKey(), intRadio);
editor.commit();
}
}
Don't you hate it when everyone tries to tell you that you shouldn't do it that way instead of just answering your question? Being able to dynamically generate content is crucial to app flexibility, I totally know what you're after.
This will work assuming all the ListView entries will be roughly the same height (non-pertinent parts omitted). The order is important, try to keep it as I've indicated:
import android.view.ViewGroup.OnHierarchyChangeListener;
public class ListPreferences extends Preference implements
OnCheckedChangeListener, OnHierarchyChangeListener {
private ListView listView;
private View thisView;
private int listHeight = 0;
#Override
protected View onCreateView(ViewGroup parent) {
this.setLayoutResource(R.layout.listview_preference_layout);
thisView = super.onCreateView(parent);
listView = (ListView) thisView.findViewById(android.R.id.list);
listView.setOnHierarchyChangeListener(this);
String[] contentString = new String[3];
if (getKey().equals("theme")) {
contentString = new String[] {
(getContext().getString(R.string.settings_theme_default)),
(getContext().getString(R.string.settings_theme_black)),
(getContext().getString(R.string.settings_theme_white)) };
} else {
contentString = new String[] {
(getContext().getString(R.string.settings_font_big)),
(getContext().getString(R.string.settings_font_medium)),
(getContext().getString(R.string.settings_font_little)) };
}
ArrayAdapter<String> array = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_list_item_single_choice,
android.R.id.text1, contentString);
listView.setAdapter(array);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
return thisView;
}
public void onChildViewAdded(View parent, View child) {
int childHeight = child.getMeasuredHeight();
if(childHeight > 0)
{
listHeight = listView.getAdapter().getCount() * childHeight;
thisView.setMinimumHeight(listHeight);
Log.i(TAG,"onChildViewAdded, done: "+listHeight+" "+childHeight);
}
}
public void onChildViewRemoved(View parent, View child) {
}
}
You'll also need res/layout/listview_preference_layout.xml with the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="fill_vertical|fill_horizontal|fill"
android:gravity="fill"
android:orientation="vertical"
>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:clipChildren="true"
android:isScrollContainer="true"
android:layout_gravity="fill_vertical|fill_horizontal|fill"
android:layout_weight="1"
android:choiceMode="singleChoice"/>
</LinearLayout
I would advise you create a custom layout for Preferences. You can then create and style your Preferences to how you like. And using the SharedPreferences from PreferenceManager.getDefaultPreferences() will allow you to save any changes as if you were using the normal Preferences solution.
As Malcolm suggested, you should probably adhere to the common practices, and not try to do anything to complex here.
Another suggestion, is dont use a ListView. Instead if you want all of your Views to be displayed, manually add these to a LinearLayout, which you then place inside your Preference View.
The problem is that you're trying to add a ListView as an item of another ListView. ListView is used in the layout of the PreferenceActivity. Therefore it doesn't work.
I don't understand why you don't want to go with the standard solution, ListPreference. It is made exactly for the purpose of presenting a user with a list of radio buttons.
Related
i have one list view with two text views inside it, one edit text that is in the same activity but not in the list view and two buttons one to add to the list view and the other to delete from it.
how to add integers to the first text view, the sum of all integers to the second one, and to be from a custom adapter.
thank you.
Activity_main.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="#+id/edit_ten"
android:hint="Score"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn_add"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Add"/>
<Button
android:id="#+id/btn_delete"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Undo"/>
</LinearLayout>
<ListView
android:id="#+id/list_sinhvien"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text_ten"
android:textSize="20dp"
android:text="Score"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text_sdt"
android:textSize="20dp"
android:text="Total"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.addanddelete;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ListView listSinhvien;
EditText editTen;
Button btnThem , btnSua;
ArrayList<Sinhvien> arraySinhvien;
CustomAdapter myadapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
anhxa();
arraySinhvien = new ArrayList<Sinhvien>();
myadapter = new CustomAdapter(this , R.layout.item_layout,arraySinhvien);
listSinhvien.setAdapter(myadapter);
btnSua.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final int count = myadapter.getCount();
myadapter.remove(myadapter.getItem(count -1));
myadapter.notifyDataSetChanged();
return;}});
}
private void anhxa(){
listSinhvien = (ListView)findViewById(R.id.list_sinhvien);
editTen = (EditText)findViewById(R.id.edit_ten);
btnThem = (Button)findViewById(R.id.btn_add);
btnSua = (Button)findViewById(R.id.btn_undo);
btnThem.setOnClickListener(this);
btnSua.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_add:
Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
String ten = editTen.getText().toString();
String sdt = editTen.getText().toString();
Sinhvien temp = new Sinhvien(R.mipmap.ic_launcher,ten , sdt);
arraySinhvien.add(temp);
myadapter.notifyDataSetChanged();
break;
}
}
}
CustomAdapter.java
package com.example.addanddelete;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class CustomAdapter extends ArrayAdapter {
Activity activity;
int layout;
ArrayList<Sinhvien> arrSinhVien;
public CustomAdapter(#NonNull Activity activity, int layout, #NonNull ArrayList<Sinhvien> arrSinhVien) {
super(activity, layout, arrSinhVien);
this.activity = activity;
this.layout = layout;
this.arrSinhVien = arrSinhVien;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater layoutInflater = activity.getLayoutInflater();
convertView = layoutInflater.inflate(layout, null);
TextView ten = (TextView) convertView.findViewById(R.id.text_score);
TextView sdt = (TextView) convertView.findViewById(R.id.text_total);
ten.setText(arrSinhVien.get(position).getTenSinhvien());
sdt.setText(arrSinhVien.get(position).getSdtSinhvien());
return convertView;
}
}
Sinhvien.java
package com.example.addanddelete;
public class Sinhvien {
String tenSinhvien;
String sdtSinhvien;
public Sinhvien(String iclauncher,String ten, String sdt) {
}
public Sinhvien(int iclauncher,String tenSinhvien, String sdtSinhvien) {
this.tenSinhvien = tenSinhvien;
this.sdtSinhvien = sdtSinhvien;
}
public String getTenSinhvien() {
return tenSinhvien;
}
public void setTenSinhvien(String tenSinhvien) {
this.tenSinhvien = tenSinhvien;
}
public String getSdtSinhvien() {
return sdtSinhvien;
}
public void setSdtSinhvien(String sdtSinhvien) {
this.sdtSinhvien = sdtSinhvien;
}
}
The general behaviour/implementation that you've outlined in your question is very well documented. That said, I'd suggest considering utilising a RecyclerView with associated custom item view and adapter, as opposed to a ListView. There are a few reasons why I'd suggest this.
I did a little searching and found several examples that cover the general idea of your implementation. This example does a great job of illustrating how to achieve what you're seeking. The article begins by outlining some reasons to work with a RecyclerView over a ListView or GridView, then proceeds to give an in-depth run-down on how to implement a RecyclerView with custom adapter (and associated item view and item class).
At a glance, your implementation would require:
An Activity containing your RecyclerView, two Buttons (used for adding and deleting elements from the RecyclerView) and an EditText for taking user input.
A custom item View representing individual items of your RecyclerView list. This would contain the two TextView views (one for displaying the integer and the other for displaying the sum of all integers).
A custom item model Class to represent the data model for the above custom item View. This would hold an integer value and likely some logic for displaying the sum.
A custom RecyclerView adapter (which ties all of the above together). This will need to handle the task of binding data from your dataset (that grows and shrinks based on user input) to instances of your custom items that are to appear in the RecyclerView list. This adapter could also be used by your add and delete item buttons to modify the elements in the RecyclerView list.
The above is outlined in far greater depth in the link I provided earlier.
I sincerely hope that helps!
I am trying to customise an EditTextPreference to display a textview(i.e to display value of the preference) and a clear/delete button on its right side.
I created CustomEditTextPreference.java
package com.customedittextpreference;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.Image;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
/**
* Created by cyong on 23/04/16.
*/
public class CustomEditTextPreference extends EditTextPreference {
private ImageButton clearButton;
private TextView valueTextView;
public CustomEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setupChangeListener();
}
public CustomEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setupChangeListener();
}
public CustomEditTextPreference(Context context) {
super(context);
setupChangeListener();
}
#Override
protected void onBindView(View view) {
super.onBindView(view);
valueTextView = (TextView) view.findViewById(R.id.value_textview);
clearButton = (ImageButton) view.findViewById(R.id.clear_button);
clearButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setText("");
}
});
String valueString = getText();
Log.v(Settings.APP_NAME, "refreshValue(): valueString=" + valueString);
valueTextView.setText(valueString);
toggleClearButton(valueString);
}
private void toggleClearButton(String value)
{
if (value.length()==0)
{
clearButton.setVisibility(View.GONE);
}
else
{
clearButton.setVisibility(View.VISIBLE);
}
}
private void setupChangeListener()
{
setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String newStringValue = (String) newValue;
valueTextView.setText(newStringValue);
toggleClearButton(newStringValue);
return true;
}
});
}
}
CustomEditTextPreference class use the layout below(i.e prefwidget_edittext.xml) as widget layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants"
android:padding="0dp">
<TextView
android:id="#+id/value_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_gravity="center_vertical"
android:singleLine="true" />
<ImageButton
android:id="#+id/clear_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:layout_marginRight="5dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:layout_gravity="center_vertical"
android:src="#mipmap/delete_icon"
android:background="#00000000"
/>
</LinearLayout>
I specify my custom EditTextPreference in a preferences_list.xml under res/xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:key="status_choice"
android:entries="#array/array_status_entries"
android:entryValues="#array/array_status_values"
android:title="#string/choose_status_title"
android:summary="%s"
android:defaultValue="0"
/>
<CheckBoxPreference
android:defaultValue="false"
android:key="has_email"
android:title="#string/has_email_title" >
</CheckBoxPreference>
<com.customedittextpreference.CustomEditTextPreference
android:widgetLayout="#layout/prefwidget_edittext"
android:title="#string/productcode_title"
android:key="code"/>
</PreferenceScreen>
I can click on the edittextpreference and enter a string. The string entered would be saved, but would not display in textview of my custom widget layout after that. However, if I kill my app, and start it again, the textview would display the saved string. Now, when I click on the clear/delete button, I can see the value being deleted in but, the UI is not being updated to clear the string the textview and hide the clear/delete button.
For convenience, I have uploaded my sample project into github below:
Sample GitHub Project
Seems like notifyChanged() needs to be called when updating a Preference.
I noticed calls setTitle() and setSummary() would update the UI. It turns out, notifyChanged() is called in those both those functions.
Update the github project with the fix.
GitHub Project
The following code is suppose to be doing this :
- Displaying a string using a standard TextView
- Displaying a string using a extended TextView. (This one overrides OnDraw and draws a line across the string, making it look like it was striked out)
The problem is that only the standard one is appearing.
Please check the following code :
ExpenseWatchActivity.java (This is the main activity)
package com.app.expensewatch;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Calendar;
public class ExpenseWatchActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int day,mon,yr;
Calendar cal = Calendar.getInstance();
day = cal.get(Calendar.DATE);
mon = 1 + cal.get(Calendar.MONTH);
yr = cal.get(Calendar.YEAR);
String text = getResources().getString(R.string.hello, day , mon , yr);
TextView tx1 = (TextView)findViewById(R.id.text1);
tx1.setText(text);
List1 list = (List1)findViewById(R.id.list1);
list.setText(text);
}
}
List1.java (This is the extended TextView)
package com.app.expensewatch;
import android.util.AttributeSet;
import android.widget.TextView;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import com.app.expensewatch.R;
public class List1 extends TextView {
public List1(Context context) {
super(context);
}
public List1(Context context, AttributeSet attrs) {
super( context, attrs );
}
public List1(Context context, AttributeSet attrs, int defStyle) {
super( context, attrs, defStyle );
}
float ht = (float)getHeight();
float wd = (float)getWidth();
#Override
protected void onDraw(Canvas canvas)
{
Paint divider = new Paint();
divider.setColor(getResources().getColor(R.color.line1));
canvas.drawLine(0,ht/2,wd,ht/2,divider);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello" />
<com.app.expensewatch.List1
android:id="#+id/list1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello" />
/>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ExpenseWatch</string>
<string name="hello">Today is : %1$d / %2$d / %3$d</string>
</resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="line1">#6456648f</color>
</resources>
When you set your width and height in the extended TextView, the view hasn't been measured yet. Its width and height are zero. Override onMeasure() in your extended TextView, and set your width and height there.
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
ht = getHeight();
wd = getWidth();
}
Always avoid setting a private variable that will hold a dimension in a view until after onMeasure() has been called.
I'm pretty new to Android and I have been playing about with GridView. I've got stuck trying to retrieve Strings from individual elements of the GridView so that I can save state based on the changes a user has made.
I've mocked up a stripped down version of what I'm trying to do for this question:
The view of each element in the grid is made up of a TextView and an EditText defined by the following xml file; grid_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "#+id/single_item_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id = "#+id/label"
android:layout_width = "fill_parent"
android:layout_height="wrap_content"
android:inputType="text">
</TextView>
<EditText
android:id = "#+id/item_value"
android:layout_width = "fill_parent"
android:layout_height="wrap_content"
android:inputType="text">
</EditText>
</LinearLayout>
My main.xml, consisting of a button and a gridview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/editbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRetrieveButtonClick"
android:text="#string/get" android:clickable="true"/>
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="250dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:paddingTop="50dp">
</GridView>
</LinearLayout>
My Activity Class:
package com.jdev.simplegrid;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.Toast;
public class SimpleGridActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
//Some hard-coded sample data -each person has a birth-name and a nickname
ArrayList<Person> people = new ArrayList<Person>();
people.add(new Person("David","Dave"));
people.add(new Person("Bruce","Batman"));
people.add(new Person("John","J"));
gridview.setAdapter(new SimpleAdapter(this, people));
}
public void onRetrieveButtonClick(View view)
{
Toast.makeText(getBaseContext(), "Get the String value of EditText at position 1 of the gridview e.g Batman", Toast.LENGTH_LONG).show();
//Get the String value of EditText at position 1 of the gridview. e.g Batman
}
}
And finally my Adapter for the view:
package com.jdev.simplegrid;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
public class SimpleAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Person> personList;
public SimpleAdapter(Context c, ArrayList<Person> people) {
mContext = c;
personList = people;
}
public int getCount() {
return personList.size();
}
public Person getItem(int position) {
return personList.get(position);
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.grid_item, parent, false);
TextView birthnameLabel=(TextView)view.findViewById(R.id.birthname);
birthnameLabel.setText(personList.get(position).getBirthname());
EditText nicknameText=(EditText)view.findViewById(R.id.nickname);
nicknameText.setText(personList.get(position).getNickname());
return view;
}
}
So my question is, how do I retrieve the a value from the gridview on the click of a button. Say the value "Batman" in EditText at position 1 of the gridview.
I feel like I'm really missing something here!
gridview.getAdapter().getItem(1).getNickname();
Is that what you're looking for? assuming your Person object has a getter method for the nickname of course. - Sorry, obviously I could've seen that in your custom adapter.
If your the idea is the user can change the nickname in the EditText, you'll probably want to add a TextChangedListener (TextWatcher) to each of them and after editting update the nickname on the Person object associated with that position in the grid; e.g. with the help of a setNickname(String) method.
The easiest way to think about this is probably in terms of 'models' and 'views'. The TextViews and EditTexts are the views, whereas the Person objects in the adapter are the models. If you want to make any changes to the data, modify the underlying models with some logic and simply have the views update/refresh after that.
I have an application with tabs. In one tab I need to put data (strings) in rows. To do so I chose tableLayout but when I wanted to use a contextmenu on its rows it doesn't work.
I can show the contextmenu onLongClick but the problem is that I can't get the info about the selected row to edit or delete the selected row. Then I read in a discussion that using listView is better than tablelayout if we have many rows. But the examples I saw extend listactivity but I don't want to do this.
So when I try working on a listView without extending listactivity I don't know how to do it what I mean is that I've never used listView before so I try different examples I found on the internet to understand it but it's not working. Here's what I did so far for the listView:
String [] items=getRessources().getStringArray(R.arra.resolution);
//Resolution is an array of strings
ListView lv=(ListeView) findViewById(R.id.listView);
v.setAdapter(new ArrayAdapter<string>(this, android.R.layout.simple_list_item_1, items);
When I compile it I get a list with elements of my array in it but first, I want to change the color of text which I can't. And secondly I want to add rows dynamically to the list which I don't know how to do either. I think I have to use an adapter to do it but I don't know how.
Can someone please guide me through this. I just want to know how to attach my list to an adapter which'll allow me to dynamically add rows, add contextMenu etc.
main Activity class:
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
public class SelectedActivity extends Activity {
private SelectedAdapter selectedAdapter;
private ArrayList<String> list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selected_example);
// populate the model - a simple a list
list = new ArrayList<String>();
list.add("Apple");
list.add("Orange");
list.add("Grape");
list.add("Grape1");
list.add("Grape2");
list.add("Grape3");
list.add("Grape4");
list.add("Grape5");
list.add("Grape6");
// create our SelectedAdapter
selectedAdapter = new SelectedAdapter(this,0,list);
selectedAdapter.setNotifyOnChange(true);
ListView listview = (ListView) findViewById(R.id.listExample);
listview.setAdapter(selectedAdapter);
listview.setOnItemClickListener(new OnItemClickListener() {
//#Override
public void onItemClick(AdapterView arg0, View view,
int position, long id) {
// user clicked a list item, make it "selected"
selectedAdapter.setSelectedPosition(position);
}
});
}
Adapter class:
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
public class SelectedAdapter extends ArrayAdapter{
// used to keep selected position in ListView
private int selectedPos = -1; // init value for not-selected
public SelectedAdapter(Context context, int textViewResourceId,
List objects) {
super(context, textViewResourceId, objects);
}
public void setSelectedPosition(int pos){
selectedPos = pos;
// inform the view of this change
notifyDataSetChanged();
}
public int getSelectedPosition(){
return selectedPos;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
// only inflate the view if it's null
// if (v == null) {
LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.selected_row, null);
// }
// get text view
TextView label = (TextView)v.findViewById(R.id.txtExample);
Button btn=(Button)v.findViewById(R.id.btn1);
// change the row color based on selected state
if(selectedPos == position){
label.setBackgroundColor(Color.CYAN);
btn.setBackgroundResource(R.drawable.next);
}
else{
label.setBackgroundColor(Color.WHITE);
}
label.setText(this.getItem(position).toString());
return(v);
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg8"
android:id="#+id/RootView"
>
<LinearLayout android:id="#+id/myLayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
</LinearLayout>
</LinearLayout>
You need to define an xml which will be used to hold data of each row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="1">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="793dp"
android:layout_height="wrap_content"
android:layout_weight="0.23" >
<TextView android:id="#+id/col1"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:width="50dp"
android:textSize="18sp"
/>
<TextView android:id="#+id/col2"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:width="150dp"
android:textSize="18sp"
/>
<ImageView android:id="#+id/editimage"
android:background="#drawable/edit"
android:clickable="true"
android:onClick="ClickHandlerForEditImage"
android:layout_width="35dp"
android:layout_height="35dp"/>
</TableRow>
</LinearLayout>
In the above xml i have also included the ImageView, it is not really required but this is just to update you that we can include the other controls also.
& at the last you should have a function in your related class:
private void LoadData()
{
DBAdapter db = new DBAdapter(this);
db.open();
Cursor cur = db.GetData();
private ListView lv = (ListView)findViewById(R.id.myLayout);
lv.setAdapter(null);
if(cur.moveToFirst())
{
String[] from = new String[] {"_id","column1"};
int[] to = new int[] {R.id.col1, R.id.col2};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.grid_item, cur, from, to);
lv.setAdapter(adapter);
}
db.close();
}
}
It appears that noone has answered you contextMenu question. To get a context menu to work with your list , after you call ListView yourList = getListView(); you must call registerForContextMenu(yourList);
And to handle the menu creation you must implement the method
#Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
super.onCreateContextMenu(context, v,menuInfo);
MenuInflater inflater = getMenuInflater();
menu.setHeaderTitle("YOUR TITLE");
menu.setHeaderIcon(R.drawable.YOUR DRAWABLE);
inflater.inflate(R.menu.YOUR_MENU_RESOURCE, menu);
}
Then you can respond to clicks by implenting the method
#Override public boolean onContextItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.YOUR_MENU_ITEM: // do stuff if the item is selected
return true;
case R.id.YOUR_MENU_ITEM: // do stuff if the item is selected
return true;
case R.id.YOUR_MENU_ITEM: // do stuff if the item is selected
return true;
}
return false; // nothing selected
}