What am trying to accomplish here is create a listview or recyclerview if you like, to set UI views on the screen instead of using a scrollview, this concept is pretty popular among iOS developers where they create a table view and pass it views in an NSArray. I have done that part so far, problem is the listeners don't work if they are set in the activity before putting them in an arraylist to be passed to the list/recycler, here's my code so far.
recyclerView = (RecyclerView) findViewById(R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
views = new ArrayList<>();
d = new DatePicker(this);
views.add(d);
e = new TextInputEditText(this);
e.addTextChangedListener(this);
views.add(e);
ee = new TextInputEditText(this);
ee.addTextChangedListener(this);
// views.add(ee);
Button b = new Button(this);
b.setText("hi btn");
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "hi", Toast.LENGTH_SHORT).show();
}
});
views.add(b);
Adapter adapter = new Adapter(views, this);
recyclerView.setAdapter(adapter);
Related
I have 2 Activities. Activity A contains recyclerview, and Activity B is going to pass the Arraylist data to recyclerview in Acrivity A.
my Adapter is like this.
public MainAdapter(MainActivity context,ArrayList<MainData> list) {
this.context = context;
this.list = list;
}
and MetaData.
public MainData(String tv_name, String tv_content) {
this.tv_name = tv_name;
this.tv_content = tv_content;
}
Activity A (that receives arraylist data):
recyclerView = (RecyclerView) findViewById(R.id.rv);
linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
list = new ArrayList<>();
list = (ArrayList<MainData>)getIntent().getSerializableExtra("key");
adapter = new MainAdapter(this,list);
recyclerView.setAdapter(adapter);
AAnd Activity B that pass Arraylist :
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(upload.this, MainActivity.class);
ArrayList<MainData> list = new ArrayList<MainData>();
EditText edit = (EditText) findViewById(R.id.edittext_name);
String tv_name = edit.getText().toString();
EditText edit_main = (EditText) findViewById(R.id.edittext_main);
String tv_content = edit_main.getText().toString();
list.add(new MainData(tv_name,tv_content));
intent.putExtra("key", list);
startActivity(intent);
}
});
And it throws Runtime Exception.
The context you provided is not complete, please review the Android guide. If you need to transfer data between activities, please check this article
enter link description here
You should use Intent extras to pass the the array list from ActivityB to ActivityA and in the ActivityA you have to pass the list to the adapter so it will set the data on the recyclerView.
You shouldn't directly pass the data from ActivityB to the adapter of AdapterA
This might be helpful: https://developer.android.com/reference/android/content/Intent
I have a listview that shows up when a fragment is opened being updated by a saved list. Initially that list is empty so it spits out an error java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0. So i want to show show something like a text saying something like empty so that when there is something in the list it will display. I have tried using the getEmptyView but to no avail. Can i please get some help on how to accomplish this thank you.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View customView = inflater.inflate(R.layout.fragment_reading_monday, container, false);
addNewButton = (Button) customView.findViewById(R.id.btnAddNewReadingMonday);
relativeLayoutMonday = (RelativeLayout) customView.findViewById(R.id.frameLayoutMonday);
listViewMonday = (ListView)customView.findViewById(R.id.listViewMonday);
listContainerMonday = (RelativeLayout)customView.findViewById(R.id.listContainerMonday);
tinyDB = new TinyDB(getContext());
addNewSubject = (Button)customView.findViewById(R.id.btnAddNewSubjectMonday);
dataModels = new ArrayList<>();
arrayListSubjectsRead = new ArrayList<>();
timeOne = new ArrayList<>();
timeTwo = new ArrayList<>();
adapter = new CustomListAdapterReading(getContext(), dataModels);
TextView empty = (TextView) customView.findViewById(R.id.emptyTextView);
listViewMonday.setEmptyView(empty);
arrayListSubjectsRead = tinyDB.getListString("ArrayForSubjects");
timeOne = tinyDB.getListString("ArrayForTimeOne");
timeTwo = tinyDB.getListString("ArrayForTimeTwo");
dataModels.add(new DataModelReading(arrayListSubjectsRead, timeOne, timeTwo));
adapter = new CustomListAdapterReading(getContext(), dataModels);
listViewMonday.setAdapter(adapter);
adapter.notifyDataSetChanged();
System.out.println(" subjects: "+arrayListSubjectsRead);
System.out.println("timeone: "+ timeOne);
System.out.println("timetwo: "+timeTwo);
addNewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
relativeLayoutMonday.removeView(noEventTextView);
Intent intent = new Intent(getActivity(),ReadMain.class);
startActivity(intent);
}
});
addNewSubject.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
return customView;
}
I want you to check this post setEmptyView on ListView not showing its view in a android app, it should solve your empty view issue.
And if you want to prevent the app from throwing an IndexOutOfBoundsException, you can simply check the size of your dataModels array before setting up your adapter.
if (dataModels.size() > 0)
{
//Setup our adapter here
}
Hope this helps :)
Something like this ?
ImageListAdapter imageListAdapter= new ImageListAdapter();
listviewObject.setAdapter(imageListAdapter);
if (imageListAdapter!= null)
if (imageListAdapter.getCount() > 0){
// implement your work
}else {
// do whatever you want on empty list adapter
}
I am using this way in my application
ListView listView = (ListView)findViewById(android.R.id.listView);
TextView noData = (TextView)findViewById(android.R.id.noData);
listView.setEmptyView(noData);
Listview will automatically set no data text when adapter is empty.
I started this program by reading in a premade database and has it display on a list, i am currently trying to convert the list to a clickable list that opens up to a new fragment. This is the code i am currently running. The commented out section is the current problem.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 =(Button) findViewById(android.R.id.button1);
db = new MyDatabase(this);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
schedule = db.getSchedule();
ListView listView = (ListView) findViewById(R.id.list);
//listView.setOnItemClickListener(this);
listView.setAdapter(new SimpleCursorAdapter(
MainActivity.this,
R.layout.row,
schedule,
new String[] {"fName", "Calories", "Protein", "Carbs" },
new int[] { R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4 },
0));
schedule = db.getSchedule();
ListView listView2 = (ListView) findViewById(R.id.list2);
listView2.setAdapter(new SimpleCursorAdapter(
MainActivity.this,
R.layout.row,
schedule,
new String[] {"fName", "Calories", "Protein", "Carbs" },
new int[] { R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4 },
0));
}
});
}
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Log.i("HelloListView", "You clicked Item: " + id + " at position:" + position);
// Then you start a new Activity via Intent
Intent intent = new Intent();
intent.setClass(this, Activity2.class);
intent.putExtra("position", position);
// Or / And
intent.putExtra("id", id);
startActivity(intent);
}
listView.setOnItemClickListener(this); will not work because it is being applied to android.view.View.OnClickListener. I have been trying to modify b1.setOnClickListener(new View.OnClickListener(), to try to change its parameters, but I'm not getting any luck. Is there an easy solution I'm not seeing or do i need to reconstruct this code to revolve around a clickable list. Thanks!
Simple, put below code lines in onCreate() of Activity instead of button's onClick() And then implement AdapterView.OnItemClickListener to your Activity.
ListView listView = (ListView) findViewById(R.id.list);
listView.setOnItemClickListener(this);
Now on line listView.setOnItemClickListener(this); this refers to your activity class and implemented interface and onItemClick will work.
If your activity is a ListActivity then no need of listView.setOnItemClickListener(this); this line.
Change the Activity to ListActivity and override the method onListItemClick of ListActivity class. Put all the code in the onItemClick of listener to
onListItemClick.
In my Main.class I am creating a dynamic ScrollView and starting it using setContentView(sv) it displays a Spinner and a Button.
The user selects an option from the Spinner and then clicks the Button. I have set a setOnClickListener to the Button.
In the onClick method I am trying to start up a new ContentView. Calling a new class file with the same type of layout as Main.class. Not sure how to go about doing this. I added finish() to the onClick method and it closes the original window but not sure how to open the new one.
Here is the Main.class code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.messages = new Messages(this);
this.datasource = new FacilitiesDataSource(this);
this.datasource.open();
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
Spinner s = new Spinner(this);
s.setId(SPINNER_FACILITIES);
s.setLayoutParams(new Spinner.LayoutParams(-2,-1));
final List<SpinnerObject> list = this.datasource.getFacilitiesList();
final ArrayAdapter<SpinnerObject> adapter = new ArrayAdapter<SpinnerObject>(this, android.R.layout.simple_spinner_dropdown_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
ll.addView(s);
Button b = new Button(this);
b.setText("Submit");
b.setLayoutParams(new LayoutParams(-2,-1));
b.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
Spinner spin = (Spinner)findViewById(SPINNER_FACILITIES);
Log.v("option picked", Integer.toString(( (SpinnerObject) spin.getSelectedItem () ).getId () ));
/*
* NEED HELP HERE
* NEED HELP HERE
* NEED HELP HERE
*/
//finish();
}
});
ll.addView(b);
setContentView(sv);
}
Here is my Vehicles.class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("Started onCreate", "Vehicle");
ScrollView sv = new ScrollView(this);
setContentView(sv);
}
I assume you mean you want to start a new Activity (from Main to Vehicles). In that case you can use the following code:
Intent intent = new Intent(Main.this, Vehicle.class);
startActivity(intent);
Make sure all Activity classes are declared in your Android Manifest file, inside the <application> tag, like this:
<activity android:name=".Vehicles" />
I am working on android media player. In main.java it includes main.xml and list.java includes
list.xml , I used intent to call list.java (when I press imagebutton) into main.java , But when I press imagebutton list.xml comes up new window I want to show in bottom of the main.xml
In main.java image button calls list.java into main.java
songslist_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(main.this, list.class);
Toast.makeText(main.this, "Song List", Toast.LENGTH_SHORT).show();
startActivityForResult(i, 100);
//Intent i = new Intent(main.this, list.class);
//startActivity(i);
}
});
/////////////////////////
public class list extends ListActivity
{
// Songs list
public ArrayList<HashMap<String, String>> songsLists = new ArrayList<HashMap<String, String>>();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.list);//list.xml
ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
SongList plm = new SongList();
// get all songs from sdcard
this.songsLists = plm.getPlayList();
// looping through playlist
for (int i = 0; i < songsLists.size(); i++)
{
// creating new HashMap
HashMap<String, String> song = songsLists.get(i);
// adding HashList to ArrayList
songsListData.add(song);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, songsListData,
R.layout.song_item, new String[] { "songTitle" }, new int[] {R.id.song_title });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
//lv = (ListView) findViewById (R.layout.list);
// listening to single listitem click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
// getting listitem index
int songIndex = position;
// Starting new intent
Intent in = new Intent(getApplicationContext(),main.class);
// Sending songIndex to PlayerActivity
in.putExtra("songIndex", songIndex);
setResult(100, in);
// Closing PlayListView
finish();
}
});
}
}
Is Your main.java class an activity? Your list ist even an activity, so You started a new one, that will be shown in a new window. I think You need another aproach to do this. Read this tutorials first:
http://www.vogella.com/articles/AndroidListView/article.html
http://windrealm.org/tutorials/android/android-listview.php
A possibillity to do such thing is to integrate a simple listView into your main.xml layout and hide this view until button will be pressed.
EDIT
for example:
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/mainListView">
</ListView>
put this into Your main.xml, maybe below Your button. Then You can reference the listview in your main.java:
mainListView = (ListView) findViewById( R.id.mainListView );
But there is a lot more to do, so I recommend You to read the tutorials to get a clear conception about how to build a listView.