An app to add , delete , and view data - java

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

Related

Android java code to add + and - button beside edittext

for(int i=0;i<j.size();i++)
{
TableLayout.LayoutParams tableRowParams=
new TableLayout.LayoutParams
(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
JsonObject jb = (JsonObject) j.get(i);
String item = jb.get("item").getAsString();
String unit = jb.get("unit").getAsString();;
String price = jb.get("price").getAsString();;
TableRow tbrow1 = new TableRow(this);
TextView tv01 = new TextView(this);
tv01.setText(item);
tv01.setTextColor(Color.BLACK);
tbrow1.addView(tv01);
TextView tv11 = new TextView(this);
tv11.setText(unit);
tv11.setTextColor(Color.BLACK);
tbrow1.addView(tv11);
TextView tv21 = new TextView(this);
tv21.setText(price);
tv21.setTextColor(Color.BLACK);
tbrow1.addView(tv21);
Button button = new Button(this);
button.setText("+");
button.setTag(item);
tbrow1.addView(button);
final String id_ = (String) button.getTag();
EditText edText = new EditText(this);
edText.setInputType(InputType.TYPE_CLASS_NUMBER);
//edText.setText(0);
tbrow1.addView(edText);
Button button1 = new Button(this);
button1.setText("-");
button.setTag(item);
tbrow1.addView(button1);
final String id1_ = (String) button.getTag();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "button clicked for "+id_, Toast.LENGTH_LONG).show();
}
});
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "button clicked for "+id1_, Toast.LENGTH_LONG).show();
}
})
}
I'm developing Android app for grocery shop . I'm getting itms details in json
Depending on items number I'm creating dynamic rows.
Now I want to add + and - button in each row .
I want it programmatically
How to to do it.
Example: + - button in zomato while adding food to cart .
As i click + or - button that corresponding item number should increment/decrement
You can use this library. It is a simple Android library to implement a number counter with increment and decrement buttons.
https://github.com/ashik94vc/ElegantNumberButton
try this,
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView textViewCount;
int count=1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewCount=findViewById(R.id.textViewCount);
textViewCount.setText(""+count);
}
public void itemSubtractClick(View view) {
if (count>0)
textViewCount.setText(""+count--);
else
Toast.makeText(this, "not allowed", Toast.LENGTH_SHORT).show();
}
public void itemAddClick(View view) {
textViewCount.setText(""+count++);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/shape"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:onClick="itemSubtractClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_subtract" />
<TextView
android:id="#+id/textViewCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#17F44336"
android:padding="10dp"
android:text="2"
android:textSize="18sp" />
<ImageView
android:onClick="itemAddClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_add" />
</LinearLayout>

adding space to make each list item bigger

My app that I am creating has two listviews. My second listview does not look the same as my second one. The lines that seperate each item are too close to the text, but I want them to have space on both the top and bottom, like the first list. I have tried to change the padding and the margins but that does not works and I have also tried to change it in one of the Java classes but I get error messages.
Here are two pictures for comparison(The first list is on the left and the second list is on the right)
The second list:
<ListView
android:id="#+id/countryselector"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_above="#+id/checkItem"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="60dp"
android:clickable="false"/>
<TextView
android:id="#+id/checkboxtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/emptyElement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="NO ITEM AVAILABLE!"
android:textColor="#525252"
android:textSize="19.0sp"
android:visibility="gone" />
<Button
android:id="#+id/checklist"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentTop="true"
android:gravity="end"
android:layout_alignParentLeft="true"
android:text=""
android:background="#mipmap/arrow2"
My first list:
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="left"
android:text="Favorites"
android:textColor="#222"
android:textSize="15dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="2dp"
android:textStyle="normal" />
<Button
android:id="#+id/tofavorites"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentTop="true"
android:gravity="end"
android:layout_alignParentLeft="true"
android:text=""
android:background="#mipmap/arrow2" />
<ListView
android:layout_marginTop="60dp"
android:id="#+id/countrieselector"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:clickable="false" />
<TextView
android:id="#+id/checkboxtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Here Is my adapter class for the second list:
public class AdapterFavorite extends BaseAdapter {
ArrayList<String> items;
ArrayList<String> items1;
LayoutInflater inflater;
ArrayList selectedItems;
List<Item> dummyItems;
ArrayList<Integer> itemsValue = new ArrayList<>();
Activity activity;
DbUtility dbUtility;
Context context;
AdapterFavorite(Activity activity, ArrayList<String> items, Context context) {
this.items = items;
inflater = activity.getLayoutInflater();
selectedItems = new ArrayList<String>();
this.activity = activity;
this.context = context;
dbUtility = new DbUtility(activity);
}
#Override
public int getCount() {
return items.size();
}
#Override
public String getItem(int i) {
return items.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(final int i, View view, ViewGroup viewGroup) {
dbUtility.open();
View v = view;
if (v == null) {
v = inflater.inflate(R.layout.checkboxlayoutfavorite, null);
}
TextView textview_countries = (TextView) v.findViewById(R.id.Textview_languages);
Button button = (Button) v.findViewById(R.id.checkbox);
items1 = new ArrayList<>();
textview_countries.setText(items.get(i));
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
////Where I tried to add/move items when the checkbox is clicked
dbUtility.delete_byID(items.get(i));
items.remove(i);
notifyDataSetChanged();
}
});
return v;
}
}
Here is my main Java class for the second listL:
ublic class FavoriteActivity extends AppCompatActivity {
DbUtility dbUtility;
List<Item> dummyItems = new ArrayList<>();
Button mainList;
KeyboardPrefManager keyboardPrefManager;
ArrayList<String> items;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ListView chl = (ListView) findViewById(R.id.languageselector);
mainList = (Button) findViewById(R.id.checklist);
chl.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
items = new ArrayList<>();
dbUtility = new DbUtility(this);
dbUtility.open();
dummyItems = dbUtility.getAllContacts();
keyboardPrefManager = new KeyboardPrefManager(this);
for (int a = 0; a < dummyItems.size(); a++) {
items.add(dummyItems.get(a).getValue());
}
mainList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(FavoriteActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
AdapterFavorite adapter = new AdapterFavorite(this, items, this);
chl.setAdapter(adapter);
chl.setEmptyView(findViewById(R.id.emptyElement));
chl.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String item = items.get(i); // Getting the dummy item from the List with position i
Toast.makeText(FavoriteActivity.this, "Clicked on " + item, Toast.LENGTH_SHORT).show();
keyboardPrefManager.selectKeyboard(item);
}
});}}
Please check your list item xml for both list same or not.Thanks

onClick not working in Fragment

Basically I have listview in my fragment layout. And another layout called item_todo.xml which contains my button.
The onClick() wasn't working while clicking the delete_task button in my layout.
It would be great if you could help me out !
Here is my Class ToDoFragment
public class ToDoFragment extends Fragment {
private static final String TAG = "MainActivity";
private TaskDbHelper mHelper;
private ListView mTaskListView;
private ArrayAdapter<String> mAdapter;
Button myButton;
FloatingActionButton btnadd;
//Overriden method onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two, container, false);
mHelper = new TaskDbHelper(getActivity());
mTaskListView = (ListView) view.findViewById(R.id.list_todo);
//Floating action button
btnadd = (FloatingActionButton) view.findViewById(R.id.btnadd);
btnadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final EditText taskEditText = new EditText(getActivity());
AlertDialog dialog = new AlertDialog.Builder(getActivity())
.setTitle("Add a new task")
.setMessage("What do you want to do next?")
.setView(taskEditText)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String task = String.valueOf(taskEditText.getText());
SQLiteDatabase db = mHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(TaskContract.TaskEntry.COL_TASK_TITLE, task);
db.insertWithOnConflict(TaskContract.TaskEntry.TABLE,
null,
values,
SQLiteDatabase.CONFLICT_REPLACE);
db.close();
updateUI();
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();
}
});
updateUI();
return view;
}
#Override
public void onActivityCreated(Bundle saved){
super.onActivityCreated(saved);
final LayoutInflater factory = getActivity().getLayoutInflater();
final View textEntryView = factory.inflate(R.layout.item_todo, null);
myButton = (Button) textEntryView.findViewById(R.id.task_delete);
myButton.setOnClickListener(new View.OnClickListener() {
// OnClick() Not Working !!
#Override
public void onClick(View view) {
deleteTask(view);
}
});
}
public void deleteTask(View view) {
View parent = (View) view.getParent();
TextView taskTextView = (TextView) parent.findViewById(R.id.task_title);
String task = String.valueOf(taskTextView.getText());
SQLiteDatabase db = mHelper.getWritableDatabase();
db.delete(TaskContract.TaskEntry.TABLE,
TaskContract.TaskEntry.COL_TASK_TITLE + " = ?",
new String[]{task});
db.close();
updateUI();
}
private void updateUI() {
ArrayList<String> taskList = new ArrayList<>();
SQLiteDatabase db = mHelper.getReadableDatabase();
Cursor cursor = db.query(TaskContract.TaskEntry.TABLE,
new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TITLE},
null, null, null, null, null);
while (cursor.moveToNext()) {
int idx = cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_TITLE);
taskList.add(cursor.getString(idx));
}
if (mAdapter == null) {
mAdapter = new ArrayAdapter<>(getActivity(),
R.layout.item_todo,
R.id.task_title,
taskList);
mTaskListView.setAdapter(mAdapter);
} else {
mAdapter.clear();
mAdapter.addAll(taskList);
mAdapter.notifyDataSetChanged();
}
cursor.close();
db.close();
}}
Fragment_two XML
<ListView
android:id="#+id/list_todo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:orientation="horizontal" >
<android.support.design.widget.FloatingActionButton
android:layout_width="50dp"
android:id="#+id/btnadd"
android:layout_height="50dp"
app:fabSize="normal"
android:clickable="true"
android:src="#drawable/red"
android:scaleType="center"
/>
</LinearLayout>
</RelativeLayout>
item_todo XML
<?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="match_parent"
android:layout_gravity="center_vertical">
<TextView
android:id="#+id/task_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="Hello"
android:textSize="20sp" />
<Button
android:id="#+id/task_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="Done" />
</RelativeLayout>
You inflate a new View in your onActivityCreated But you never used that view !!! myButton refer to a button which its view has not been used !
Edit :
There are lots of ways you can reach your goal:
Interfaces
You can initialize an interface and pass it to fragment, so whenever something happen in your activity (button click for example), you will understand
BroadcastReceivers
You can register local broadcast receivers in your fragment and when user click on the button, you send that broadcast, so fragment will understand
i suggest the second one and using EVENTBUS library

Listview and button hidden

Well, I try to explain.
I made an application where I have a listview where each row have also two buttons (I have made it with other question posted in this site).
The problem is this:
The two buttons is "start" and "stop". When I click start, a service starts and when I click on stop this service has to stop (I haven't implemented the service for now).
So, when I click start, I would like to hide the start button, in this way I know that the service is started.
How can I do it? Besides, it can be the right choice implements the service in this way?
I have choosen this idea because I need to stop the service when I decide to stop it.
Code:
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//generate list
ArrayList<String> list = new ArrayList<String>();
list.add("item1");
list.add("item2");
//instantiate custom adapter
android.widget.ListAdapter adapter = new ListAdapter(this,0,list);
//handle listview and assign adapter
ListView lView = (ListView)findViewById(R.id.list_item);
lView.setAdapter(adapter);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
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.example.sara.myapplication.MainActivity">
<ListView
android:id="#+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
row_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Text"
android:id="#+id/list_item_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5pt"
android:layout_marginTop="2pt"
android:textSize="10pt"
android:layout_weight="0.49"/>
<Button
android:text="Start"
android:id="#+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="Stop"
android:id="#+id/stop_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/start_button"/>
</LinearLayout>
ListAdapter.java
public class ListAdapter extends ArrayAdapter<String> {
private ArrayList<String> list;
private int layout;
private Context context;
public ListAdapter(Context context, int resource, ArrayList<String> objects) {
super(context, resource, objects);
context = context;
layout = resource;
list = objects;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
view = inflater.inflate(R.layout.row_item, null);
}
//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
Button deleteBtn = (Button)view.findViewById(R.id.stop_button);
Button addBtn = (Button)view.findViewById(R.id.start_button);
deleteBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "delete - Button was clicked for list item " + position, Toast.LENGTH_SHORT).show();
//do something
notifyDataSetChanged();
}
});
addBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "start - Button was clicked for list item " + position, Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
}
});
return view;
}
}
you can use some boolean flag value to change the visibility of button on click.
addBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "start - Button was clicked for list item " + position, Toast.LENGTH_SHORT).show();
//do something
addBtn.setVisibility(View.GONE);
notifyDataSetChanged();
}
});
deleteBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "delete - Button was clicked for list item " + position, Toast.LENGTH_SHORT).show();
//do something
addBtn.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}
});
In this way seems working
If on your list you have more than one element, then you have to implement a custom adapter and create an ad hoc listner for each element. Use setTag and getTag.
CHange row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Text"
android:id="#+id/list_item_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5pt"
android:layout_marginTop="2pt"
android:textSize="10pt"
android:layout_weight="0.49"/>
<Button
android:text="Start"
android:id="#+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="start"/>
<Button
android:text="Stop"
android:id="#+id/stop_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/start_button"
android:onClick="stop"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/start_button" />
</LinearLayout>
And in MainAcitivty
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//generate list
ArrayList<String> list = new ArrayList<String>();
list.add("item1");
list.add("item2");
//instantiate custom adapter
ListAdapter adapter = new ListAdapter(this, 0, list);
//handle listview and assign adapter
ListView lView = (ListView)findViewById(R.id.list_item);
lView.setAdapter(adapter);
}
public void start(View v){
Button b = (Button) v;
v.setVisibility(View.INVISIBLE);
}
public void stop(View v){
}
}
ListAdapter
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
view = inflater.inflate(R.layout.row_item, null);
}
//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
Button deleteBtn = (Button)view.findViewById(R.id.stop_button);
Button addBtn = (Button)view.findViewById(R.id.start_button);
return view;
}
You need to use the setVisible(false) method
Example:
addBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "start - Button was clicked for list item " + position, Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
addBtn.setVisible(false); // ← Hide the "Start button"
}
});
To Hide start Button after clicking,
addBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "start - Button was clicked for list item " + position, Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
addBtn.setVisibility(View.GONE);
}
});

how to pass data from textview to listview items selection

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");

Categories