CarSelection.java:
public class CarSelection extends Activity {
private Spinner spinner1;
// array list for spinner adapter
private ArrayList<Category> brandsList;
ProgressDialog pDialog;
// Url to get all categories
private String URL_CATEGORIES = "http://10.0.2.2/view/get_brands.php";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_car_selection);
Bundle b = getIntent().getExtras();
if(b != null){
String selection1=b.getString("pickup");
String selection2=b.getString("pickdate");
String selection3=b.getString("picktime");
String selection4=b.getString("dropoff");
String selection5=b.getString("dropdate");
String selection6=b.getString("droptime");
TextView text1=(TextView)findViewById(R.id.pickup);
TextView text2=(TextView)findViewById(R.id.pickdate);
TextView text3=(TextView)findViewById(R.id.picktime);
TextView text4=(TextView)findViewById(R.id.dropoff);
TextView text5=(TextView)findViewById(R.id.dropdate);
TextView text6=(TextView)findViewById(R.id.droptime);
text1.setText(selection1);
text2.setText(selection2);
text3.setText(selection3);
text4.setText(selection4);
text5.setText(selection5);
text6.setText(selection6);
}
else{
Toast.makeText(CarSelection.this,"Haven't Received any data yet", Toast.LENGTH_LONG).show();
}
brandsList = new ArrayList<Category>();
// Show the Up button in the action bar.
setupActionBar();
addListenerOnSpinnerItemSelection();
new GetCategories().execute();
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinnerbrand);
spinner1.setOnItemSelectedListener(new CarBrandSelection(this));
}
...
}
CarBrandSelection.java:
public class CarBrandSelection implements OnItemSelectedListener {
private TextView pdestination, pdate, ptime, ddestination, ddate, dtime;
Activity mActivity;
public CarBrandSelection(Activity activity) {
mActivity = activity;
}
public void onClick(View v) {
}
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
switch(pos){
case 1:
pdestination=(TextView) findViewById(R.id.pickup);
pdate=(TextView) findViewById(R.id.pickdate);
ptime=(TextView) findViewById(R.id.picktime);
ddestination=(TextView) findViewById(R.id.dropoff);
ddate=(TextView) findViewById(R.id.dropdate);
dtime=(TextView) findViewById(R.id.droptime);
Bundle b=new Bundle();
b.putString("destination", pdestination.getText().toString());
Intent intent = new Intent(mActivity, FirstListView.class);
intent.putExtras(b);
mActivity.startActivity(intent);
break;
case 2:
Intent intent1 = new Intent(mActivity, SecondListView.class);
mActivity.startActivity(intent1);
break;
case 3:
Intent intent2 = new Intent(mActivity, ThirdListView.class);
mActivity.startActivity(intent2);
break;
case 4:
Intent intent3 = new Intent(mActivity, FourthListView.class);
mActivity.startActivity(intent3);
break;
// and so on
// .....
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
FirstListView.java:
public class FirstListView extends Activity implements FetchDataListener{
private ProgressDialog dialog;
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Bundle b = getIntent().getExtras();
String selection1=b.getString("destination");
TextView text1=(TextView)findViewById(R.id.p_destination);
text1.setText(selection1);
initView();
addListenerOnListItemSelection();
}
main_activity.xml
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="235dp"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"/>
<LinearLayout
android:id="#+id/linealayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/p_destination"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/drop_off" />
</LinearLayout>
activity_car_selection.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".CarSelection" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/vehicle_brand"/>
<TextView android:id="#+id/pickup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<TextView android:id="#+id/pickdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<TextView android:id="#+id/picktime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<TextView android:id="#+id/dropoff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<TextView android:id="#+id/dropdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<TextView android:id="#+id/droptime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>
I really need help to solve this problem. I want to do like when a user clicks on a specific item in the spinner (R.id.spinnerbrand), the data from the textview (R.id.pickup) will be passed to the next activity which is FirstListView.java. I'm not sure whether I do it correctly because the data cannot be passed. Really appreciate if someone can help to solve this. Thanks a lot
If you want to get the data from the spinner you can use:
String selected = parent.getItemAtPosition(pos).toString();
And for passing the data to the other activity basically this is what you need to do:
in the first activity you should create a Intent set the Action and add what you need:
Intent intent = new Intent();
intent.setAction(this, SecondActivity.class);
intent.putExtra(tag, value);
startActivity(intent);
and in the second activity:
Intent intent = getIntent();
intent.getBooleanExtra(tag, defaultValue);
intent.getStringExtra(tag, defaultValue);
intent.getIntegerExtra(tag, defaultValue);
one of the get-functions will give return you the value, depending on the datatype you are passing through.
or in the second activity, for example i want to get the value of name that I include at:
intent.putExtra("name", name); on first class, for that you can simply make:
Bundle b = new Bundle();
b = getIntent().getExtras();
String name = b.getString("name");
Related
My Activity currently holds everything! I would love to have my ListView in another Activity. I have tried cutting and putting in a new Activity, but it can't seem to work because of an error that has to do with the .setText("") on the last line.
Button save;
ArrayList<String> addArray = new ArrayList<String>();
EditText edit;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detailsctivity);
Intent intent = getIntent();
save = (Button) findViewById(R.id.confirm);
edit = (EditText) findViewById(R.id.input);
listView = (ListView) findViewById(R.id.list);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String getInput = edit.getText().toString();
if (addArray.contains(getInput)){
Toast.makeText(getBaseContext(), "Item Added!", Toast.LENGTH_SHORT).show();
}
else if (getInput == null || getInput.trim().equals("")){
Toast.makeText(getBaseContext(), "NO ITEM!", Toast.LENGTH_SHORT).show();
}
else {
addArray.add(getInput);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(DetailsActivity.this,
android.R.layout.simple_list_item_1,
addArray);
listView.setAdapter(adapter);
((EditText) findViewById(R.id.input)).setText("");
}
}
});
}
The Intent intent = getIntent(); comes from a button from my MainActivity.
I guess one step of it is to move the array adapter into the new Activity, which I did as well... but failed again. I do understand the usage of intents as well! Maybe something is missing somewhere?
My layout goes like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_detailsctivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.shoppinglist.pixelite.shoppinglist.DetailsActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/input"
android:hint="#string/newProduct"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/list"
android:layout_below="#id/input"
>
</ListView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/confirm"
android:hint="#string/add"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
I have created an app which has in its first Activity (home) one Button, which goes to next Activity.
In the next Activity I have put some add, delete and list all Buttons, but it's not working.
Please help me out with this
While deleting the data I should have a check box to select the data in another Activity.
I am using Android Studio 2.1.3
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#android:id/list"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_above="#+id/btnAdd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List All"
android:id="#+id/btnGetAll"
android:layout_marginBottom="94dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="#+id/btnAdd"
android:layout_alignTop="#+id/btnGetAll"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
android:id="#+id/btnDel"
android:layout_below="#android:id/list"
android:layout_alignParentEnd="true" />
public class List_View extends ListActivity implements android.view.View.OnClickListener{
Button btnAdd,btnGetAll,btnDel;
TextView student_Id;
#Override
public void onClick(View view)
{
if (view== findViewById(R.id.btnAdd))
{
Intent intent = new Intent(this,StudentDetails.class);
intent.putExtra("student_Id",0);
startActivity(intent);
}
else {
StudentRepo repo = new StudentRepo(this);
ArrayList<HashMap<String, String>> studentList = repo.getStudentList();
if (studentList.size() != 0) {
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
student_Id = (TextView) view.findViewById(R.id.student_Id);
String studentId = student_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(), StudentDetails.class);
objIndent.putExtra("student_Id", Integer.parseInt(studentId));
startActivity(objIndent);
}
});
/* ListAdapter adapter = new SimpleAdapter( MainActivity.this,studentList, R.layout.activity_view_student_entry, new String[] { "id","name"}, new int[] {R.id.student_Id, R.id.student_name});
setListAdapter(adapter);*/
ListAdapter adapter = new SimpleAdapter(List_View.this, studentList, R.layout.activity_view__student__entry, new String[]{"id", "name", "email", "age", "place", "phnumber"}, new int[]{R.id.student_Id, R.id.student_name, R.id.student_email, R.id.student_age, R.id.student_place, R.id.student_phnumber});
setListAdapter(adapter);
} else {
Toast.makeText(this, "No student!", Toast.LENGTH_SHORT).show();
}
}
if (view== findViewById(R.id.btnDel))
{
Intent i = new Intent(this,Delete.class);
startActivity(i);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list__view);
btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(this);
btnGetAll = (Button) findViewById(R.id.btnGetAll);
btnGetAll.setOnClickListener(this);
btnDel = (Button) findViewById(R.id.btnDel);
btnDel.setOnClickListener(this);
}
}
Try this.
android:descendantFocusability="blocksDescendants"
OR
In onItemClick try this.
String item = ((TextView)view).getText().toString();
Here I am trying to set text to a textview in my activity.
The text is set to the text view based on the text of the text view in my activity and the selected item in the custom dialog fragment.
The dialog fragment consists of recycler view and a button.when item is selected from the fragment and text of textview of my activity, and with both cases settext to the textview. I tried different code but I not getting how to achieve it anyone give me solution for it. is there a way to get it.
by using value of text and selected item, adding my math and condition to it. settext to the textview. I am beginner to coding can any one help me please I'm trying from 5 days to achieve it. please help me thanks in advance
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="YOUR DAILY TARGET"
android:layout_gravity="center"
android:id="#+id/textView7"
android:layout_marginTop="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:layout_gravity="center"
android:id="#+id/res"
android:layout_marginTop="20dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Select a glass"
android:layout_marginLeft="10dp"
android:id="#+id/textView6"
android:layout_gravity="center" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="65dp"
android:src="#android:drawable/ic_input_add" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="70dp"
android:layout_height="70dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="0"
android:background="#drawable/circle"
android:gravity="center"
android:id="#+id/glasses"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="31dp"
android:layout_marginEnd="31dp"
android:layout_marginBottom="121dp" />
</RelativeLayout>
</LinearLayout>
MainActivity:
public class MainActivity extends AppCompatActivity {
private Listen selectedListen;
protected void setDataFromMyDialog(Listen listen) {
this.selectedListen = listen;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.getBoolean("isFirstRun", true);
if (isFirstRun) {
//show start activity
startActivity(new Intent(MainActivity.this, Page.class));
Toast.makeText(MainActivity.this, "First Run", Toast.LENGTH_LONG)
.show();
}
getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
.putBoolean("isFirstRun", false).commit();
TextView textView = (TextView)findViewById(R.id.res);
Intent r = getIntent();
double res = r.getDoubleExtra("res", 0);
textView.setText(+res + "L"); // with this text value
double ram = Double.parseDouble(textView.getText().toString());
TextView gls = (TextView) findViewById(R.id.glasses);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DiaFragment df = new DiaFragment();
df.show(MainActivity.this.getSupportFragmentManager(), "Frag");
}
});
}
with the text of textview above and selected item in my dialog fragment by using both the values the set text of TextView glasses.is it possible to get like this
DiaFrag:
public class DiaFragment extends DialogFragment {
ListAdapter listAdapter;
public DiaFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_dialog, container, false);
setCancelable(false);
Button ok = (Button) v.findViewById(R.id.submit);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false);
RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.list);
listAdapter = new ListAdapter(getContext(),getData());
recyclerView.setAdapter(listAdapter);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((MainActivity)getActivity()).setDataFromMyDialog(listAdapter.getSelectedData());
dismiss();
}
});
return v;
}
public static List<Listen> getData()
{
List<Listen> data = new ArrayList<>();
int[] images = {R.drawable.bottle,R.drawable.lotion,R.drawable.soap,R.drawable.soda,R.drawable.sodaa};
String[] texts = {"250ml","300ml","500ml","750ml","1ltr"};
for (int i=0;i<texts.length && i<images.length;i++){
Listen current = new Listen();
current.img = images[i];
current.text= texts[i];
data.add(current);
}
return data;
}
}
Although a developer for more years than I can remember I'm new to Java/Android and the whole OOP thing... Yes we do still exist... shambling relics of a more sequential age...!!
Anyway I have a fragment which invokes a CursorAdapter to populate a ListView.
The ListView has several TextViews and two buttons on each row.
I have been trying to set up listeners on each of the buttons so far without success...
I've searched this forum (and others) and I've used several bits of code suggested by contributors..
From my research my biggest problem seems to be that I'm doing all this from a Fragment.
My question is how do I pass the base ListView id from the Fragment to the Adapter...???
My ListView Child XML...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_marginTop="#dimen/textViewMarginTop"
android:layout_marginRight="#dimen/textViewMarginRight"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="12345678"
android:id="#+id/job_id"
/>
<TextView
android:layout_marginTop="#dimen/textViewMarginTop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="abcdef"
android:id="#+id/from_location"
android:layout_toRightOf="#+id/job_id"
/>
<TextView
android:layout_marginTop="#dimen/textViewMarginTop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Blanchardstown"
android:id="#+id/to_location"
android:layout_toRightOf="#+id/from_location"
/>
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="#+id/job_bid"
android:text="#string/joblist_bid"
android:layout_below="#+id/job_id"
android:layout_toLeftOf="#+id/job_details"
/>
<Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:id="#+id/job_details"
android:text="#string/joblist_details"
android:layout_below="#+id/to_location"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
My ListView Parent XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_marginTop="#dimen/textViewMarginTop"
android:layout_marginRight="#dimen/textViewMarginRight"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="12345678"
android:id="#+id/job_id"
/>
<TextView
android:layout_marginTop="#dimen/textViewMarginTop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="abcdef"
android:id="#+id/from_location"
android:layout_toRightOf="#+id/job_id"
/>
<TextView
android:layout_marginTop="#dimen/textViewMarginTop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Blanchardstown"
android:id="#+id/to_location"
android:layout_toRightOf="#+id/from_location"
/>
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="#+id/job_bid"
android:text="#string/joblist_bid"
android:layout_below="#+id/job_id"
android:layout_toLeftOf="#+id/job_details"
/>
<Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:id="#+id/job_details"
android:text="#string/joblist_details"
android:layout_below="#+id/to_location"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
My Fragment Code
public class jobListFragment extends ListFragment
{
private final String m_LogcatTag = "assignment5";
public ListView m_jobListView;
public JobDataAdpter m_jobDataAdapter;
JobsDataSource m_datasource;
private SQLiteDatabase m_database;
CursorAdapter m_cursoradaptor;
public View m_jobListFragmentBaseView;
Cursor m_cursor;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
m_jobListFragmentBaseView = inflater.inflate(layout.joblistfragment, container, false);
ListView m_jobListView = (ListView)m_jobListFragmentBaseView.findViewById(android.R.id.list);
//Declare an object to handle database I-O and open a channel to the database
m_datasource = new JobsDataSource (getActivity().getApplicationContext());
m_datasource.open();
m_cursor = m_datasource.getAllJobs();
m_jobDataAdapter = new JobDataAdpter(getActivity().getApplicationContext(), m_cursor, 0);
m_jobListView.setAdapter(m_jobDataAdapter);
return m_jobListFragmentBaseView;
}
My Adapter Code
public class JobDataAdpter extends CursorAdapter
{
private final String mLogcatTag = "assignment5";
protected ListView mListView; THIS IS THE VARIABLE IN QUESTION
public JobDataAdpter(Context context, Cursor cursor, int flags )
{
super(context, cursor, 0);
}
protected static class RowViewHolder
{
public TextView m_job_bid;
public TextView m_job_details;
}
//End New Code\
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
View view = View.inflate(context, R.layout.jobslistlayout, null);
RowViewHolder holder = new RowViewHolder();
holder.m_job_bid = (TextView) view.findViewById(R.id.job_bid);
holder.m_job_details = (TextView) view.findViewById(R.id.job_details);
holder.m_job_bid.setOnClickListener(m_OnBidClickListener);
holder.m_job_details.setOnClickListener(m_OnDetailsClickListener);
view.setTag(holder);
return view;
}
#Override
public void bindView(View view, Context context, Cursor cursor)
{
TextView m_job_IdView = (TextView) view.findViewById(R.id.job_id);
TextView m_from_LocationView = (TextView) view.findViewById(R.id.from_location);
TextView m_to_LocationView = (TextView) view.findViewById(R.id.to_location);
// Extract properties from cursor
long row_id = cursor.getLong((cursor.getColumnIndexOrThrow(MySQLiteOpenHelper.m_COLUMN_ID)));
String m_job_id = cursor.getString(cursor.getColumnIndexOrThrow(MySQLiteOpenHelper.m_COLUMN_JOB_ID));
String m_From_Location = cursor.getString(cursor.getColumnIndexOrThrow(MySQLiteOpenHelper.m_COLUMN_JOB_FROM_LOCATION));
String m_To_Location = cursor.getString(cursor.getColumnIndexOrThrow(MySQLiteOpenHelper.m_COLUMN_JOB_TO_LOCATION));
// Populate fields with extracted properties
m_job_IdView.setText(m_job_id);
m_from_LocationView.setText(String.valueOf(m_From_Location));
m_to_LocationView.setText(String.valueOf(m_To_Location));
}
private View.OnClickListener m_OnBidClickListener = new View.OnClickListener()
{
#Override
public void onClick(View v)
{ HERE IS WHERE THE VARIABLE IS USED
final int position = mListView.getPositionForView((View) v.getParent());
Log.v(mLogcatTag, "Title clicked, row %d" + position);
}
};
private View.OnClickListener m_OnDetailsClickListener = new View.OnClickListener()
{
#Override
public void onClick(View v)
{
final int position = mListView.getPositionForView((View) v.getParent());
Log.v(mLogcatTag, "Text clicked, row %d" + position);
}
};
}
OK guys .....That's it.... Hope you can help
I have two Activity, A and B. In A, it has a listView and a button. In B, it has imageView and editText . Now what I trying to achieve is return the text from B to listView A.
Activity A
View claims = inflater.inflate(R.layout.receipt_text, container, false);
listV = (ListView) claims.findViewById(R.id.listView);
String [] status ={"Type of Claims :" +name,"Amount :" +result};
adapter=new ArrayAdapter<String>(this,R.layout.claims,status);
listV.setAdapter(adapter);
return claims;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive text from B
switch (requestCode) {
case 0:
result = data.getStringExtra("text");
name = data.getStringExtra("a");
description = data.getStringExtra("c");
break;
}
Activity B
ImageView viewImage;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.project);
txt = (EditText) findViewById(R.id.editText36);
txt1=(TextView)findViewById(R.id.textView57);
Button b = (Button) findViewById(R.id.button17);
addListenerOnButton();
b.setOnClickListener(new View.OnClickListener() { // return to A
public void onClick(View arg0) {
Intent returnIntent = new Intent();
a = "Project";
text = txt.getText().toString(); // amount
returnIntent.putExtra("text", text);
returnIntent.putExtra("a", a);
returnIntent.putExtra("c", c);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
public void addListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Global.img=null;
Intent i = new Intent(Project1.this, C.class);
startActivityForResult(i, PROJECT_REQUEST_CODE);
}
});
}
public void onActivityResult(int requestCode,int resultCode, Intent data)
{ // receive from C
if(requestCode==PROJECT_REQUEST_CODE) {
if(data!=null&&data.hasExtra("text")) {
c = data.getStringExtra("text");
txt1.setText(c);
viewImage.setImageBitmap(Global.img);
}
}
else if (requestCode==CAMERA_REQUEST_CODE)
{
}
}
}
receipt_text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:padding="10dp"
android:text="#string/text" android:textSize="20sp" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
<ListView android:id="#+id/listView1" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
claims.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
</android.support.v4.widget.DrawerLayout>
Error
11-16 10:57:24.696 30313-30313/com.example.project.project
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.project.project, PID: 30313
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
The constructor for ArrayAdapter is wrong.
adapter=new ArrayAdapter<String>(this,R.layout.claims,c);
Instead of passing an int into the ArrayAdapter constructor, creating a List and pass it in. Later add the data you get from Activity B to the list.
List<String> list = new ArrayList<>();
adapter=new ArrayAdapter<String>(this, R.layout.claims, list);
When you get the data back from ActivityB, do this
String data = Integer.toString(c);
list.add(data);
adapter.notifyDataSetChanged();