I have a class that extends ListActivity with a SimpleAdapter as the list adapter. My code looks like this:
public class ListOfFirms extends ListActivity {
Intent extras;
int time;
String km;
ArrayList<String> firms = new ArrayList<String>();
SimpleAdapter adapter;
static final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firms);
extras = getIntent();
time = extras.getIntExtra("time", 0);
km = extras.getStringExtra("km");
adapter = new SimpleAdapter(
this, list, R.layout.taxi_custom,
new String[] {"name","price"},
new int[] {R.id.taxi_name,R.id.taxi_price});
initializeFirm();
setListAdapter(adapter);
}
My question is how I can add a button to each element in the list, the button should be floating to right. My list contains object of the class Firm, how can I know which object that I grab out from the list, when a user presses this button?
here is example of custom listview which may help you
use custom adapter....
and set
listview.setAdapter(adapter);
You will have to write a CustomAdapter which extends BaseAdapter.
If you can use OnTouchListener and OnLongClickListener instead of a button it is a little easier to implement. Also if you just want to select the item, it ies easier tu use the standard built-in Android mechanisms.
Only if you REALLY need a button on each list item you have to do it like Gaurav Agarwal suggested... - which is something you might have to do sooner or later anyway :-)
Related
ok so I'm making an app where I use an edit text to write anything and when I press a button it adds whatever I wrote in the edit to a listView, and it works, but I want the List to be in a different activity than the button and edit text, so I moved it without changing the code.can anyone figure this out,
BTW all the variables are public.
public class MainActivity extends AppCompatActivity {
public ArrayList<String> arrayList2;
public ArrayAdapter<String> adapter,adapter2;
public EditText editText,editText2;
public ArrayList<String> itemList,itemList2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] items = {};
itemList = new ArrayList<String>(Arrays.asList(items));
adapter = new ArrayAdapter<String>(this, R.layout.activity_list_layout,R.id.txtItem,itemList);
ListView listV = (ListView)findViewById(R.id.list_layout);
listV.setAdapter(adapter);
editText = (EditText)findViewById(R.id.thingadd);
Button btAdd = (Button)findViewById(R.id.add);
String[] age = {};
arrayList2 = new ArrayList<String>(Arrays.asList(age));
itemList2 = new ArrayList<String>(Arrays.asList(age));
adapter2 = new ArrayAdapter<String>(this, R.layout.activity_list__layout2,R.id.txtage,itemList2);
ListView listV2 = (ListView)findViewById(R.id.Age);
listV2.setAdapter(adapter2);
editText2 = (EditText)findViewById(R.id.agetxt);
btAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String newItem=editText.getText().toString();
String newItem2=editText2.getText().toString();
itemList.add(newItem);
itemList2.add(newItem2);
adapter.notifyDataSetChanged();
adapter2.notifyDataSetChanged();
}
});
}
}
You can use SQLite Database for that, where you would be saving data in database from one activity and reading it and displaying it in listview from another.
You have 2 options to do so.
You can use any database service. Store the values and display them in the next activity. But this needs Internet service to be enabled in the phone.
You can use intent. Convert those input to string type and pass them to next activity and display them using ids. This type do not require Internet service. To know more about this, Visit this link
I'm kinda new to Android Studio so I will be grateful if you can help me with my question. How can I enter some text and add it to a ListView with the use of a Bundle?
For example, say I enter a name in the EditText component in the MainActivty, and then when I press the OK button, it will be seen in to another Activity in a List View.
I've been using Bundles to transfer text to another Activity but I can't figure out how to transfer text to ListView.
If all you want to do is display strings in your list items, you can do it fairly simply, otherwise you will have to make a custom adapter. For the former, this is what you will want to do
Create an ArrayList<String> where you will store your values that are entered from the EditText.
Create an ArrayAdapter<String> that will serve to connect the ArrayList to the ListView.
Your final code will look something like this:
public class MainActivity extends Activity {
private ListView listView;
List<String> strings;
ArrayAdapter<String> arrayAdapter;
public void onCreate(Bundle saveInstanceState) {
setContentView(R.layout.yourLayout);
listView= (ListView) findViewById(R.id.yourListView);
strings = new ArrayList<String>();
strings.add("list item 1");
strings.add("list item 2");
arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
strings);
listView.setAdapter(arrayAdapter);
}
}
Then in the onClick of the "OK" button simply add the string from the EditText to the arrayList, and notify the listView that it should be updated be calling this:
arrayAdapter.notifyDataSetChanged();
I don't think you can use Bundles for what you want to achieve. Here's how I do it...
First you need to set up your list. I would use global variables:
ListView listView;
EditText editText;
ArrayList<String> strings;
ArrayAdapter<String> adapter;
Now, in onCreate() , you can do...
listView = (ListView) findViewById(R.id.your_listview);
editText = (EditText) findViewById(R.id.your_edittext );
strings = new ArrayList<>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);
listView.setAdapter(adapter);
You will need to set the OnClickListener for your Button. I would do this in onCreate() too:
Button button = (Button) findViewById(R.id.your_button);
button .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Here we get the text from the EditText component
String text = editText.getText().toString();
// Now add it to the list
strings.add(text);
// And finally, update the list
adapter.notifyDataSetChanged();
}
});
I'm using a listview to load all database items inside it, and it works. The problem is everytime i recall that activity my listview is populated my duplicate items.
Here the code:
public class UserActivity extends ActionBarActivity{
public static ArrayList<String> ArrayofName = new ArrayList<String>();
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_activity);
DataHandler db = new DataHandler(this);
List<Data> items = db.getTitle();
listView = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, ArrayofName);
listView.setAdapter(adapter);
}
e.g. if i insert item "Facebook" it will be inserted inside my database, when i call UserActivity it will load database content(in this case Facebook item) and show all its content inside my listView. The problem is when i do this my listView show me 2 Facebook item. If i have 2 items, when i recall UserActivity it will show me 4 items.
What's wrong?
Thanks
The problem is with
public static ArrayList<String> ArrayofName = new ArrayList<String>();
It should not be static else this will be crated once and it will not initialize on the next activity recreate.Remove the static keyword and write like this-
public ArrayList<String> ArrayofName = new ArrayList<String>();
OR
Ensure the ArrayList is having no elements when you satrts populating from the database.
There are two solutions:
1.) Remove the static keyword from
public static ArrayList<String> ArrayofName = new ArrayList<String>();
If you can't remove the keyword for any reason, use solution 2.).
2.) Call the method clear() of your ArrayList before adding new entries.
Okay, so my supervisor wants me to do this:
The third screen should be accessible via the "View All" button. It should show all the records that were added. Then beside each name, there should be a Delete button for removing the record from the SQLite database.
Please refer to this link to see what I'm talking about since I'm not good in expressing myself in English.
I've already made a way to show all the data that are saved in the database, but I really have no idea on how to automatically put an Delete button beside the data and delete it by clicking it.
Here's my code: (If there are more simple codes that you can suggest, then I'll be more glad.)
public class CheckData extends ListActivity implements OnClickListener {
TextView selection;
public int idToModify;
DataManipulator dm;
List<String[]> list = new ArrayList<String[]>();
List<String[]> names2 =null ;
String[] stg1;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.check);
dm = new DataManipulator(this);
names2 = dm.selectAll();
stg1=new String[names2.size()];
int x=0;
String stg;
for (String[] name : names2) {
stg = name[1];
stg1[x]=stg;
x++;
//ONCLICK
View homeonviewall = findViewById(R.id.homeonviewall);
homeonviewall.setOnClickListener(this);
View newdataonviewall = findViewById(R.id.newdataonviewall);
newdataonviewall.setOnClickListener(this);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,stg1);
this.setListAdapter(adapter);
selection=(TextView)findViewById(R.id.selection);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.homeonviewall:
Intent a = new Intent(this, Dbsample.class);
startActivity(a);
break;
case R.id.newdataonviewall:
Intent b = new Intent(this, SaveData.class);
startActivity(b);
break;
}
}
}
1) Make a layout to display the name and the Delete button. Pass the layout to your adapter, where you currently use android.R.layout.simple_list_item_1, for example
new ArrayAdapter<String>(this, R.layout.row_layout_delete, stg1);
2) Extend your Adapter and override getView() to add an OnClickListener to your button. Inside onClick() simply delete the current row.
I recommend using a CursorAdapter, like SimpleCursorAdapter, they are specifically designed to link database information to a ListView.
This answer covers an extra topic, but I provide details on how to extend an Adapter and implement an OnClickListener: How to attach multiple touch actions to a single list item?, the "quick and dirty" answer should help you. You should also watch the Google I/O presentation Turbo-Charge Your UI to get the most from your Adapters.
I am just starting out using Android, so this is probably a very basic question.
I have created an array called priorityNames. I want to display that in a list and be able to make a selection from that list. At this stage, I cannot get the list to display.
As a starting point I used a short example from windrealm.org tutorials
Any help would be appreciated. Also if anyone can point me to a better example it would be appreciated.
public class SimpleListViewActivity extends Activity {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, R.array.priorityNames);
mainListView.setAdapter(listAdapter);
}
}
You are passing just an id with R.array.priorityNames.
Use:
getResources().getStringArray(R.array.priorityNames)
You are probably missing a TextView in your xml layout (simplerow.xml) with id:
...android:id="#android:id/text1"...