Multiple ListViews, with same content, on different layouts - android

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

Related

combining 2 activities together

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

How to add two buttons in listview

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.

Edit Text and put it to List View

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();
}
});

Fragment dialog in particular row in android listview

Here is what I have done till now.
// Declaration
NearByFriendsAdapter adapter;
ListView lst_NearByFriends;
// Initialization
lst_NearByFriends= (ListView) getView().findViewById(R.id.lst_NearbyFriends);
list=new ArrayList<>();
for(int i=0;i<=10;i++)
{
NearByFriendsSupportClass nearByFriendsSupportClass=new NearByFriendsSupportClass();
nearByFriendsSupportClass.setFriendsName("Chirag Solanki");
list.add(nearByFriendsSupportClass);
}
adapter=new NearByFriendsAdapter(getActivity(),list,getFragmentManager());
lst_NearByFriends.setAdapter(adapter);
Here is my adapter with holder
holder.img_Infoimage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
InfoAlertDialog alertDialog=new InfoAlertDialog();
alertDialog.show(fragmentManager, NearByFriendsFragment.class.getName());
}
});
private static class NearbyHolder{
ImageView img_Infoimage;
}
I think this is enough to understand my problem.
Now when any user click on any row in listview, I want to open fragment dialog on that particular row. so please help me what to do to solve my problem.
Register an OnItemClickListener on your listview.

Deleting data from SQLite using listview by clicking Delete Button

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.

Categories

Resources