I have two activity (DetailProduct and Wishlist) and i want to display data from DetailProduct to Wishlist, in here i using SharedPreferences for put data from DetailProduct to Wishlist.
This is my activity DetailProduct
wishlist.setOnClickListener(new View.OnClickListener() {
String imgpicaso = getIntent().getStringExtra("aaa");
String vardetailed = getIntent().getStringExtra("acb");
String thisname = getIntent().getStringExtra("name");
String text = getIntent().getStringExtra("abb");
#Override
public void onClick(View view) {
Intent tambah = new Intent(DetailProduct.this, Wishlist.class);
SharedPreferences sharedPreferences = DetailProduct.this.getSharedPreferences("baba", MODE_PRIVATE);
SharedPreferences.Editor shreditor = sharedPreferences.edit();
shreditor.putString("aaa", imgpicaso);
shreditor.putString("name", thisname);
shreditor.putString("abb", text);
shreditor.commit();
startActivity(tambah);
}
});
This is img DetailProduct
This is img Wishlist, please open this img
And this is file Wishlist.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wishlist);
setTitle("Your Wishlist");
ImageView resultgmb = (ImageView) findViewById(R.id.resultgmb);
TextView detailtok = (TextView) findViewById(R.id.detailtok);
TextView resulttext = (TextView) findViewById(R.id.resulttext);
Button bayarwish = (Button) findViewById(R.id.check);
Button hapus = (Button) findViewById(R.id.delete);
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("baba", MODE_PRIVATE);
String imgpicaso = sharedPreferences.getString("aaa", "");
Picasso.with(getBaseContext()).load(imgpicaso).into(resultgmb);
String detailtext = sharedPreferences.getString("name", "");
detailtok.setText(detailtext);
String text = sharedPreferences.getString("ccc", "");
resulttext.setText("$: " +text);
listproduct = (ListView) findViewById(R.id.listproduct);
ArrayList<String> arrayList = new ArrayList<>();
ArrayAdapter<String> arr = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
listproduct.setAdapter(arr);
arr.notifyDataSetChanged();
}
this is xml Wishlist
<SearchView
android:id="#+id/search"
android:background="#color/cardview_light_background"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</SearchView>
<LinearLayout
android:orientation="vertical"
android:id="#+id/relone"
android:layout_below="#id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/rel"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/noimage"
android:id="#+id/resultgmb"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_alignParentRight="true"/>
<TextView
android:layout_marginTop="10dp"
android:text="setext"
android:textStyle="bold"
android:textColor="#000"
android:textSize="17dp"
android:id="#+id/detailtok"
android:layout_width="wrap_content"
android:layout_height="30dp" />
<TextView
android:text="setdescription"
android:layout_below="#id/detailtok"
android:textColor="#f00"
android:id="#+id/resulttext"
android:layout_width="wrap_content"
android:layout_height="30dp" />
<LinearLayout
android:layout_below="#id/resulttext"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CHECK"/>
<Button
android:id="#+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DELETE"/>
</LinearLayout>
<ListView
android:id="#+id/listproduct"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
</LinearLayout>
Please help me, thank you
As #sark9012 pointed out, you might want to use Intent Data rather than shared preferences for that purpose. Here is a tutorial on that.
Edit
Although I'd really advise against using Shared Preferences data for the purpose you're looking for, it seems you got the code correct for retrieving the data from the Shared Preferences, what you might be missing out on is populating the ListView. This is the code you sent us:
listproduct = (ListView) findViewById(R.id.listproduct);
ArrayList<String> arrayList = new ArrayList<>();
ArrayAdapter<String> arr = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
listproduct.setAdapter(arr);
arr.notifyDataSetChanged();
I don't really see you populating the adapter with the data retrieved, so you might want to have a look into that. Check this example from CodePath on how to setup the ListView with an ArrayAdapter.
Generate comma separated string and store that in shared preference
while storing to SP.
like string1 = a1+","a2+","a3;
while retrieving from SP.
like array[] = string1.split(",");
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, array);
Related
I made this mobile app that can add table rows after pressing a button. Each row contains an Edittext, four dropdown menus (spinners), and a button that deletes a row. Each row is like a duplicate after I press the button "add". Now, my only problem is how to save that specific content into firebase's real-time database or could firestore. Do you guys have recommendations of how to do this, any resources, or is this question is broad? I can specify some things you're confused.
I can save an edit text that holds the title for the document, and it successfully works, except the table rows because it holds multiple data types. I just heard that firebase can only hold primitive datatypes.
This is the code that is relevant to the problem, so I won't have to paste all 357 lines of code.
private android.support.v7.widget.Toolbar maintoolbar, editToolBar;
private FirebaseAuth mAuth;
private FloatingActionButton fab;
private RelativeLayout layoutMain;
private RelativeLayout layoutButtons;
private RelativeLayout layoutContent;
private boolean isOpen = false;
private Button addnewRowButton, saveItButton;
private EditText titleofDoc;
private Context context = null;
private ProgressBar pb;
private DatabaseReference dr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
dr = FirebaseDatabase.getInstance().getReference().child("Notes").child(mAuth.getCurrentUser().getUid());
maintoolbar = findViewById(R.id.mainToolBar);
setSupportActionBar(maintoolbar);
getSupportActionBar().setTitle("Files");
layoutMain = findViewById(R.id.mainLays);
layoutButtons = findViewById(R.id.layoutButtons);
layoutContent = findViewById(R.id.layoutContent);
addnewRowButton = findViewById(R.id.AddRow);
saveItButton = findViewById(R.id.SaveThis);
titleofDoc = findViewById(R.id.TitleofDoc);
editToolBar = findViewById(R.id.main_edit_toolbar);
pb = findViewById(R.id.progressBar2);
saveItButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String title = titleofDoc.getText().toString().trim();
if (!TextUtils.isEmpty(title)) {
pb.setVisibility(View.VISIBLE);
createNew(title);
pb.setVisibility(View.INVISIBLE);
} else {
Snackbar.make(v, "Fill Empty Fields", Snackbar.LENGTH_SHORT).show();
pb.setVisibility(View.INVISIBLE);
}
}
});
addnewRowButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addNewRow();
}
});
fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewMenu();
}
});
}
This is the method that makes the table rows.
private void addNewRow() {
final TableLayout tl = findViewById(R.id.tbllays);
String[] teamRoles = {"Director", "Marketing", "Team", "All"};
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, teamRoles);
context = getApplicationContext();
//adds new row
final TableRow tbr = new TableRow(context);
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
tbr.setLayoutParams(layoutParams);
//add edittext to name the task
EditText editText = new EditText(context);
editText.setHint("Add Task");
tbr.addView(editText, 0);
//add spinner to assign teams
Spinner toDo = new Spinner(context);
toDo.setAdapter(adapter);
tbr.addView(toDo, 1);
//add spinner to assign teams
Spinner InProgress = new Spinner(context);
InProgress.setAdapter(adapter);
tbr.addView(InProgress, 2);
//add spinner to assign teams
Spinner Test = new Spinner(context);
Test.setAdapter(adapter);
tbr.addView(Test, 3);
//add spinner to assign teams
Spinner Done = new Spinner(context);
Done.setAdapter(adapter);
tbr.addView(Done, 4);
// Add a button in the second column
Button button = new Button(context);
button.setText("X");
tbr.addView(button, 5);
button.setBackgroundColor(Color.rgb(159, 168, 218));
// Get delete table row button.
Button deleteRowButton = (Button) button;
deleteRowButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tbr.removeAllViews();
}
});
tl.addView(tbr);
}
This is the method that can only save the title instead of the content (the table rows in this case).
private void createNew(String title) {
if (mAuth.getCurrentUser() != null) {
final DatabaseReference newThingRef = dr.push();
final Map thingMap = new HashMap();
thingMap.put("title", title);
thingMap.put("timestamp", ServerValue.TIMESTAMP);
Thread mainThread = new Thread(new Runnable() {
#Override
public void run() {
newThingRef.setValue(thingMap).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Note added", Toast.LENGTH_SHORT).show();
Intent MainIntent = new Intent(MainActivity.this, MainActivity.class);
startActivity(MainIntent);
} else {
String error = task.getException().getMessage();
Toast.makeText(MainActivity.this, "ERROR: " + error, Toast.LENGTH_LONG).show();
}
}
});
}
});
mainThread.start();
} else {...}
}
This is the xml file (the part where the editor is held)
<RelativeLayout
android:id="#+id/layoutContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/mainToolBar"
>
<RelativeLayout
android:id="#+id/layoutButtons"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:gravity="center"
android:visibility="gone">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainEditor">
<LinearLayout
android:id="#+id/LinLay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="370dp">
<android.support.v7.widget.Toolbar
android:id="#+id/main_edit_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" />
<ProgressBar
android:id="#+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:indeterminate="true" />
<EditText
android:id="#+id/TitleofDoc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:ellipsize="end"
android:hint="#string/title"
android:importantForAutofill="no"
android:inputType=""
android:maxHeight="1dp"
android:maxLength="30"
android:maxLines="1"
android:padding="10dp"
tools:targetApi="o" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:alpha=".3"
android:background="#android:color/black" />
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<TableLayout
android:id="#+id/tbllays"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/AddRow"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_weight=".1"
android:background="#color/colorAccent"
android:text="#string/add_row"
android:textColor="#color/colorPrimaryDark" />
<Button
android:id="#+id/SaveThis"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_weight=".1"
android:background="#color/colorPrimaryDark"
android:text="#string/save" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:text="#string/task" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="#string/to_do" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_weight=".1"
android:text="#string/in_progress" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_weight=".1"
android:text="#string/testing" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="4"
android:layout_weight=".1"
android:text="#string/done" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="5"
android:layout_weight=".1"
android:text="#string/delete" />
</TableRow>
</TableLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
</RelativeLayout>
Ignore the user part, that is only if the user is signed in.
This is how it runs
You need to add an Listener at the end of each spinner and store in in the variable for example:
Spinner Done = new Spinner(context);
Done.setAdapter(adapter);
Done.setOnItemSelectedListner(this)
tbr.addView(Done, 4);
Then store this variable Done using DatabaseReference
You don't actually want to save the rows, you want to save the underlying content.
Instead of thinking of the on-screen rows as the content itself, think of them as just displaying your underlying content. This underlying content can be backed up, modified, whatever you want, whilst the representation of it on screen can be completely reconstructed at any time.
To convert your code, I'd suggest the following series of steps:
Create something like a Task class, that has all the fields you want (e.g. name, team).
Store a list of Tasks in your current Activity / Fragment class, e.g. private List<Task> myTasks.
Write a method displayTasks that removes all existing rows from your layout, then iterates through myTasks and draws them all to the screen.
Change addNewRow so that it adds a new Task to myTasks, then calls displayTasks.
You now have a list of Tasks, which can be written directly into Cloud Firestore, and be retrieved later.
I'm fully aware this answer isn't the simple "just do x" you're after, but it's the answer you actually need! All of the steps are very simple, and of course feel free to modify them for your use case.
Once you're comfortable with the concept of "what's on screen isn't my data, just a representation of it", you may want to look into LiveData and ViewModel. They're trickier, but with a lot of benefits (explained within the links).
Searchable Dropdown spinner
hi ,, I have spinner full by sqlite database I want can search in it
how can i do that
I try used Autocomplete text but not worked well
this my spinner.aXml
<Spinner
android:layout_width="match_parent"
android:layout_height="35dp"
android:id="#+id/SpinnerHotel"
android:paddingLeft="10dp"
android:background="#drawable/Spinner_Style"
android:spinnerMode="dropdown"
android:gravity="center"
android:visibility="visible"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp" />
and text_View.axml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:id="#+id/autoCompleteTextView1"
android:textColor="#68767A"
android:text="Select a Hotel .. "
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textSize="18dp" />
and this my Activity
async protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.GoShow_layout);
Agent = FindViewById<Spinner>(Resource.Id.SpinnerAgentt);
List<string> my_array = new List<string>();
my_array = await login.Get_Hotel();
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Resource.Layout.Text_View, my_array);
adapter.SetDropDownViewResource(Resource.Layout.Text_View);
Hotel.Adapter = adapter;
Hotel.ItemSelected += Hotel_ItemSelected;
}
private void Agent_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
var Spinner = sender as Spinner;
string Agentt = Spinner.GetItemAtPosition(e.Position).ToString();
Toast.MakeText(this, Agentt , ToastLength.Short).Show();
}
I think you have to combine Spinner with AutoCompleteTextView, like here Combine Spinner and AutoCompleteTextView
I'm trying to populate sqlite records into a listview.
In my table, I'm splitting an address into columns e.g Street, Postcode, City & State.
I'm using SimpleCursorAdapter to load results into views like in the code below:
Here is my ListActivity.java
private DBManager dbManager;
private ListView listView;
private SimpleCursorAdapter adapter;
DbHelper myDb;
final String[] from = new String[] {
DbHelper.COL_NAME,
DbHelper.COL_STREET,
DbHelper.COL_POSTCODE,
DbHelper.COL_CITY,
DbHelper.COL_STATE };
final int[] to = new int[] { R.id.name, R.id.address };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_emp_list);
dbManager = new DBManager(this);
myDb = new DbHelper(this);
dbManager.open();
Cursor cursor = dbManager.fetch();
listView = (ListView) findViewById(R.id.list_view);
listView.setEmptyView(findViewById(R.id.empty));
adapter = new SimpleCursorAdapter(this, R.layout.activity_view_record, cursor, from, to, 0);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
Here is my view:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/name"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/id"
android:maxLines="1"
android:padding="3dp"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="#+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/nama"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/id"
android:ellipsize="end"
android:maxLines="2"
android:padding="3dp"
android:visibility="visible" />
</RelativeLayout>
My problem is, how to combine STREET, POSTCODE, CITY, STATE and put them into R.id.address?
Using the above method, i have to create a textview for each column, otherwise, it will error.
Make the following changes:
In your layout xml change
<TextView
android:id="#+id/address"
android:text="#string/format_address"
......
/>
In your strings.xm add the following:
<string name="format_address">%1$s, %2$s, %3$s, %4$s</string>
In your source code add the following:
Resources res = getResources();
String text = String.format(res.getString(R.string.format_address), STREET_VALUE, POSTCODE_VALUE, CITY_VALUE, STATE_VALUE);
NOTE STREET_VALUE, POSTCODE_VALUE, CITY_VALUE, STATE_VALUE replace with actual values or String ref.
Here is the layout I am trying to get. As long as I don't actually try to populate my spinners, everything loads just fine. Based on what I have found in my searches and the log is that I need to use a TextView with the ArrayAdapter. Exactly how I do that while preserving the current layout - Spinners and a submit button at the top with a ListView for the returned results below the spinners is what I am having trouble achieving.
Here is my onCreate method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.master);
DBHelper db = new DBHelper(this);
List<Stations> st = db.getAllStations();
List<CharSequence> stations = new ArrayList<CharSequence>();
setList(4, 9);
for (int i = 0; i < st.size(); i++)
{
stations.add(st.get(i).getStation());
}
ListView list = (ListView) findViewById(R.id.list);
Spinner s1 = (Spinner) findViewById(R.id.spinnerStart);
Spinner s2 = (Spinner) findViewById(R.id.spinnerEnd);
ArrayAdapter<CharSequence> SimpleSpinner1 = new ArrayAdapter<CharSequence>(this, R.layout.spinner, R.id.textView1);
ArrayAdapter<CharSequence> SimpleSpinner2 = new ArrayAdapter<CharSequence>(this, R.layout.spinner, R.id.textView1);
SimpleAdapter nSchedule = new SimpleAdapter(this, departures, R.layout.row,
new String[] {"train", "from", "to"}, new int[] {R.id.TRAIN_CELL, R.id.FROM_CELL, R.id.TO_CELL});
for (CharSequence c : stations)
{
SimpleSpinner1.add(c);
SimpleSpinner2.add(c);
}
list.setAdapter(nSchedule);
s2.setAdapter(SimpleSpinner2);
s1.setAdapter(SimpleSpinner1);
}
master.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Spinner
android:id="#+id/spinnerStart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<Spinner
android:id="#+id/spinnerEnd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/spinnerStart" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/spinnerEnd"
android:text="Button" />
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/button1"
layout="#layout/listview" />
</RelativeLayout>
spinner.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Instead of R.id.spinnerStart / R.id.spinnerEnd you need to pass a textView
EDIT:
Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.spinner_item, R.id.textView1);
spinner1.setAdapter(adapter);
textView1 is present in spinner_item layout. Hope this is clear
Try this
// set id for spinner
spinner2 = (Spinner) findViewById(R.id.spinner2);
// prepare a list
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
// set adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// set adapter to spinner
spinner2.setAdapter(dataAdapter);
I am new to android/java and trying to learn by typing code from tutorials. I'm trying to see if I can make a FAQs android module.
Could someone show me how to display only the list of questions and when you click on a question, correspond question and answer display on the next activity?
It's currently displaying both question and answer for the both activities.
Hope my question makes sense. please kindly ask and I will explain as best as I can.
Thank you very much!
faq_list.xml
<item>
<id>1</id>
<question>Whats my phone number?</question>
<answer>my phone number here</answer>
</item>
<item>
<id>2</id>
<question>Whats my fax number?</question>
<answer>my fax number here</answer>
</item>
<item>
<id>3</id>
<question>What is my mailing address?</question>
<answer>my address here</answer>
</item>
faqs_questions.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- qnestion -->
<TextView
android:id="#+id/question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="14sp"
android:layout_marginBottom="10dip"
android:layout_marginTop="10dip"
android:layout_weight="1"
android:paddingLeft="5sp" />
<!-- answer -->
<TextView
android:id="#+id/answer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="14sp"
android:layout_marginBottom="10dip"
android:layout_marginTop="10dip"
android:layout_weight="2"
android:paddingLeft="5sp">
</TextView>
</LinearLayout>
faqs_answers.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="#+id/question_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold"
android:textColor="#000"
android:paddingLeft="5sp"
/>
<TextView android:id="#+id/answer_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textColor="#000"
android:paddingLeft="5sp"
/>
</LinearLayout>
FaqsQuestionsActivity.java
public class FaqsQuestionsActivity extends ListActivity {
static final String URL = "http://myweb.com/faq_list.xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "id";
static final String KEY_QUESTION = "question";
static final String KEY_ANSWER = "answer";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.faqs_questions);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_QUESTION, parser.getValue(e, KEY_QUESTION ));
map.put(KEY_ANSWER, parser.getValue(e, KEY_ANSWER));
menuItems.add(map);
}
ListAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.faqs_question_list,
new String[] { KEY_QUESTION, KEY_ANSWER }, new int[] {R.id.question, R.id.answer});
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String question = ((TextView) view.findViewById(R.id.question)).getText().toString();
String answer = ((TextView) view.findViewById(R.id.answer)).getText().toString();
Intent in = new Intent(getApplicationContext(), FaqsAnswersActivity.class);
in.putExtra(KEY_QUESTION, question);
in.putExtra(KEY_ANSWER, answer);
startActivity(in);
}
});
}
}
FaqsAnswersActivity.java
public class FaqsAnswersActivity extends Activity {
// XML node keys
static final String KEY_QUESTION = "question";
static final String KEY_ANSWER = "answer";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.faqs_answers);
// getting intent data
Intent in = getIntent();
// Get XML values from previous intent
String question = in.getStringExtra(KEY_QUESTION);
String answer = in.getStringExtra(KEY_ANSWER);
// Displaying all values on the screen
TextView lblQuestion = (TextView) findViewById(R.id.question_label);
TextView lblAnswer = (TextView) findViewById(R.id.answer_label);
lblQuestion.setText(question);
lblAnswer.setText(answer);
}
}
It looks to me like the previous answer may give you a problem since you are pulling values from your TextView to put in the Intent for the next Activity.
Instead, I suggest that you change the visibility of your answer TextView in "questions.xml" like this:
<TextView
android:id="#+id/answer"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="14sp"
android:layout_marginBottom="10dip"
android:layout_marginTop="10dip"
android:layout_weight="2"
android:paddingLeft="5sp">
</TextView>
Also, since the view is not visible, you could remove the layout properties that are now irrelevant. I believe all you should need is id and visibility.
<TextView
android:id="#+id/answer"
android:visibility="gone">
</TextView>
The simple list adapter builds the list based on the xml files, just remove the reference to the answer from the listview and it should work
ListAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.faqs_question_list,
new String[] { KEY_QUESTION }, new int[] {R.id.question});
and remove the answer textview from the faq_questions.xml