Items in a ListActivity - java

In my ListActivity I have three different options that are clickable. In my "row.xml" I have place an ImageView, but am not sure how to implement it into my Activity so that each selectable item has a different image. Would it be another Adapter?
Here is my ListActivity
public class StreamCasts extends ListActivity {
private static final String[] items={ "item1", "item2", "item3"
};
public void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.heroselect);
setListAdapter(new ArrayAdapter<String>(this,
R.layout.row, R.id.label,
items));
}

You'll need to create a custom ArrayAdapter that lets you customize the view returned on the overrided getView.
Take a look at http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/ but a general search for 'custom arrayadapter' will give you the start of this.
Get used to it as this is a very common pattern with complicated listviews.

Related

I want only one custom Toolbar for all activities I have tried many solutions but none of them worked

BASE ACTIVITY
public class BaseActivity extends AppCompatActivity {
Spinner main_spinner;
String[] OptionsSp = {"Identify Species", "Laws and Sections", "Court Details", "Investigating Officer", "Accused Information", "Offense Information",
"Operation Detail", "Seizure List", "Arrest Procedure", "Bail", "Prayer",
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mytoolbar);
Toolbar toolbar = findViewById(R.id.myToolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
main_spinner = findViewById(R.id.main_spinner);
ArrayAdapter SpAdapter = new ArrayAdapter(BaseActivity.this, android.R.layout.simple_spinner_item, OptionsSp);
SpAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
main_spinner.setAdapter(SpAdapter);
}
}
CLASS WHERE I WANT TO GET THE CUSTOM TOOBAR
public class SpeciesAct extends BaseActivity implements AdapterView.OnItemSelectedListener {
Spinner main_spinner;
int pos = 99;
ImageView backbnofmain;
String[] OptionsSp = {"Identify Species", "Laws and Sections", "Court Details", "Investigating Officer", "Accused Information", "Offense Information",
"Operation Detail", "Seizure List", "Arrest Procedure", "Bail", "Prayer",
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
}}
AND I AM NOT GETTING VALUES IN SPINNER ALSO
NOTE: I don't want to use include layout in all activities because ,I want spinner filled only once and can be reusable in all activities from toolbar otherwise I have to find ids and do duplicate work everytime.
If your use case is a Single Activity Application, which it seems like, you might be better suited to using Fragments. That way you will have the Single Toolbar in your Activity, and fragments underneath it. Based on the Fragment you can also set different state in your Activity's Toolbar.
You can read about Fragments here

Adding checkboxes into a ListView

Hello I'm currently trying to make my first app on android and I've gotten some problems. I'm trying to make a todo-list app so want some kind of input to be transformed into checkboxes. I've made it work with radio buttons using radioGroup. But when using Checkboxes with ListView it just doesn't work.
Here's my code:
public class MainActivity extends AppCompatActivity {
EditText t;
ListView listView;
ArrayList<CheckBox> checkList = new ArrayList<>();
int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void add(View view){
t = findViewById(R.id.input);
String s = t.getText().toString();
CheckBox check = new CheckBox(this);
checkList.add(check);
listView = findViewById(R.id.list);
checkList.get(i).setText(s);
listView.addView(checkList.get(i));
i++;
t.setText("");
}}
The app crashes saying something about adapterView
You're unfortunately doing something wrong here. You are trying to add your view to Listviews on
listView.addView(checkList.get(i));
This is not the correct way to use ListViews. You have to use an adapter. The adapter would need to provide data for the listview to load.
Please have a look at this tutorial for how to use listviews.
Below is a summary of the steps to use a listView correctly.
Create a new layout file (say custom_cell.xml) inside your layouts folder.
Insert a checkbox inside your custom_cell.xml and place an Id which you can use later to identify the checkbox
Create an adapter for your listview
Override the getView method
Inside the getView method, inflate a new cell using the custom_cell.xml or reuse an existing cell if provided
Reference the Checkbox by using the Id you provided in the custom_cell.xml file

Android Program (Adding items to ListView in another activity)

I am curious how I would go about adding an an editText variable to a list in another activity. I have created an arrayList that names are stored into as they are entered in the editText line. These are stored in the nameList variable in the class StartingActivity. How would i go about adding and showing these names into a ListView in another activity and class called ListActivity as shown. Thank you. Also, how do i refractor the names of classes and files without it just basically not making my program runable.
Code:
Class StartingActivity:
public void onClick_btnConfirm {
EditText editName = (EditText) findViewById(R.id.editName);
TextView textOutput = (TextView) findViewById(R.id.textOutput);
Intent intentList = new Intent(StartingActivity.this, ListActivity.class);
if(editName.length()!=0) {
nameList.add(editName.getText().toString());
editName.setText("");
}
//intentList.putExtra("Name", editName.toString());
startActivity(intentList);
}
Class ListActivity:
public class ListActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
ListView listView = (ListView) findViewById(R.id.listAthletes);
final StartingActivity nameList = new StartingActivity();
nameList.getNameList();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, nameList.getNameList());
listView.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
}
}
You cannot run findviewbyid() for an id in another Activity.This answer might be of help to you. If that is not what you really want to go with, you can do two things:
Send Intents with the string.
Use a sharedPreference and write the string to a sharedPreference. SharedPreferences also have an OnChangeListener, so you will be able know if the preference has changed. In the onResume() of that activity , you can just read from the sharedPreference, and update the UI
Also take a look at AsyncTask here
As mentioned , you can also use the SQLite Database

Add tab view android

I have made a simple contact apps and now I wish to add tab view to this app. I am following the tutorial here. Below is part of the source code for my MainActivity.jave:
public class MainActivity extends ListActivity {
private ListView contactListView;
private CursorAdapter contactListViewAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contactListView = getListView(); // get ListView id
contactListView.setOnItemClickListener(viewContactListListener);
String[] from = new String[] { "familyName" }; // built an String array
// named "from"
int[] to = new int[] { R.id.contactTextView };
contactListViewAdapter = new SimpleCursorAdapter(MainActivity.this,
R.layout.contact_list_item, null, from, to);
setListAdapter(contactListViewAdapter); // set adapter
}
I tried to change "public class MainActivity extends ListActivity" to "public class MainActivity extends TabActivity implements OnTabChangeListener{". However, I get the error "The method getListView() is undefined for the type MainActivity" and other similar errors. I need suggestions on how to fix this. Thanks for any help.
ListActivity provides some help methods to manage ListView so if you want to change your parent activity from ListActivity to TabActivity you need to handle ListActivity behaviour yourself.
Basically you need to get and store somewhere the ListView object. Something like mListView = (ListView) findViewById(android.R.id.list) on your onCreate method and then implement missing method
ListView getListView()
{
return mListView;
}
Also setting your adapter will be a bit different. Instead of calling setListAdapter(contactListViewAdapter); // set adapter there should be contactListView.setAdapter(contactListViewAdapter);

ListView in xml, setting Adapter to

I have following code:
public class ShowActivity extends ListActivity implements OnItemClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.show_list);
ListView lv = getListView();
String[] projection = new String[]{
BaseColumns._ID,
DepotTableMetaData.ITEM_NAME,
DepotTableMetaData.ITEM_AMOUNT,
DepotTableMetaData.ITEM_UNIT,
DepotTableMetaData.ITEM_PPU,
DepotTableMetaData.ITEM_TOTAL,
DepotTableMetaData.ITEM_COMMENT};
Cursor c = managedQuery(ContentProviderMetaData.DepotTableMetaData.CONTENT_URI, projection, null, null, ContentProviderMetaData.DepotTableMetaData.DEFAULT_SORT_ORDER);
String[] columns = new String[]{BaseColumns._ID,ContentProviderMetaData.DepotTableMetaData.ITEM_NAME, ContentProviderMetaData.DepotTableMetaData.ITEM_AMOUNT, ContentProviderMetaData.DepotTableMetaData.ITEM_UNIT, ContentProviderMetaData.DepotTableMetaData.ITEM_PPU, ContentProviderMetaData.DepotTableMetaData.ITEM_TOTAL, ContentProviderMetaData.DepotTableMetaData.ITEM_COMMENT};
int[] to = new int[]{R.id.lname,R.id.lamount,R.id.lunit,R.id.lppu,R.id.ltotal,R.id.lcomment};
Log.d("ShowActivity","Cursor, columns, to set - now setting adapter");
SimpleCursorAdapter simpleadapter = new SimpleCursorAdapter(this,R.layout.list_entry, c, columns, to);
// ListView lv = (ListView)findViewById(R.id.ListView);
this.setListAdapter(simpleadapter);
lv.setOnItemClickListener(this);
// intentCheck();
}
It works fine, but as you might have seen at my comments, i don't want to create the listview via getListView(), i want to do a usual setContentView(R.Layout.---) and populate a Listview, which is defined there. But all tutorials I have seen yet do it like that, and I have no clue how to change that to my wishes.
How does my layout-xml have to look like? how do i "tell him to use it" ?
Thanks in advance.
You can use directly setContentView(R.layout.main); on a ListActivity as long as you define a listView with the android:list id on your xml:
<ListView android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
To set the adapter call directly setListAdapter from your ListActivity
Don't use ListActivity as your base class, extend Activity, set your content to the layout, grab a reference to your expanded ListView (findViewById(...)) and then handle setting the adapter that way.
ListView lv = (ListView)findViewById(android.R.id.list);
No change needed in the .xml

Categories