I would like to add two buttons in my listview.
Two button edit and delete for my list.
I have already traveled the other posts on this subject but I still can not :(.
I have already created a custom listview.
I am a beginner so I have a hard time understanding.
This would be nice a little help.
Here is my code
My current listview
public class liste_offre extends AppCompatActivity {
DatabaseHelper myDb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.liste_offre);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ListView listView = (ListView) findViewById(R.id.List_offre);
myDb = new DatabaseHelper(this);
ArrayList<String> theList = new ArrayList<>();
Cursor res = myDb.getAllData();
if (res.getCount()==0){
Toast.makeText(liste_offre.this,"Liste vide",Toast.LENGTH_LONG).show();
}else {
while (res.moveToNext()){
theList.add(res.getString(1));
ListAdapter listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,theList);
listView.setAdapter(listAdapter);
}
}
}
}
Please refer the below post it may be exactly what you want
ListView with Add and Delete Buttons in each Row in android
Hope this helps you.
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 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"...
Multiple ListViews, with same content, on different layouts
So basically what I have is two ListViews that are getting their content from SQLite DB. I have created a BaseActivity below to extend my other activities to access the same data. The problem I ran into is that I cannot display the data because their are two different layout that contain these ListViews, one in a Dialog and the other in a TabWidget, that are both in separate activities.
So basically....
I need to know how to display two ListViews with the same data that are in different activities (one in dialogBox and the other in TabWidget)
The error I am currently getting is from the layout in the SimpleCursorAdapter is only for one of the ListViews and it wont add the other because it cannot find the View
I am not extending ListActivity at any point
Thank you very much in advance. I will be standing by to edit or clarify.
Part of my Base Activity
public class BaseActivity extends Activity
{
private SimpleCursorAdapter contactAdapter;
public static final String ROW_ID = "row_id";
private static ListView study_guide_list_view;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String[] from = new String[] { "name" };
int[] to = new int[] { R.id.study_guide_item_in_listview };
contactAdapter = new SimpleCursorAdapter(BaseActivity.this, R.layout.study_guide_item_in_listview, null, from, to);
}
}
This segment is where I add the ListView to the TabWidget and it is currently working
study_guide_list_view = (ListView) findViewById(R.id.list);
contactAdapter = getSimpleCursorAdapter();
study_guide_list_view.setAdapter( contactAdapter );
study_guide_list_view.setOnItemClickListener(listview_item_listener);
Where I am trying to add the ListView in the Custom Dialog Box (does not work: error is on study_guide_dialog_list_view.setAdapter( contactAdapter ); )
public OnClickListener save_slide_page_to_guide_btn_listener = new OnClickListener()
{
#Override
public void onClick(View v)
{
TabbedPagesActivity.getListViewAdapter();
dialog = new Dialog(PDFViewerActivity.this);
dialog.setContentView(R.layout.study_guide_custom_dialog_box);
dialog.setTitle("Select a Study Guide");
dialog.setCancelable(true);
study_guide_dialog_list_view = (ListView) findViewById(R.id.list);
contactAdapter = getSimpleCursorAdapter2();
study_guide_dialog_list_view.setAdapter( contactAdapter );
study_guide_dialog_list_view.setOnItemClickListener(listview_item_listener);
Button dialog_ok_btn = (Button) dialog.findViewById(R.id.dialog_ok_btn);//it says cancel though
dialog_ok_btn.setTextSize(20);
dialog_ok_btn.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/AGENCYR.TTF"));
dialog_ok_btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
dialog.dismiss();
}
});
dialog.show();
}
};
Wow so I finally figured it out. The problem lies within the custom Dialog. Instead of calling...
study_guide_dialog_list_view = (ListView) findViewById(R.id.list);
it needs to be....
study_guide_dialog_list_view = (ListView) dialog.findViewById(R.id.list);
If you do not do this the findViewById will return null, hence the NullPointerException
I'd like to refresh the Listview items. These items are populated from SQLite database. My code is below
public class Weeve extends Activity {
private String[] lv_arr;
protected ListView CView;
private DBAdapter mDbHelper;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new DBAdapter(this);
mDbHelper.open();
Cursor c = mDbHelper.getAll();
if (c.getCount() > 0)
{if (c.moveToFirst())
{
ArrayList strings = new ArrayList();
do {
String mC = c.getString(0);
strings.add(mC);
} while (c.moveToNext());
lv_arr = (String[]) strings.toArray(new String[strings.size()]);
}
}
else Toast.makeText(this,
"No more Records",
Toast.LENGTH_SHORT).show();
c.close();
ListView CView = new ListView(this);
CView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, lv_arr));
setContentView(CView);}}
I'd like to make refreshing this list view after adding, updating or deleting SQLite table. These operations are called by content or option menu. I tried to create these code into a separated function and call it after every operation. But can't. I think setContentView(CView) statement.
I also tried to use SimpleCursorAdapter like notepad sample from Android.com. I got Thread error. Help me.
You can do that using Adapter.notifyDataSetChanged().
However, I find your code slightly off. You're fetching rows from the database using a cursor, just to create a new ArrayAdapter for the row values. Why not use a CursorAdapter to back your list? That's what it's meant for.
make sure all your activities are in the manifest