I'm doing a product sales project. I am using the codes below for the product list, but I want to prevent other products from being affected when I click on one. How can I resolve this?
My Activity class:
public class ProductListActivity extends AppCompatActivity {
private ArrayList<ClassListItems> itemArrayList; //List items Array
private MyAppAdapter myAppAdapter; //Array Adapter
private ListView listView; // Listview
private boolean success = false; // boolean
private ConnectionHelper connectionClass; //Connection Class Variable
private ImageButton btnComplete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_list);
listView = (ListView) findViewById(R.id.listView); //Listview Declaration
connectionClass = new ConnectionHelper(); // Connection Class Initialization
itemArrayList = new ArrayList<ClassListItems>(); // Arraylist Initialization
btnComplete = (ImageButton) findViewById(R.id.btnComplete);
btnComplete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*Orfiche gl = (Orfiche) getApplicationContext();
gl.setAmount("0");*/
finish();
}
});
// Calling Async Task
SyncData orderData = new SyncData();
orderData.execute("");
}
// Async Task has three overrided methods,
private class SyncData extends AsyncTask<String, String, String>
{
String msg = "Internet/DB_Credentials/Windows_FireWall_TurnOn Error, See Android Monitor in the bottom For details!";
ProgressDialog progress;
@Override
protected void onPreExecute() //Starts the progress dailog
{
progress = ProgressDialog.show(ProductListActivity.this, "Synchronising",
"Listview Loading! Please Wait...", true);
}
@Override
protected String doInBackground(String... strings) // Connect to the database, write query and add items to array list
{
try
{
Connection conn = connectionClass.connection(); //Connection Object
if (conn == null)
{
success = false;
}
else {
// Change below query according to your own database.
String query = "";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
if (rs != null) // if resultset not null, I add items to itemArraylist using class created
{
while (rs.next())
{
try {
itemArrayList.add(new ClassListItems(rs.getString("NAME"),rs.getString("CODE"),rs.getString("GERCEK_STOK")));
} catch (Exception ex) {
ex.printStackTrace();
}
}
msg = "Found";
success = true;
} else {
msg = "No Data found!";
success = false;
}
}
} catch (Exception e)
{
e.printStackTrace();
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
msg = writer.toString();
success = false;
}
return msg;
}
@Override
protected void onPostExecute(String msg) // disimissing progress dialoge, showing error and setting up my listview
{
progress.dismiss();
Toast.makeText(ProductListActivity.this, msg + "", Toast.LENGTH_LONG).show();
if (success == false)
{
}
else {
try {
myAppAdapter = new MyAppAdapter(itemArrayList, ProductListActivity.this);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(myAppAdapter);
} catch (Exception ex)
{
}
}
}
}
public class MyAppAdapter extends BaseAdapter //has a class viewholder which holds
{
public class ViewHolder
{
TextView textName, textCode, textStock;
EditText edtAmount;
Button productAdd;
}
public List<ClassListItems> parkingList;
public Context context;
ArrayList<ClassListItems> arraylist;
private MyAppAdapter(List<ClassListItems> apps, Context context)
{
this.parkingList = apps;
this.context = context;
arraylist = new ArrayList<ClassListItems>();
arraylist.addAll(parkingList);
}
@Override
public int getCount() {
return parkingList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) // inflating the layout and initializing widgets
{
View rowView = convertView;
ViewHolder viewHolder= null;
if (rowView == null)
{
LayoutInflater inflater = getLayoutInflater();
rowView = inflater.inflate(R.layout.activity_list_items, parent, false);
viewHolder = new ViewHolder();
viewHolder.textName = (TextView) rowView.findViewById(R.id.textName);
viewHolder.textCode = (TextView) rowView.findViewById(R.id.textCode);
viewHolder.textStock = (TextView) rowView.findViewById(R.id.textStock);
viewHolder.edtAmount = (EditText) rowView.findViewById(R.id.edtAmount);
viewHolder.productAdd = (Button ) rowView.findViewById(R.id.btnAdd);
rowView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
// here setting up names and images
viewHolder.textName.setText(parkingList.get(position).getName()+"");
viewHolder.textCode.setText(parkingList.get(position).getCode()+"");
viewHolder.textStock.setText(parkingList.get(position).getStock()+"");
viewHolder.edtAmount.getText();
ViewHolder finalViewHolder = viewHolder;
View finalRowView = rowView;
ArrayList<String> myList = new ArrayList<String>();
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BottomSheetDialog bottomSheet = new BottomSheetDialog();
bottomSheet.show(getSupportFragmentManager(),
"ModalBottomSheet");
String name,code;
double amount;
name = finalViewHolder.textName.getText().toString();
code = finalViewHolder.textCode.getText().toString();
amount = Double.parseDouble(finalViewHolder.edtAmount.getText().toString());
OrfLine orfLine = new OrfLine();
orfLine.setName(name);
System.out.println("ProductList : "+ orfLine.getName());
orfLine.setCode(code);
orfLine.setAmount(amount);
finalViewHolder.textName.setTextColor(Color.GREEN);
}
});
return rowView;
}
}
}
activity_product_list.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"
tools:context=".Activity.ProductListActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="Search" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/ic_search_blue" />
<ImageButton
android:id="@+id/btnComplete"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@android:drawable/checkbox_on_background" />
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="multipleChoice" />
</LinearLayout>``
*activity_list_items*
enter <?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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:backgroundTint="@color/cardview_dark_background"
tools:context=".Activity.ListItemsActivity">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:backgroundTint="#00FFFFFF">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textCode"
android:layout_width="100dp"
android:layout_height="match_parent"
android:maxLines="1"
android:textColor="@color/black"
android:text="TextView" />
<TextView
android:id="@+id/textName"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:textColor="@color/black"
android:text="TextView" />
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:orientation="vertical">
<EditText
android:id="@+id/edtAmount"
android:layout_width="72dp"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="right"
android:text="0"
android:textColor="@color/black"
android:focusableInTouchMode="true"/>
<TextView
android:id="@+id/textStock"
android:layout_width="72dp"
android:layout_height="wrap_content"
android:ems="10"
android:enabled="false"
android:gravity="right"
android:inputType="number"
android:text="0"
android:textColor="@color/black"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
For example, when you click on an item in the onclick event, 5 more texts become green.