The idea is I want to display list of mp3 files from SDCARD in listview, each line is also contain a spinner with three items (share, rename and delete), I used custom arrayadapter for this purpose and I am stuck on how to add the spinner on the custom arrayadapter class
this is the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:theme="#style/AppTheme"
tools:context=".MainActivity"
>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="416dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp" />
<Button
android:id="#+id/stopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:layout_marginBottom="28dp"
android:text="Stop" />
</LinearLayout>
this is where i create the spinner
<?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"
android:orientation="horizontal"
android:paddingLeft="14dp"
>
<TextView
android:id="#+id/text1"
android:layout_width="0dp"
android:layout_height="33dp"
android:layout_weight="1"
android:gravity="center|start"
/>
<Spinner
android:id="#+id/spinner"
android:layout_width="31dp"
android:layout_height="33dp"
android:layout_gravity="right"
android:background="#drawable/download"
>
</Spinner>
</LinearLayout>
here I create the datatype
public class MyDataType {
// Name of the song
private String mSongName;
// Drawable Menu resource ID
private int mSpinner;
/** Audio resource ID for the word */
//private int mAudioResourceId;
//public MyDataType(String vName, int imageResourceId, int audioResourceId){
public MyDataType(String vName, int spinner){
mSongName = vName;
mSpinner = spinner;
//mAudioResourceId = audioResourceId;
}
public String getSongName() {
return mSongName;
}
public int getSpinner() {
return mSpinner;
}
}
here i create the custom adapter
public class SongAdapter extends ArrayAdapter<MyDataType> {
public SongAdapter(Context context, ArrayList<MyDataType> words) {
super(context, 0, words);
}
Spinner mSpinner;
ArrayAdapter<CharSequence> mAdapter;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
MyDataType currentSong = getItem(position);
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.song_list, parent, false);
}
TextView songTextView = (TextView)
listItemView.findViewById(R.id.text1);
songTextView.setText(currentSong.getSongName());
mSpinner = (Spinner) listItemView.findViewById(R.id.spinner);
return listItemView;
}
}
this is the main activity
public class MainActivity extends AppCompatActivity {
private static final String SD_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Xylophone/";
ArrayList<MyDataType> file_list = new ArrayList<MyDataType>();
private final ArrayList<String> songs = new ArrayList<String>();
private MediaPlayer mp = new MediaPlayer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer mp = new MediaPlayer();
//updatePlayList();
if (ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
fillList();
} else {
ActivityCompat.requestPermissions(this, new String[] {android.Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
}
final SongAdapter adapter = new SongAdapter(this, file_list);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try{
mp.reset();
mp.setDataSource(SD_PATH + songs.get(position));
mp.prepare();
mp.start();
}catch (IOException e){
Log.v(getString(R.string.app_name),e.getMessage() );
}
}
});
Button stopPlay = (Button) findViewById(R.id.stopBtn);
stopPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mp.stop();
}
});
}
private void fillList() {
File file = new File(SD_PATH );
File list[] = file.listFiles();
for (int i = 0; i < list.length; i++) {
//file_list.add(list[i].getName(),);
songs.add(list[i].getName());
MyDataType myList = new MyDataType(list[i].getName(), R.id.spinner);
file_list.add(myList);
}
}
}
the string value that cotain menu items
<resources>
<string name="app_name">PlayMp3List</string>
<string-array name="spinner_data">
<item>Rename</item>
<item>Delete</item>
<item>Share</item>
</string-array>
</resources>
OK, it seems that you have almost every thing in place that you need. However, there are a few minor changes you will need to make before this is working. First, in the code where you check the permissions if permission is granted, you call fillList(). But in the Else condition, you request the permission. In the code above, you are not showing how you are handling the result of the request for permission. If the permission is granted after the request, then in that case, you must also call fillList() there as well. And, after calling fillList() there, you will also need to recreate the SongAdapter, and also redo the setAdapter on your list view.
In fillList(), you are initializing your myList item with the song title, and the ID of a spinner. You shouldn't be doing that. What you should be doing is populating the myList item with the 'value' of the spinner... and the default should probably be 0.
In your getView() method in the custom Adapter, you are getting the current item based on the position parameter and putting the song title into the textView. Then, you are getting a reference to the Spinner, but you are not doing anything with it. After getting the reference you should be doing something like mSpinner.value = currentSong.getSpinner();
What you want to do in your MyDataType class is to store the value of the spinner, not any reference to the spinner itself. You also need some sort of click event so that if the user changes the value of the spinner in the row, you can update your ArrayList with the new value. That assumes the user can change that selected spinner item, if not, then when you are creating the list, you have to figure out what value to set each song's spinner value with, which you are not doing.
Your Adapter code should look like this. Note the removal of getContext().
public class SongAdapter extends ArrayAdapter<MyDataType> {
Context mContext = null;
public SongAdapter(Context context, ArrayList<MyDataType> words) {
super(context, 0, words);
mContext = context;
}
Spinner mSpinner;
ArrayAdapter<CharSequence> mAdapter;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
MyDataType currentSong = getItem(position);
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(mContext.inflate(R.layout.song_list, parent, false);
}
TextView songTextView = (TextView)
listItemView.findViewById(R.id.text1);
songTextView.setText(currentSong.getSongName());
mSpinner = (Spinner) listItemView.findViewById(R.id.spinner);
mSpinner.value = currentSong.getSpinner();
return listItemView;
}
}
In your list item click method you will need to set a breakpoint to see that the path you are passing in mp.setDataSource is a valid path. It is possible that your SD_PATH needs a '/' at the end or something.
In your layout.xml file you would define your spinner like this:
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/country_arrays"
android:prompt="#string/country_prompt" />
Where the entries #array/country_arrays points to the list that you defined in Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MyAndroidApp</string>
<string name="country_prompt">Choose a country</string>
<string-array name="country_arrays">
<item>Malaysia</item>
<item>United States</item>
<item>Indonesia</item>
<item>France</item>
<item>Italy</item>
<item>Singapore</item>
<item>New Zealand</item>
<item>India</item>
</string-array>
</resources>
You are missing the value in your spinner xml definition:
<Spinner
android:id="#+id/spinner"
android:layout_width="31dp"
android:layout_height="33dp"
android:layout_gravity="right"
android:background="#drawable/download"
android:entries="#array/spinner_data"
>
</Spinner>
Related
Please bear with me as I am new to android studio. I want to make a ScrollView containing images with corresponding name (TextView). I want to be able to select an image by touching it in the ScrollView, but I don't know how. I have implemented the ScrollView like this, I also want to be able to add pictures with a name attached to them.
Main_activity.xml
<HorizontalScrollView
android:id="#+id/scrollFilterView"
android:layout_width="fill_parent"
android:layout_height="130dp"
android:scrollbars="none"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.57"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<LinearLayout
android:id="#+id/gallery"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" />
</HorizontalScrollView>
scrollview.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/filterView"
android:layout_width="90dp"
android:layout_height="90dp"
app:srcCompat="#android:drawable/sym_def_app_icon"
android:contentDescription="filterView" />
<TextView
android:id="#+id/textFilter"
android:layout_width="90dp"
android:gravity="center"
android:layout_height="wrap_content"
android:text="textFilter" />
MainActivity.java
LayoutInflater inflater = LayoutInflater.from(this);
for (int i = 0; i < NUMBER_OF_FILTERS_GRID; i++ ) {
View scrollView = inflater.inflate(R.layout.scrollview, gallery, false);
TextView textview = scrollView.findViewById(R.id.textFilter);
textview.setText("Filter "+ i);
ImageView filterView = scrollView.findViewById(R.id.filterView);
filterView.setImageResource(R.mipmap.ic_launcher);
gallery.addView(scrollView);
}
}
Some input to if this is the right path to take and what functions i should use is very appreciated :)
Use a RecyclerView instead of a ListView, and used with and adapter, this will grant you more control over each item displayed on the list (in this case in the RecyclerView),
you will need to create and Adapter class, a simple class for your Items and a new XML layout that will serve as the model of each item in the list.
Simple class for your items:
public class dataSetItem {
private Integer image;
private String listItemText;
public Integer getImage() {
return image;
}
public void setImage(Integer image) {
this.image = image;
}
public String getListItemText() {
return listItemText;
}
public void setListItemText(String listItemText) {
this.listItemText = listItemText;
}
}
In your mainActivity remove the listView and for loop, use this instead:
public class MyActivity extends Activity {
private RecyclerView recyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager layoutManager;
private ArrayList mAdapter = new ArrayList();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
recyclerView = (RecyclerView) findViewById(R.id.your_recycler_view);
mDataset.add(dataSetItem("item 1", R.mipmap.ic_launcher))
mDataset.add(dataSetItem("item 2", R.mipmap.ic_launcher))
// You can add more items to the mDataset list and call the adapter again to update the RecyclerView
// use a linear layout manager
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
// specify an adapter (see also next example)
mAdapter = new MyAdapter(myDataset);
recyclerView.setAdapter(mAdapter);
}
// ...
}
Create this XML layout which will serve as the model for each item, called my_item_list_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:id="#+id/the_image_i_want_to_click"
android:layout_width="80dp"
android:layout_margin="10dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:background="#color/ADRBlack"
/>
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="220dp"
android:textColor="#color/Black"
android:textSize="16sp"
tools:text="Example Restaurant" />
</LinearLayout>
</LinearLayout>
Now the adapter class, in this case called MyAdapter:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private customItem[] mDataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView textView;
public MyViewHolder(TextView v) {
super(v);
textView = v;
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(customItem[] myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
TextView v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.my_item_list_layout, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.textView.setText(mDataset[position].getListItemText);
holder.textView.setBackground(mDataset[position].getImage);
//Now to do something when you clic the image use this
holder.textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
//Your action here
}
});
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return mDataset.length;
}
}
I have a dynamically created list. It's fragment-layout is as follows:
<ListView
android:id="#+id/colorsList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
When I add a row to the listview, I want an "X" mark to appear at the end of the row so that when the user clicks on the "X", the row gets deleted.
Not sure, if its relevant but, the way I am adding and removing rows to the listview is as follows.
Logic to add a row tot he listView:
#BindView(R.id.understood_languages_list)
View mColorsList;
*********
*********
ListView listFavoriteColors;
listFavoriteColors = (ListView) mColorsList;
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, getColorsList());
listFavoriteColors.setAdapter(arrayAdapter);
Logic to remove a row from the ListView.
listFavoriteColors.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
initializeView();
arrayAdapter.remove(((TextView)view).getText().toString());
}
});
Any helpful suggestions on how this can be achieved is much appreciated.
User Custom Adapter with custom Layout to attach delete button to each item in listview
try this code :
CustomAdapter
public class ListAdapter extends ArrayAdapter<String> {
private int resourceLayout;
private Context mContext;
ListInterFace interface;
public ListAdapter(Context context, int resource, List<String>
items,ListInterFace interface) {
super(context, resource, items);
this.resourceLayout = resource;
this.mContext = context;
this.interface=interface;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(mContext);
v = vi.inflate(resourceLayout, null);
}
String value= getItem(position);
if (value!= null) {
TextView tt1 = (TextView) v.findViewById(R.id.dummytext);
Button cancel_click=v.findViewById(R.id.cancel_click);
if (tt1 != null) {
tt1.setText(value);
}
//remove item on button click
cancel_click.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
interface.removeItem(position);
}
})
}
return v;
}
}
R.layout.itemlistrow defines the row of the ListView.
<?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="wrap_content"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/dummytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dummy"
android:textColor="#android:color/black"
android:textSize="18sp"
android:padding="5dp"/>
<ImageView
android:id="#+id/cancel_click"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#mipmap/cancel"
android:layout_alignParentRight="true"
android:layout_margin="5dp"/>
</RelativeLayout>
</LinearLayout>
In the MainActivity define ListViewlike this,
ListView yourListView = (ListView) findViewById(R.id.itemListView);
// get data from the table by the ListAdapter
ListAdapter customAdapter = new ListAdapter(this, R.layout.itemlistrow, myList, new ListInterFace () {
#Override
public void removeItem( int position) {
myList.remove(position);
customAdapter .notifyDataSetChanged();
});
yourListView .setAdapter(customAdapter);
InterFace
public interface ListInterFace {
void removeItem(int position);
}
try this link to remove item from list view on button click
Remove Item From List View On Button Click
I have a simple listView via a custom adapter. Each row in the list view is composed of a ImageButton and TextView.
The idea is to change the ImageButton when user taps on it and it does not work. However, ImageButton changes for the views that are scrolled and recycled. I mean, when user scrolls down the list and comes back to the top and tap on the ImageButton it works as expected for those rows that were scrolled and back again.
Can you guys help me out what i'm missing? I'm extremely new to Android and have been stuck around this for about a week now trying to fix this.
Here's the APK for the sample test app:
https://www.dropbox.com/s/pzlahtqkgj010m8/app-ListViewExample.apk?dl=0
Here are the relevant parts of my code:
avtivity_main.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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
/>
</RelativeLayout>
and to render the single row in the ListView the following xml is utilized:
single_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:src="#drawable/b"
android:layout_weight="2"
style="#style/ListView.first"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_weight="10"
android:layout_gravity="center"
/>
</LinearLayout>
coming to the Java code,
MainActivity.java is
public class MainActivity extends AppCompatActivity {
String[] titles;
ListView list;
private listViewAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
titles = res.getStringArray(R.array.titles);
list = (ListView) findViewById(R.id.listView);
adapter = new listViewAdapter(this, titles);
list.setAdapter(adapter);
}
}
and the custom adapter listViewAdapter.java look like this
class sListItem {
public ImageButton button;
public TextView text;
public boolean active;
}
public class listViewAdapter extends ArrayAdapter<String> {
private final String[] titles;
Context context;
private int size = 15;
public sListItem mp[] = new sListItem[size];
public listViewAdapter(Context context, String[] titles) {
super(context, R.layout.single_row, R.id.imageButton, titles);
this.context = context;
this.titles = titles;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, parent, false);
if(mp[position] == null)
mp[position] = new sListItem();
mp[position].button = (ImageButton) row.findViewById(R.id.imageButton);
mp[position].button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp[position].active = !mp[position].active;
setIcon(position, mp[position].active);
}
});
mp[position].text = (TextView) row.findViewById(R.id.textView);
mp[position].text.setText(titles[position]);
setIcon(position, mp[position].active);
return row;
}
private void setIcon(int position, boolean active) {
Drawable drawable;
if(active){
drawable = getContext().getResources().getDrawable(R.drawable.a);
} else {
drawable = getContext().getResources().getDrawable(R.drawable.b);
}
mp[position].button.setImageDrawable(drawable);
}
}
EDIT: Added link to sample test app.
You should tell adapter to update by calling notifydatasetchanged in the click listener
mp[position].button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp[position].active = !mp[position].active;
setIcon(position, mp[position].active);
notifydatasetchanged();
}
});
I've seen lots of posts regarding this question but none of them guided me through a solution to my problem. I have a listview using a custom cursoradapter which every item has a checkbox in it. This checkbox is not binded to the database, because my intention is to use it for deletion. Once delete button is clicked, all items with checkboxes checked should be deleted from view and database. I implemented the following logic, but turns out the method <my listview instance>.getCheckedItemPositions() always returns a SparseBooleanArray empty. Can anyone tell me what I am doing wrong? I am not sure if this is the best approach anyway, so any other solution or ideas are welcome.
This is my CursorAdapter
public class ExpenseCursorAdapter extends CursorAdapter {
private NumberFormat formatter = NumberFormat.getCurrencyInstance();
Context context;
private DatabaseHelper db;
public ExpenseCursorAdapter(Context context, Cursor c) {
super(context, c);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// when the view will be created for first time,
// we need to tell the adapters, how each item will look
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.expense_single_row_item, parent, false);
return retView;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
// here we are setting our data
// that means, take the data from the cursor and put it in views
TextView textViewParticipantId = (TextView) view.findViewById(R.id.tv_expense_participant);
textViewParticipantId.setText(cursor.getString(cursor.getColumnIndex("name")));
TextView textViewExpenseDescription = (TextView) view.findViewById(R.id.tv_expense_description);
textViewExpenseDescription.setText(cursor.getString(cursor.getColumnIndex("description")));
TextView textViewExpenseAmount = (TextView) view.findViewById(R.id.tv_expense_amount);
textViewExpenseAmount.setText(formatter.format(Double.valueOf(cursor.getDouble(cursor.getColumnIndex("amount")))));
}
}
This is my item layout:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1,2,3" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="10" >
<CheckBox
android:id="#+id/cb_expense_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:button="#drawable/selector_check"
android:focusable="false"
android:padding="2dip"
android:weightSum="1"/>
<TextView
android:id="#+id/tv_expense_participant"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:weightSum="3"
android:gravity="left"/>
<TextView
android:id="#+id/tv_expense_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:weightSum="3"
android:gravity="left"/>
<TextView
android:id="#+id/tv_expense_amount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:weightSum="3"
android:gravity="left" />
</TableRow>
And this is the activity with the logic I am using on delete button --removeExpense():
private static final String TAG = ExpenseActivity.class.getSimpleName();
private ListView listView;
private ExpenseCursorAdapter customAdapter;
private DatabaseHelper databaseHelper;
private long eventId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expense);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
eventId = getIntent().getLongExtra("tag_event_id", -1);
databaseHelper = new DatabaseHelper(this);
listView = (ListView) findViewById(R.id.expense_list_data);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "clicked on item: " + position+" - ID: "+id);
}
});
new Handler().post(new Runnable() {
#Override
public void run() {
customAdapter = new ExpenseCursorAdapter(ExpenseActivity.this, databaseHelper.getAllExpensesByEventCursor(eventId));
listView.setAdapter(customAdapter);
}
});
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
...
private void removeExpenses() {
Log.d(TAG, "Remove Expense button clicked on");
DatabaseHelper db = new DatabaseHelper(this);
SparseBooleanArray checkedItemPositions = listView.getCheckedItemPositions();
int itemCount = listView.getCount();
for (int i = itemCount - 1; i >=0; i--) {
if (checkedItemPositions.valueAt(i)) {
//db.deleteExpense(listView.getItemIdAtPosition(checkedItemPositions.keyAt(i)));
db.deleteExpense(listView.getItemIdAtPosition(i));
}
customAdapter.notifyDataSetChanged();
}
checkedItemPositions.clear();
listView.setAdapter(customAdapter);
listView.clearChoices();
if (db != null){
db.close();
}
}
I'm completely new to Java/Android and am stuck with listviews.
What I want is to have 4 lines in a row in listview with different font styles
rowlayout.xml
<TextView
android:id="#+id/1"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/2"
android:textSize="30sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/3"
android:typeface="sans"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/4"
android:typeface="sans"
android:textSize="15sp"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
I have this in a separate xml file
results.xml
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/TitleLbl" >
</Listview>
and my array adapter looks like this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.rowlayout, R.id.2, Array);
setListAdapter(adapter);
What I'm trying to do is get an arraylist (containing ID, name, surname and number) to populate the listview so that each information is on its own line in the row.
the array adapter above works, but each of the four informations is in their own row.
Can anyone help?
(also, I've scoured the internet to try and sort this out, but can't seem to find anything that helps me)
You may need to use custom list view with custom array adapter. Refer this link for example.
Content:
The Android HelloListView ( http://developer.android.com/resources/tutorials/views/hello-listview.html ) tutorial shows how to bind a ListView to an array of string objects, but you'll probably outgrow that pretty quickly. This post will show you how to bind the ListView to an ArrayList of custom objects, as well as create a multi-line ListView.
Let's say you have some sort of search functionality that returns a list of people, along with addresses and phone numbers. We're going to display that data in three formatted lines for each result, and make it clickable.
First, create your new Android project, and create two layout files. Main.xml will probably already be created by default, so paste this in:
<?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">
<TextView
android:layout_height="wrap_content"
android:text="Custom ListView Contents"
android:gravity="center_vertical|center_horizontal"
android:layout_width="fill_parent" />
<ListView
android:id="#+id/ListView01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
Next, create a layout file called custom_row_view.xml. This layout will be the template for each individual row in the ListView. You can use pretty much any type of layout - Relative, Table, etc., but for this we'll just use Linear:
<?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">
<TextView android:id="#+id/name"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#FFFF00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/cityState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Now, add an object called SearchResults. Paste this code in:
public class SearchResults {
private String name = "";
private String cityState = "";
private String phone = "";
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setCityState(String cityState) {
this.cityState = cityState;
}
public String getCityState() {
return cityState;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPhone() {
return phone;
}
}
This is the class that we'll be filling with our data, and loading into an ArrayList.
Next, you'll need a custom adapter. This one just extends the BaseAdapter, but you could extend the ArrayAdapter if you prefer.
public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.name);
holder.txtCityState = (TextView) convertView.findViewById(R.id.cityState);
holder.txtPhone = (TextView) convertView.findViewById(R.id.phone);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtName.setText(searchArrayList.get(position).getName());
holder.txtCityState.setText(searchArrayList.get(position).getCityState());
holder.txtPhone.setText(searchArrayList.get(position).getPhone());
return convertView;
}
static class ViewHolder {
TextView txtName;
TextView txtCityState;
TextView txtPhone;
}
}
(This is basically the same as the List14.java API demo)
Finally, we'll wire it all up in the main class file:
public class CustomListView extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<SearchResults> searchResults = GetSearchResults();
final ListView lv1 = (ListView) findViewById(R.id.ListView01);
lv1.setAdapter(new MyCustomBaseAdapter(this, searchResults));
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
SearchResults fullObject = (SearchResults)o;
Toast.makeText(ListViewBlogPost.this, "You have chosen: " + " " + fullObject.getName(), Toast.LENGTH_LONG).show();
}
});
}
private ArrayList<SearchResults> GetSearchResults(){
ArrayList<SearchResults> results = new ArrayList<SearchResults>();
SearchResults sr1 = new SearchResults();
sr1.setName("John Smith");
sr1.setCityState("Dallas, TX");
sr1.setPhone("214-555-1234");
results.add(sr1);
sr1 = new SearchResults();
sr1.setName("Jane Doe");
sr1.setCityState("Atlanta, GA");
sr1.setPhone("469-555-2587");
results.add(sr1);
sr1 = new SearchResults();
sr1.setName("Steve Young");
sr1.setCityState("Miami, FL");
sr1.setPhone("305-555-7895");
results.add(sr1);
sr1 = new SearchResults();
sr1.setName("Fred Jones");
sr1.setCityState("Las Vegas, NV");
sr1.setPhone("612-555-8214");
results.add(sr1);
return results;
}
}
Notice that we first get an ArrayList of SearchResults objects (normally this would be from an external data source...), pass it to the custom adapter, then set up a click listener. The listener gets the item that was clicked, converts it back to a SearchResults object, and does whatever it needs to do.