Button does not work inside a listview - android

This is the main Activity, Where a listview has some data and a button
it is show data,but whenever i clicked the button it does not work.
final String [] from = new String[]{AllDB.COLUMN_NAME, AllDB.COLUMN_AGE, AllDB.COLUMN_HEIGHT , AllDB.COLUMN_WEIGHT};
final int [] to = new int[]{R.id.nameTV,R.id.ageTV,R.id.heightTV,R.id.weightTV};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
dBmanegar = new AllDBmanegar(this);
dBmanegar.open();
final Cursor cursor = dBmanegar.fetch();
listView = (ListView) findViewById(R.id.list_view);
listView.setEmptyView(findViewById(R.id.emptyTV));
adapter = new SimpleCursorAdapter(this, R.layout.show_person_info, cursor, from, to,0);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//TextView idTextView = (TextView)view.findViewById(R.id.idTV);
TextView nameTextView = (TextView) view.findViewById(R.id.nameTV);
TextView ageTextView = (TextView) view.findViewById(R.id.ageTV);
TextView heightTextView = (TextView) view.findViewById(R.id.heightTV);
TextView weightTextView = (TextView) view.findViewById(R.id.weightTV);
Button diet = (Button) view.findViewById(R.id.diet);
diet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent diet_P = new Intent(getApplicationContext(), Diet.class);
startActivity(diet_P);
}
});
//String iD = idTextView.getText().toString();
String name = nameTextView.getText().toString();
String age = ageTextView.getText().toString();
String height = heightTextView.getText().toString();
String weight = weightTextView.getText().toString();
Cursor cursor1 = (Cursor) parent.getItemAtPosition(position);
cursor1.moveToFirst();
//iD = cursor1.getString(cursor1.getColumnIndex(MyDB.COLUMN_ID));
Intent intent = new Intent(getApplicationContext(), Modify_person_info.class);
//intent.putExtra("_id",iD);
intent.putExtra("name", name);
intent.putExtra("age", age);
intent.putExtra("height", height);
intent.putExtra("weight", weight);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.add_person,menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.add_person){
Intent intent = new Intent(this,Add_new.class);
startActivity(intent);
}else if (id == R.id.laout){
Intent backToHome = new Intent(this,MainActivity.class);
startActivity(backToHome);
finish();
}
return super.onOptionsItemSelected(item);
}
And this is the XML of SimpleCursorAdapter where it is display the data and button
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="3dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Name"
android:background="#f49c71"
android:id="#+id/nameTV"
android:layout_below="#+id/ageTV"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:textAppearance="?android:attr/textAppearanceSmall"
android:background="#bbbbff"
android:id="#+id/ageTV"
android:text="Age"
android:layout_below="#+id/heightTV"
android:layout_alignLeft="#+id/heightTV"
android:layout_alignStart="#+id/heightTV" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="3dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Height"
android:background="#f49c71"
android:id="#+id/heightTV"
android:layout_below="#+id/ageTV"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Weight"
android:background="#bbbbff"
android:id="#+id/weightTV"
android:layout_below="#+id/heightTV"
android:layout_alignLeft="#+id/heightTV"
android:layout_alignStart="#+id/heightTV" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/diet"
android:layout_weight=".5"
android:text="Add Diet"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/setText"/>
</LinearLayout>

Try to add this following attribute to Button from XML:
android:focusable="false"
If it didn't work, create a custom adapter for listview:
Custom Adapter for List View

Related

Multiple Intents passing listView info from one activity to another

I'm trying to pass information from my second activity to my first activity. In my second Activity contains a list view of images of provinces in Canada. Each item in the list contains 3 properties (Province Name, Capital of the Province, Image id for the actual flag of the province). When the user selects an item from the listView it should direct them to the first Activity and pass the intent information from the second Activity to the first Activity. The information that will be passed are the name of the province and the capital of the province. I feel like I'm doing it correctly and my app currently has no issue in terms of bugs or crashes but I can't seem to figure out why the information is not being passed from activity 2 to activity 1. It just passes nothing. Thanks to those all in advance that help me out!
Main Activity.Java
TextView provinceTxt, capitalTxt;
String provinceString, capitalString;
ArrayList<Province> provinces;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
provinceTxt = findViewById(R.id.txtProvince);
capitalTxt = findViewById(R.id.txtCapital);
display();
}
public void selectProvince(View view) {
showAlertDialog();
}
public void showAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Would You Like to Open the Second Activity?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(MainActivity.this, Second_Activity.class);
startActivity(intent);
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
public void display(){
Intent intent = getIntent();
provinceString = intent.getStringExtra("name");
provinceTxt.setText(provinceString);
capitalString = intent.getStringExtra("capital");
capitalTxt.setText(capitalString);
}
}
Second Activity.Java
List<Province> provinces;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_);
provinces = ProvincesData.getList();
listView = findViewById(R.id.listView);
CustomAdapter adapter = new CustomAdapter(Second_Activity.this, R.layout.list_view_row, provinces);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Province province = (Province) listView.getItemAtPosition(position);
Intent intent = new Intent();
intent.putExtra("name", province.getName());
intent.putExtra("capital", province.getCapital());
finish();
}
});
}
}
Province.Java
public class Province {
String name;
String capital;
public Province(String name, String capital, int armID) {
this.name = name;
this.capital = capital;
this.armID = armID;
}
int armID;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCapital() {
return capital;
}
public void setCapital(String capital) {
this.capital = capital;
}
public int getArmID() {
return armID;
}
public void setArmID(int armID) {
this.armID = armID;
}
}
ProvincesData.Java
public static List<Province> getList() {
List<Province> provinces = new ArrayList<Province>();
Province province1 = new Province("Alberta","Edmonton", R.drawable.alberta);
provinces.add(province1);
Province province2 = new Province("British Columbia","Victoria", R.drawable.british_columbia);
provinces.add(province2);
Province province3 = new Province("Manitoba","Winnipeg", R.drawable.manitoba);
provinces.add(province3);
Province province4 = new Province("New Brunswick","Fredericton", R.drawable.new_brunswick);
provinces.add(province4);
Province province5 = new Province("Newfoundland and Labrador","St. John's", R.drawable.newfoundland_and_labrador);
provinces.add(province5);
Province province6 = new Province("Nova Scotia","Halifax", R.drawable.nova_scotia);
provinces.add(province6);
Province province7 = new Province("Ontario","Toronto", R.drawable.ontario);
provinces.add(province7);
Province province8 = new Province("Quebec","Quebec City", R.drawable.quebec);
provinces.add(province8);
Province province9 = new Province("Saskatchewan","Regina", R.drawable.saskatchewan);
provinces.add(province9);
Province province10 = new Province("Prince Edward ","Charlottetown", R.drawable.prince_edward_island);
provinces.add(province10);
return provinces;
}
}
CustomAdapter.Java
public class CustomAdapter extends ArrayAdapter {
static class ViewHolder{
TextView provinceTv;
TextView cityTv;
ImageView armID;
}
List<Province> provinces;
public CustomAdapter(#NonNull Context context, int resource, #NonNull List objects) {
super(context, resource, objects);
provinces = objects;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
ViewHolder holder;
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_view_row , null);
holder = new ViewHolder();
holder.provinceTv = view.findViewById(R.id.txtProvinceRow);
holder.cityTv = view.findViewById(R.id.txtCapitalRow);
holder.armID = view.findViewById(R.id.imageView);
view.setTag(holder);
}
holder = (ViewHolder)view.getTag();
holder.provinceTv.setText(provinces.get(position).getName());
holder.cityTv.setText(provinces.get(position).getCapital());
holder.armID.setBackgroundResource(provinces.get(position).getArmID());
return view;
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:text="Selected Province Info"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="25dp"
android:orientation="horizontal">
<TextView
android:id="#+id/txtProvinceTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Province Name"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<TextView
android:id="#+id/txtProvince"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="text"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="25dp"
android:orientation="horizontal">
<TextView
android:id="#+id/txtCapitalTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Capital City"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textStyle="bold"/>
<TextView
android:id="#+id/txtCapital"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="text"
android:textAppearance="#style/TextAppearance.AppCompat.Large" />
</LinearLayout>
<Button
android:id="#+id/btnSelect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="30dp"
android:text="Select Province"
android:onClick="selectProvince"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textStyle="bold"/>
</LinearLayout>
Activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Second_Activity">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
list_view_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
tools:srcCompat="#tools:sample/avatars" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_weight="2"
android:layout_marginHorizontal="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/txtProvinceRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textStyle="bold"
android:text="" />
<TextView
android:id="#+id/txtCapitalRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:text="" />
</LinearLayout>
</LinearLayout>
You forget to start the intent
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Province province = (Province) listView.getItemAtPosition(position);
Intent intent = new Intent(this.Second_Activity.class, MainActivity);
intent.putExtra("name", province.getName());
intent.putExtra("capital", province.getCapital());
startActivity(intent);
finish();
}
});
receive the intent
public void display(){
Bundle extras = intent.getExtras();
if(extras != null){
String provinceString = extras.getString("name");
provinceTxt.setText(provinceString);
String capitalString = extras.getString("capital");
capitalTxt.setText(capitalString);
}
}
for more details refer this
you can use Activity.startActivityForResult(Intent intent, int requestCode)to start Second Activity and use Activity.onActivityResult(int requestCode, int resultCode, Intent data) to receive data that from Second Activityin MainActivity

How to send List View values to the another activity in android

i am creating a simple crud system in android.i can view all data in to Listview. if i select a item from the list view it going to another activity for edit. but pass data to the Another activity successfully. but how to assign to textfields. i can pass only one textfield. how to pass values in to the relavent textfields. i attached the screen shot and codes below
Screen Shots
view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".view">
<ListView
android:id="#+id/lst1"
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</LinearLayout>
View.java
public class view extends AppCompatActivity {
ListView lst1;
ArrayList<String> titles = new ArrayList<String>();
ArrayAdapter arrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
SQLiteDatabase db = openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
lst1 = findViewById(R.id.lst1);
Cursor c = db.rawQuery("select * from record",null);
int id = c.getColumnIndex("id");
int name = c.getColumnIndex("name");
int age = c.getColumnIndex("age");
c.moveToFirst();
titles.clear();
arrayAdapter = new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,titles);
lst1.setAdapter(arrayAdapter);
if(c.moveToFirst()) {
do {
titles.add(c.getString(id) + " \t " + c.getString(name) + " \t " + c.getString(age));
} while (c.moveToNext());
arrayAdapter.notifyDataSetChanged();
lst1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String tt = titles.get(position).toString();
Intent i = new Intent(getApplicationContext(),edit.class);
i.putExtra("age",tt);
startActivity(i);
}
});
}
Edit.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity = "center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Edit System"
android:textColor="#color/colorAccent"
android:textSize="40dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ID" />
<EditText
android:id="#+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name" />
<EditText
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Age" />
<EditText
android:id="#+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Edit" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Delete" />
</LinearLayout>
Edit.java
public class edit extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
EditText ed1 = findViewById(R.id.age);
Intent i = getIntent();
String ttt = i.getStringExtra("age").toString();
ed1.setText(ttt);
}
}
Create one pojo class like
class student{
String id,
String name,
String age,
String titles
}
now replace below code
if(c.moveToFirst()) {
do {
titles.add(c.getString(id) + " \t " + c.getString(name) + " \t " + c.getString(age));
} while (c.moveToNext());
arrayAdapter.notifyDataSetChanged();
lst1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String tt = titles.get(position).toString();
Intent i = new Intent(getApplicationContext(),edit.class);
i.putExtra("age",tt);
startActivity(i);
}
});
with
ArrayList<student> stud = new ArrayList<student>();
if(c.moveToFirst()) {
do {
student stu = new student()
stu.id = c.getString(id);
stu.name = c.getString(name);
stu.age = c.getString(age);
//you need to add the Student object stu not the ArrayList Object stud
stud.add(stu);
titles.add(c.getString(id) + " \t " + c.getString(name) + " \t " + c.getString(age));
} while (c.moveToNext());
arrayAdapter.notifyDataSetChanged();
lst1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String tt = titles.get(position).toString();
student stu = stud.get(position);
Intent i = new Intent(getApplicationContext(),edit.class);
i.putExtra("age",stu.age);
startActivity(i);
}
});
You need to create a class for your custom items:
class Student{
String id;
String name;
String age;
String title;
public Student(String id, String name, String age)
{
this.id = id;
this.name = name;
this.age = age;
title = id + " \t" +name+" \t"+age;
}
}
//View.java:
if(c.moveToFirst()) {
do {
Student tmpStudent = new Student(c.getString(id), c.getString(name), c.getString(age));
titles.add(tmpStudent.title);
} while (c.moveToNext());
arrayAdapter.notifyDataSetChanged();
lst1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String studentString[] = titles.get(position).toString().Split(" \t");
Student tmpStringStudent = new Student(studentString[0], studentString[1], studentString[2]);
Intent i = new Intent(getApplicationContext(),edit.class);
i.putExtra("id",tmpStringStudent.id);
i.putExtra("name",tmpStringStudent.name);
i.putExtra("age",tmpStringStudent.age);
startActivity(i);
}
});
}
//Edit.java:
public class edit extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
EditText ageEditText = findViewById(R.id.age);
EditText idEditText = findViewById(R.id.id);
EditText nameEditText = findViewById(R.id.name);
Intent i = getIntent();
String id = i.getStringExtra("id").toString();
String name = i.getStringExtra("name").toString();
String age = i.getStringExtra("age").toString();
idEditText.setText(id);
nameEditText.setText(name);
ageEditText.setText(age);
}
}
I hope this helps.

Display corresponding details of user when selected from list

I have developed a screen where a no. of people from the database are displayed in a list view. I want to display the profile page of the selected person. So my question is how to bind each detail of the selected person like name, contact, etc. to the profile page which I have created? Will I have to call the getById API in the onItemClickListener?
Here's the edited code:-
public class Test extends AppCompatActivity {
List<Genie> genieList;
GenieAdapter genieAdapter;
TextView responseView;
ProgressBar progressBar;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
responseView = (TextView) findViewById(R.id.responseView);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
button = (Button) findViewById(R.id.test);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Test.this, "Blahblah", Toast.LENGTH_LONG).show();
new RetrieveFeedTask().execute();
}
});
}
class RetrieveFeedTask extends AsyncTask<Void, Void, List<Genie>> {
private Exception exception;
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
responseView.setText("");
}
protected List<Genie> doInBackground(Void... urls) {
GenieService genieService = new GenieService();
return genieService.getAll();
}
protected void onPostExecute(List<Genie> genies) {
if (genies == null) {
new ArrayList<Genie>(); // "THERE WAS AN ERROR"
} else {
progressBar.setVisibility(View.GONE);
Log.i("INFO", genies.get(0).name);
List<String> rows = genies.stream().map(genie -> getRow(genie)).collect(Collectors.toList());
genieAdapter=new GenieAdapter(getApplicationContext(),R.layout.genie_list, genies);
ListView list=(ListView)findViewById(R.id.listViewMain);
list.setAdapter(genieAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(Test.this, "" + position, Toast.LENGTH_SHORT).show();
// if (position == 1) {
// startActivity(new Intent(Test.this, viewGenie1.class));
// }
}
});
list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Test.this, viewGenie1.class);
intent.putExtra("name", "%s");
intent.putExtra("add", "%s");
intent.putExtra("phn", "%s");
intent.putExtra("sal", "%s");
intent.putExtra("lea", "%s");
startActivity(intent);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
private String getRow(Genie g) {
return String.format("%s, %s, %s, %s, %s", g.name, g.salary, g.contact, g.paid_leaves, g.address);
}
}
}
Here's the viewGenie1.class:-
public class viewGenie1 extends AppCompatActivity implements View.OnClickListener {
TextView name;
EditText address, contact, salary, leaves;
Button attendance;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_genie1);
name = (TextView) findViewById(R.id.txName);
address = (EditText) findViewById(R.id.txAddress);
contact = (EditText) findViewById(R.id.txContact);
salary = (EditText) findViewById(R.id.txSalary);
leaves = (EditText) findViewById(R.id.txLeaves);
Button update=(Button)findViewById(R.id.btUpdate);
update.setOnClickListener(this);
Button delete=(Button)findViewById(R.id.delete);
delete.setOnClickListener(this);
Button attendance = (Button) findViewById(R.id.attendance);
attendance.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showAtt();
}
});
String value = "";
if (getIntent().hasExtra("name")) {
String name = getIntent().getExtras().getString("name");
String add = getIntent().getExtras().getString("add");
String phn = getIntent().getExtras().getString("phn");
String sal = getIntent().getExtras().getString("sal");
String lea = getIntent().getExtras().getString("lea");
}
name.setText(value);
address.setText(value);
contact.setText(value);
salary.setText(value);
leaves.setText(value);
}
#Override
public void onClick(View view) {
final AlertDialog.Builder builder=new AlertDialog.Builder(viewGenie1.this);
builder.setMessage("Are you sure you want to delete records?");
builder.setCancelable(true);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
new deleteTask().execute();
Toast.makeText(viewGenie1.this, "Genie deleted..!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(viewGenie1.this, navDrawer.class));
// GenieService genieService=new GenieService();
// genieService.delete(2);
// Log.d("Information", String.valueOf(genieService.delete(2)));
// Log.i("INFO", genies.get(0).name);
// startActivity(new Intent(viewGenie1.this,Test.class));
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}
private class deleteTask extends AsyncTask {
#Override
protected Object doInBackground(Object[] objects) {
GenieService genieService = new GenieService();
return genieService.delete(6);
}
}
public void showAtt() {
Intent intent = new Intent(this, viewAbsentee.class);
startActivity(intent);
}
}
Here's the xml file of the profile page I have created with hard coded values but want to display the actual values from the local mysql database using an API call:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bcak"
tools:context="com.codionics.geniem.AddGenie"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="313dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#drawable/gradientbackground"
android:orientation="vertical">
<ImageView
android:layout_width="117dp"
android:layout_height="117dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:src="#drawable/genie" />
<TextView
android:id="#+id/txName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Abc"
android:textColor="#ffffff"
android:textSize="21sp"
android:textStyle="bold" />
</LinearLayout>
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="115dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="175dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact"
android:textColor="#f000"
android:textStyle="bold"
android:textSize="20sp" />
<EditText
android:id="#+id/txContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:text="123456789"
android:textColor="#3F51B5"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:textColor="#f000"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="#+id/txAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:text="Pune"
android:textColor="#3F51B5"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
<LinearLayout
android:layout_width="360dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="42dp"
android:paddingLeft="25dp">
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:src="#drawable/ic_attach_money_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="20dp"
android:text="Paid leaves : "
android:textColor="#303F9F"
android:textSize="27dp"
android:textStyle="bold" />
<EditText
android:id="#+id/txLeaves"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:textSize="20dp"
android:textStyle="bold"
android:layout_weight="1"
android:text=" 5" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="42dp"
android:paddingLeft="25dp">
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:src="#drawable/ic_money" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="20dp"
android:text="Salary : "
android:textColor="#303F9F"
android:textSize="27dp"
android:textStyle="bold" />
<EditText
android:id="#+id/txSalary"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:textSize="20dp"
android:textStyle="bold"
android:text=" 5000"
android:textColor="#123" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/btUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginTop="30dp"
android:background="#drawable/buttonstylegradient"
android:text="Update Genie"
android:textColor="#fff" />
<Button
android:id="#+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="80dp"
android:layout_marginTop="-50dp"
android:background="#drawable/buttonstylegradient"
android:text="Delete Genie"
android:textColor="#fff" />
<Button
android:id="#+id/attendance"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/buttonstylegradient"
android:textColor="#fff"
android:text="Attendance" />
</LinearLayout>
I want to display the details in the a profile page like this:-
profile page
Please pass the value in Intent using putExtra()
Intent intent = new Intent(Test.this, viewGenie1.class);
intent.putExtra("key","Value"); //Key must be unique and value should be the value which you want to pass to viewGenie1 class.
startActivity(intent);
In viewGenie1 class you can get the value like this
String value="";
if(getIntent().hasExtra("key")) {
value = getIntent().getExtras().getString("key");
}
Please replace
String value = "";
if (getIntent().hasExtra("name")) {
String name = getIntent().getExtras().getString("name");
String add = getIntent().getExtras().getString("add");
String phn = getIntent().getExtras().getString("phn");
String sal = getIntent().getExtras().getString("sal");
String lea = getIntent().getExtras().getString("lea");
}
name.setText(value);
address.setText(value);
contact.setText(value);
salary.setText(value);
leaves.setText(value);
To
String mName = "",mAdd="",mPhn="",mSal="",mLea="";
if (getIntent().hasExtra("name")) {
mName = getIntent().getExtras().getString("name");
mAdd = getIntent().getExtras().getString("add");
mPhn = getIntent().getExtras().getString("phn");
mSal = getIntent().getExtras().getString("sal");
mLea = getIntent().getExtras().getString("lea");
}
name.setText(mName);
address.setText(mAdd);
contact.setText(mPhn);
salary.setText(mSal);
leaves.setText(mLea);

Tapping rows in Listview not working even w/ SetOnItemClicklistener in Android

For some reason tapping a row in my list view doesn't seem to work even if I have the correct listener code. There are only textviews in the template of the lists. I know there's been discussion about assigning a listener when there is a button in the template of a list view. see here.
Here is my code:
ScheduleActivity:
public class ScheduleActivity extends AppCompatActivity {
private String url;
JSONObject data = null;
Toolbar toolbar;
Intent intent;
String userId;
int eventId;
ListView scheduleListView;
ScheduleAdapter scheduleAdapter;
ArrayList<Schedule> scheduleList = new ArrayList<>();
DBManager dbManager = new DBManager(ScheduleActivity.this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schedule_layout);
Log.d("Test", ">>>ScheduleActivity<<<");
Bundle extras = getIntent().getExtras();
userId = extras.getString("userId");
eventId = extras.getInt("eventId");
toolbar = (Toolbar) findViewById(R.id.schedule_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Schedules");
scheduleList = dbManager.getSchedules(eventId);
scheduleAdapter = new ScheduleAdapter(ScheduleActivity.this, scheduleList);
scheduleListView = (ListView) findViewById(R.id.scheduleListView);
scheduleListView.setAdapter(scheduleAdapter);
scheduleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.w("Test", scheduleList.get(position).toString());
intent = new Intent(ScheduleActivity.this, UpdateScheduleActivity.class);
intent.putExtra("userId", userId);
intent.putExtra("eventId", eventId);
startActivity(intent);
}
});
scheduleListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Log.w("Test", "Long click works");
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.schedule_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.new_schedule) {
intent = new Intent(ScheduleActivity.this, CreateScheduleActivity.class);
intent.putExtra("eventId", eventId);
startActivity(intent);
}else if(id == R.id.rankings){
Log.d("Test", "Rankings Activity clicked!");
}
return super.onOptionsItemSelected(item);
}
ScheduleAdapter:
public class ScheduleAdapter extends ArrayAdapter<Schedule>{
public ScheduleAdapter(Context context, List<Schedule> schedule) {
super(context, R.layout.schedule_list_row, schedule);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater eventInflater = LayoutInflater.from(getContext());
if (convertView == null) {
convertView = eventInflater.inflate(R.layout.schedule_list_row, parent, false);
}
Schedule singleSchedule = getItem(position);
//TODO implement ViewHolder pattern
TextView club1Code = (TextView) convertView.findViewById(R.id.club1TextView);
TextView club2Code= (TextView) convertView.findViewById(R.id.club2TextView);
TextView club1Score = (TextView) convertView.findViewById(R.id.club1ScoreTextView);
TextView club2Score = (TextView) convertView.findViewById(R.id.club2ScoreTextView);
TextView club1SpiritScore = (TextView) convertView.findViewById(R.id.club1SpiritScoreTextView);
TextView club2SpiritScore = (TextView) convertView.findViewById(R.id.club2SpiritScoreTextView);
TextView time = (TextView) convertView.findViewById(R.id.timeTextView);
TextView day = (TextView) convertView.findViewById(R.id.dayTextView);
club1Code.setText(singleSchedule.getClub1Id());
club2Code.setText(singleSchedule.getClub2Id());
day.setText("Day " + singleSchedule.getDay());
time.setText(singleSchedule.getStartTime() + " - " + singleSchedule.getEndTime());
club1Score.setText(Integer.toString(singleSchedule.getClub1Score()));
club2Score.setText(Integer.toString(singleSchedule.getClub2Score()));
club1SpiritScore.setText(Integer.toString(singleSchedule.getClub1SpiritScore()));
club2SpiritScore.setText(Integer.toString(singleSchedule.getClub2SpiritScore()));
return convertView;
}
}
Activity Layout:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.kennanseno.ultimate_scoreboard_app.Activity.ScheduleActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/schedule_toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#2196F3"
android:title="#string/event_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
</android.support.v7.widget.Toolbar>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scheduleListView"
android:layout_centerHorizontal="true"
android:layout_below="#+id/schedule_toolbar" />
</RelativeLayout>
Row Template:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/club1_name_text"
android:id="#+id/club1TextView"
android:textAlignment="textStart"
android:layout_above="#+id/club1SpiritScoreTextView"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/club2_name_text"
android:id="#+id/club2TextView"
android:layout_gravity="right"
android:textAlignment="textEnd"
android:layout_below="#+id/dayTextView"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/score_divider"
android:id="#+id/score_divider"
android:textSize="50sp"
android:layout_below="#+id/timeTextView"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/club1_score_text"
android:id="#+id/club1ScoreTextView"
android:textSize="50sp"
android:layout_marginEnd="22dp"
android:layout_below="#+id/timeTextView"
android:layout_toStartOf="#+id/score_divider"
android:textAlignment="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/club2_score_text"
android:id="#+id/club2ScoreTextView"
android:textSize="50sp"
android:layout_marginStart="22dp"
android:layout_below="#+id/timeTextView"
android:layout_toEndOf="#+id/score_divider"
android:textAlignment="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/start_time_text"
android:id="#+id/timeTextView"
android:textAlignment="center"
android:layout_below="#+id/dayTextView"
android:layout_alignEnd="#+id/club2ScoreTextView"
android:layout_alignStart="#+id/club1ScoreTextView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/club1_spirit_score_text"
android:id="#+id/club1SpiritScoreTextView"
android:textAlignment="textStart"
android:textSize="30sp"
android:layout_alignTop="#+id/club2SpiritScoreTextView"
android:layout_alignStart="#+id/club1TextView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/club2_spirit_score_text"
android:id="#+id/club2SpiritScoreTextView"
android:textAlignment="textEnd"
android:textSize="30sp"
android:layout_below="#+id/club2TextView"
android:layout_alignEnd="#+id/club2TextView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/schedule_day_text"
android:id="#+id/dayTextView"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_alignEnd="#+id/club2ScoreTextView"
android:layout_alignStart="#+id/club1ScoreTextView" />
</RelativeLayout>
In Row Template xml:
android:clickable="true"
causing issue because RelativeLayout is clickable with match_parent height-width.
if you want to get click event for whole row then no need to make parent layout clickable just set setOnItemClickListener to ListView otherwise you need to set onClickListener for RelativeLayout in ScheduleAdapter class.

Android Custom ListView CursorAdapter updating the last item

I'm having issue with my custom listview (CursorAdapter)
When I click btAdd on 2nd item, it's updating the last list item. And also, when I click an item, it doesn't display this
Log.e("Selected", "" + position);
What I wanna do is to update the edittext of the 2nd item or whatever is the selected item.
MainActivity.class
private void initControls(View rootView) {
lv = (ListView) rootView.findViewById(R.id.listView1);
MyTextView tvHeader = (MyTextView) rootView.findViewById(R.id.tvHeader);
DatabaseHelper dbHelper = new DatabaseHelper(getActivity());
DBConnector.dbConnect(dbHelper);
cursor = dbHelper.getAllItems(user);
adapterProd = new CustomAdapter(getActivity(), cursor);
lv.setAdapter(adapterProd);
adapterProd.notifyDataSetChanged();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Log.e("Selected", "" + position);
}
});
if(adapterProd.getCount() < 1) {
tvHeader.setVisibility(View.VISIBLE);
tvHeader.setText(getResources().getString(R.string.no_saved_items));
} else {
tvHeader.setVisibility(View.GONE);
}
}
class CustomAdapter extends CursorAdapter
{
TextView name, price, description;
EditText etQuantity;
ImageButton ibAdd, ibSubtract;
ImageView img;
LayoutInflater inflater;
#SuppressWarnings("deprecation")
public CustomAdapter(Context context, Cursor c) {
super(context, c);
inflater = LayoutInflater.from(context);
}
#Override
public void bindView(final View itemView, Context context, Cursor cursor) {
name = (TextView) itemView.findViewById (R.id.tvName);
price = (TextView) itemView.findViewById(R.id.tvPrice);
description = (TextView) itemView.findViewById(R.id.tvDescription);
etQuantity = (EditText) itemView.findViewById(R.id.etQuantity);
etQuantity.append(String.valueOf(quantity));
etQuantity.setSelection(etQuantity.getText().length());
ibAdd = (ImageButton) itemView.findViewById(R.id.ibAdd);
ibSubtract = (ImageButton) itemView.findViewById(R.id.ibSubtract);
img = (ImageView) itemView.findViewById(R.id.imageView);
checkBox = (CheckBox) itemView.findViewById(R.id.checkBox);
final String selected =
cursor.getString(
cursor.getColumnIndex(Constants.KEY_ID));
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
//Case 1
Log.e("Selected", "" + selected);
itemView.setPressed(true);
}
else {
//case 2
Log.e("Deselected", "" + selected);
itemView.setPressed(false);
}
}
});
ibAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
quantity++;
etQuantity.setText(String.valueOf(quantity));
}
});
ibSubtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
quantity--;
if(quantity < 1) {
quantity = 1;
}
etQuantity.setText(String.valueOf(quantity));
}
});
if(cursor.getString(
cursor.getColumnIndex(Constants.KEY_PRODUCT_NAME)) != null) {
name.setText(cursor.getString(
cursor.getColumnIndex(Constants.KEY_PRODUCT_NAME)));
price.setText(Formatter.formatWithPesoSign(cursor.getString(
cursor.getColumnIndex(
Constants.KEY_PRODUCT_PRICE))));
description.setText(cursor.getString(
cursor.getColumnIndex(Constants.KEY_PRODUCT_NAME)));
} else {
price.setVisibility(View.GONE);
name.setText(cursor.getString(
cursor.getColumnIndex(Constants.KEY_PRODUCT_PARTITION_KEY)));
description.setText(cursor.getString(
cursor.getColumnIndex(Constants.KEY_PRODUCT_ROW_KEY)));
if(ConnectionDetector.hasNetworkConnection(getActivity())) {
GetSavedItemAsyncTask task = new GetSavedItemAsyncTask(
getActivity(), accessToken, sessionKey,
cursor.getString(
cursor.getColumnIndex(Constants.KEY_PRODUCT_PARTITION_KEY)),
cursor.getString(
cursor.getColumnIndex(Constants.KEY_PRODUCT_ROW_KEY)),
cursor.getString(
cursor.getColumnIndex(Constants.KEY_ID)),
user);
task.execute();
}
}
Picasso.with(getActivity())
.load(Constants.displayProductThumbnail(Constants.MERCHANT,
cursor.getString(
cursor.getColumnIndex(Constants.KEY_PRODUCT_ROW_KEY)), "200"))
.placeholder(R.drawable.placeholder)
.into(img);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.list_item, parent, false);
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="5dp"
android:clickable="true"
android:descendantFocusability="beforeDescendants" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:background="#drawable/card_background_selector"
android:descendantFocusability="afterDescendants">
<ImageView
android:layout_marginRight="10dp"
android:scaleType="fitXY"
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/imageView" />
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/imageView"
android:layout_toLeftOf="#+id/checkBox"/>
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:id="#+id/tvPrice"
android:textColor="#color/cadmium_orange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/price"
android:layout_below="#+id/tvDescription"
android:layout_toRightOf="#+id/imageView" />
<TextView
android:id="#+id/tvDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/description"
android:layout_below="#+id/tvName"
android:layout_toRightOf="#+id/imageView" />
<LinearLayout
android:id="#+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/tvPrice"
android:layout_toRightOf="#+id/imageView"
android:layout_toEndOf="#+id/imageView" >
<ImageButton
style="#style/My.Button"
android:id="#+id/ibSubtract"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/ic_remove_white_36dp"
android:padding="5dp" />
<EditText
android:background="#fff"
android:id="#+id/etQuantity"
android:layout_width="30dp"
android:layout_height="30dp"
android:focusable="false"
android:gravity="center"
android:inputType="number" />
<ImageButton
style="#style/My.Button"
android:id="#+id/ibAdd"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="5dp"
android:src="#drawable/ic_add_white_36dp"
android:padding="5dp" />
</LinearLayout>
<CheckBox
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkBox"
android:checked="false" />
</RelativeLayout>
Any ideas? I would gladly appreciate any help. Thanks.
ibAdd Button click always update last row etQuantity TextView because etQuantity is reference to TextView which is return by getView method on last call.
To update clicked row TextView value use setTag/getTag method to get clicked row in onClick of ibAdd and ibSubtract Buttons. like:
ibAdd.setTag(etQuantity);
ibAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView etQuantityView=(TextView)v.getTag();
quantity++;
etQuantityView.setText(String.valueOf(quantity));
}
});
Do same on ibSubtract Button click to show subtracted value
This is improving on ρяσѕρєя K's answer (which had worked for me). This is a better way of using two tags:
ibAdd.setTag(R.id.TAG_ID_1, etQuantity);
ibAdd.setTag(R.id.TAG_ID_2, new Integer(quantity));
ibAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView etQuantityView = (TextView) v.getTag(R.id.TAG_ID_1);
int prevQuantity = (Integer) v.getTag(R.id.TAG_ID_2);
prevQuantity++;
etQuantityView.setText(String.valueOf(prevQuantity));
}
});
NOTE: The ids R.id.TAG_ID_1 and R.id.TAG_ID_2 are required to be defined in the res/values folder as tags.xml. The contents are like this:
<resources>
<item name="TAG_ID_1" type="id"/>
<item name="TAG_ID_2" type="id"/>
</resources>

Categories

Resources