How do i create a listview in my video application? - java

Can someone tell me how should am i going to create my listview which look similar here.
Problem 1: The listview example i show in the link only caters to 2 file object as you can see in the link but in my own context i need a rather more "dynamic" video_list which increase/decrease with respect to time,
but how am i going to fulfill the sort of look and feel in my codes which has icons, file name, and file size yet at the same time looking clean and simple on each file object??
Problem 2: And why is my listview having error when the directory is empty despite having this to handle the "empty" in my xml
<TextView
android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No File Directory found on SD Card"/>
Can someone guide on this matter because i'm rather new in android/java... Thanks
This is what i tried out so far...
public class ListViewActivity extends ListActivity {
private List<String> videoItems = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videolistview);
getVideoFiles(new File("/sdcard/Video_List").listFiles());
}
public void getVideoFiles(File[] videoList)
{
videoItems = new ArrayList<String>();
for (File file : videoList)
{
videoItems.add(file.getName());
}
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, videoItems));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
}
}
EDITED

To add your list of videos, simply use the following line:
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, Videos));

Related

Getting a button to work in the list view

I am new to android and my code has got a bit messy. I have successfully created a list view extended from item_layout.xml. When I click on the list view It works exactly how I want it. However in each item of the list view I have a button that when clicked I want the item of the list to delete.
When researching I have come across that you need to create a customer adapter to do this however I have come so far in the project that I wouldn't even know where to start.
This code it used successfully to when the list items are clicked it works. This is just put in the Main Activity class
mylist.setOnItemClickListener(
new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
}
}
);
I populate the list using this function just outside the main activity class. It is needed to be written like this as It gets the items from a database and has to be called depending on different circumstances
private void populatelistView() {
Cursor res = userDb.getAllRows();
String[] fromFeildnames = new String[]{ DatabaseUser.KEY_1, DatabaseUser.KEY_2};
int[] toViewIds = new int[]{R.id.textViewNum, R.id.textViewItem};
SimpleCursorAdapter myCursorAdaptor;
myCursorAdaptor = new SimpleCursorAdapter(getBaseContext(), R.layout.item_layout, res, fromFeildnames, toViewIds, 0);
//ListView mylist = (ListView) findViewById(R.id.listViewID);
mylist.setAdapter(myCursorAdaptor);
}
I would like to be able to get the button on each items to work by not changing much of what I have already written. I have tried just using the following code. But because it is in a different xml layout it display an error of null reference towards the item button
delete.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
final int position = listView.getPositionForView(parentRow);
Toast.makeText(getApplicationContext(), "Button " + position, Toast.LENGTH_SHORT).show();
}
}
);
Please could someone help me make the button work without changing much code or give me a step by step tutorial on how to add an adapter but make my populateListView function do the same thing.
Ps. I have looked at so many tutorials about list adapters but can't find ones that are for my specific need

How to open a file the same name as defined in the String?

I'm creating a lyric app and I need some help in coding the next processes I need.
I created a ListView and added some Strings on it.
public class MainActivity extends AppCompatActivity {
String titles[] = new String [] {"Amazing Grace", "How Great Thou Art",
"King of All Kings", "What A Beautiful Name"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView =(ListView) findViewById(R.id.titlelist);
ArrayAdapter<String> adapter=new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,titles);
listView.setAdapter(adapter);
}
}
Now the next step is to create an OnItemClickListener and let's say
if "Amazing Grace" was selected from the list,
it will look for a file the same name as it is defined in the String.
For example : "Amazing Grace.xml" //even with the space included
so the logic will be like : open filelocation/"title that was selected".xml
I can't use "case" since I will be creating lots of song titles and add more as I update the app.
Thanks for reading, I'd really appreciate any help with this ;)
You should map Your lyrics, so when You click on item with pos = 5 You will know what item's ps correlates with what file(or xml).
Here is the sample how to map ids with filenames:
HashMap<String,Strin> lyricsMap = new HashMap<>();
lyricsMap(0, R.raw.song_lyric0);
lyricsMap(1, R.raw.song_lyric1);
lyricsMap(2, R.raw.song_lyric2);
lyricsMap(3, R.raw.song_lyric3);
lyricsMap(4, R.raw.song_lyric4);
lyricsMap(5, R.raw.song_lyric5);
lyricsMap(6, R.raw.song_lyric6);
//..
Here is the sample how to use OnItemClickListener:
AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) {
int rawResId = lyricsMap.get(pos);
//here comes the method for returning lyrics for file by it's resource id
//...
}
};
adapter.setOnItemClickListener(onItemClickListener);
P.S I assume You are not working on database items, otherwise You should use id instead of pos value.
To open file:
int selected = 0; // set selected to index of what is selected
File file = new File(Environment.getExternalStorageDirectory(), //folder location where you store the files
titles[selected]+".xml"); //in case of xml files. If other types, you'll need to add case for diff types
Uri path = Uri.fromFile(file);
Intent fileOpenintent = new Intent(Intent.ACTION_VIEW);
fileOpenintent .setDataAndType(path, "application/xml"); //for xml MIME types are text/xml and application/xml
try {
startActivity(fileOpenintent);
}
catch (ActivityNotFoundException e) {
}
Your biggest issue as you explained it was how to handle multiple file names. That's this part of code: titles[selected]+".xml"

How to perform clicking on the first item in an ArrayList by Espresso

I have tried to click on the first item of my arraylist for few hours, I have read a lot of questions on Stack Overflow, tutorials and I have tried to implement them in many ways, but it doesn't work.
clickView(getSearchActivity().hintListView);
onData(anything())
.onChildView(withId(R.id.hintLayout))
.atPosition(0)
.perform(click());
onData(allOf(is(instanceOf(String.class))))
.atPosition(0)
.perform(click());
onData(instanceOf(ArrayList.class))
.atPosition(0)
.perform(click());
onData(instanceOf(String.class))
.atPosition(0)
.perform(click());
onData(anything())
.inAdapterView(withId(R.id.hintLayout))
.atPosition(0)
.perform(click());
onData(anything())
.inAdapterView(allOf(withId(R.id.hintLayout), isCompletelyDisplayed()))
.atPosition(0).perform(click());
I have to click on the first item of the ListView, that take ArrayList
id of this ListView is hintLayout.
Does anybody know what is the problem ? and where can it be ?
A ListView must be filled with an adapter. After your adapter is finished just add it to your ListView like this:
private void setListAdapter(ListDataAdapter adapter){
lvYourListView.setAdapter(adapter);
}
In order to get the data back out on the OnClick event you need to something like this in you onCreate() method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lvYourListView = (ListView) findViewById(R.id.lvListview);
lvYourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListData data = new ListData();
data = (ListData)parent.getItemAtPosition(position);
doSomethingWithData(data);
}
});
If you need help with the adapter - just let me know. I will need to see more of your code in order to do that.

Android Custom Array Adapter to read CSV files

I am a newbie to android application development. I am currently working on a project which deals with the import of csv files which consists of only one column and many rows , onto my android project. I am able to read those csv files onto my project using csv adapters and array adapters and I didn't use any string arrays. Only used csvadapter and array adapters. Now in the list view where all the csv datas are presented, I am having a concern where when clicked on an item in the list I wanted to toast the item name. I tried out all possible combinations but it displays me only a toast saying string#somerandomvalue. Request someone to help me on this. Thanks in advance.
The following is my csvadapter class. The .csv files are in placed in assets folder..
public class CSVAdapter extends ArrayAdapter<clock>{
Context ctx;
public CSVAdapter(Context context,int textViewResourceId)
{
super (context,textViewResourceId);
this.ctx = context;
loadArrayFromFile();
}
#Override
public View getView(final int pos,View convertView,final ViewGroup parent){
TextView mView=(TextView)convertView;
if(null == mView){
mView = new TextView(parent.getContext());
mView.setTextSize(28);
}
mView.setText(getItem(pos).getTime());
return mView;
}
And this is my class to read that .csv array into GUI
public class mrvtoparanur extends Activity {
CSVAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mrvtoparanur);
final ListView mList = (ListView)findViewById(R.id.mrvtoparanurlist);
mAdapter=new CSVAdapter(this,-1);
mList.setAdapter(mAdapter);
mList.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getApplicationContext(), "you selected item number"+arg2, Toast.LENGTH_SHORT).show();
}});
}
Request someone to help on the Toast in the second class java file. In the msg "YOU SELECTED ITEM NUMBER" the arg2 gives the item id.. In a similar way i want to display or toast the item name instead of item ID. Pls help me someone
This is clock.java file... Please refer below.
public class clock {
private String t;
public String getTime() {
return t;
}
public void setTime(String t) {
this.t = t;
}
}
Use the data from the adapter:
clock clicked = mAdapter.getItem(arg2);
(Then I don't know the structure of the clock object)

Custom Array Adapter Returning Blank Screen?

I'm still very new to application development, so this is probably a very stupid question but I can't seem to find the right answer (or at least one that I can understand with my very limited knowledge of java).
I'm using a custom ArrayAdapter called ListRow. It works fine with a regular Activity, but not with the ListActivity that I need it to be in for my app to work.
Below is a sample of the code that I'm using. Any help would be greatly appreciated and you'd be helping a ton!
ListView mListview;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ListRow(this, THEME_NAMES, THEME_ICONS));
getListView().setTextFilterEnabled(true);
}
public class ListRow extends BaseAdapter {
private Context mContext;
private String[] mThemeNames = THEME_NAMES;
private int[] mThemeIcons = THEME_ICONS;
public ListRow(Context c, String[] t, int[] i) {
mContext = c;
mThemeNames = t;
mThemeIcons = i;
mListview=(ListView)findViewById(R.id.list);
}
#Override
public int getCount() {
return mThemeNames.length;
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(int position, View converView, ViewGroup parent) {
View List;
if(converView==null){
List=new View(mContext);
LayoutInflater mLayoutinflater=getLayoutInflater();
List=mLayoutinflater.inflate(R.layout.list_view, parent, false);
} else {
List = (View)converView;
}
ImageView imageView = (ImageView)List.findViewById(R.id.image);
TextView textView = (TextView)List.findViewById(R.id.text);
imageView.setImageResource(mThemeIcons[position]);
textView.setText(mThemeNames[position]);
return List;
}
}
And here's the layout I've defined for each list item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:id="#+id/image"
android:layout_alignParentLeft="true"
android:contentDescription="#string/preview" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/image" />
</RelativeLayout>
If you can, please use small words with me lol, java has turned out to be hard to understand for me, and also try to explain as much as you can. Thanks in advance!
FIGURED IT OUT!
So I just put you all through a bit of hell. The layout that contains my list items is called list_item, not list_view. However I have learned a lot here so THANK YOU ALL VERY MUCH! I wish there were a way I could help you guys out...
Moral of this question? CHECK YOUR LAYOUT NAMES!!
You need to set The Adapter in this way
setListAdapter(new ListRow(this, your_theme_names_array, your_theme_icon_array));
You dont need to use ArrayAdapter for this, that is just for Creating a Adapter for an array of String
EDITED
The Layout XML does not have the problem i think.
Check the List given below one by one
Check List
Check Whether R.layout.list_view point to the layout you given in the Question.
Try this for setting adapter setListAdapter(new ListRow(this, String[] { }, int[] { })); it will show you blank screen (If you get the Blank Screen that means either THEME_NAMES or THEME_ICONS is null or their values is null)
Remove the Line imageView.setImageResource(mThemeIcons[position]); and
textView.setText(mThemeNames[position]); this will also give u blank screen (If you get blank screen then R.layout.list_view does not contain R.id.image or R.id.text.
You have to add your mListView in your ArrayAdapter in setListAdapter.Only then the contents of your listview will be display in the pattern you have mentioned in customadapter. I cannot see where you have added elements in listview.

Categories

Resources