ListView item as a progress bar - list

I was wondering if someone can maybe help me with the following intent:
I created an app which simply displays a table. It shows a table head and beyond that a list view which contains several items.
Is it possible, that if one clicks on one of these items, a progress bar is shown which loads smoothely in a certain time interval (lets say 30 seconds) from left to right?
Please see my screenshot below, maybe it's easier to understand what I am trying to do:
Finally, I don't know if it helps, but let me attach some code as well. I shortened it a little, if something is missing for understanding (like the list class) or if I removed to much, please let me know:
public class TableActivity : Activity
{
ListView lv;
ListAdapter adapter = null;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
//...
lv = FindViewById<ListView>(Resource.Id.geraeteListView);
adapter = new ListAdapter(this, Resource.Layout.List, geraeteliste.CurrentGeraeteliste, Intent.GetStringExtra("ServerIP"))
{
parentActivity = this
};
lv.Adapter = adapter;
}
private async void OnItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
if (geraeteliste.CurrentGeraeteliste[e.Position].bespielt == "J") return;
//…
await Task.Delay(0);
}
}
public class ListAdapter : ArrayAdapter
{
//...
public override View GetView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater inflater = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
v = inflater.Inflate(Resource.Layout.List, parent, false);
}
//...
v.FindViewById<ImageView>(Resource.Id.typeImageView).SetImageResource(Resource.Drawable.Icon);
v.FindViewById<TextView>(Resource.Id.numberTextView).Text = Geraetelist[position].geraeteplatz;
v.FindViewById<TextView>(Resource.Id.descriptionTextView).Text = Geraetelist[position].geraeteinformation;
ImageView iv = v.FindViewById<ImageView>(Resource.Id.bspImageView);
iv.SetImageResource(Geraetelist[position].bespielt == "N" ? Resource.Drawable.greenPoint : Resource.Drawable.redPoint);
//…
return v;
}
}
Can anyone maybe give me a short hint or something? How can I implement such a progress bar for every item on click?^^ Would be really happy for every helping effort.
Thanks in advance for every answer,
Best regards

Is it possible, that if one clicks on one of these items, a progress bar is shown which loads smoothly in a certain time interval (lets say 30 seconds) from left to right?
If you want to display progressbar when click Listview item, I do one sample for you, you can take a look:
1.Declare ListView layout
<?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">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ListView" />
</LinearLayout>
2.Declare list row layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ProgressBar android:id="#+id/progressBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:visibility="invisible"
style="#android:style/Widget.ProgressBar.Horizontal" />
</LinearLayout>
3.Define ListView Adapter
public class Post
{
public string title { get; set; }
public bool isselect { get; set; }
}
public class CusotmListAdapter : BaseAdapter<Post>
{
Activity context;
List<Post> list;
public CusotmListAdapter(Activity _context, List<Post> _list)
: base()
{
this.context = _context;
this.list = _list;
}
public override Post this[int position]
{
get { return list[position]; }
}
public override int Count
{
get { return list.Count; }
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
// re-use an existing view, if one is available
// otherwise create a new one
if (view == null)
view = context.LayoutInflater.Inflate(Resource.Layout.ListItemRow, parent, false);
Post item = this[position];
view.FindViewById<TextView>(Resource.Id.textView1).Text = item.title;
if(item.isselect==true)
{
view.FindViewById<ProgressBar>(Resource.Id.progressBar1).Visibility = ViewStates.Visible;
}
else
{
view.FindViewById<ProgressBar>(Resource.Id.progressBar1).Visibility = ViewStates.Invisible;
}
return view;
}
}
4.Setting ListView Adapter
public class MainActivity : AppCompatActivity
{
private ListView listview;
List<Post> posts = new List<Post>();
private CusotmListAdapter adapter;
private int oldposition = -1;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
posts.Add(new Post() { title = "title 1", isselect = false });
posts.Add(new Post() { title = "title 2", isselect = false });
posts.Add(new Post() { title = "title 3", isselect = false });
posts.Add(new Post() { title = "title 4", isselect = false });
posts.Add(new Post() { title = "title 5", isselect = false });
posts.Add(new Post() { title = "title 6", isselect = false });
posts.Add(new Post() { title = "title 7", isselect = false });
posts.Add(new Post() { title = "title 8", isselect = false });
SetContentView(Resource.Layout.activity_main);
listview = FindViewById<ListView>(Resource.Id.ListView);
listview.ItemClick+= OnListItemClick;
adapter= new CusotmListAdapter(this, posts);
listview.Adapter = adapter;
}
private void OnListItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
if(oldposition!=-1)
{
Post p1 = adapter[oldposition];
p1.isselect = false;
}
oldposition = e.Position;
Post p = adapter[e.Position];
p.isselect = true;
adapter.NotifyDataSetChanged();
}
}
Update
You can change some code in CusotmListAdapter GetView event:
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
// re-use an existing view, if one is available
// otherwise create a new one
if (view == null)
view = context.LayoutInflater.Inflate(Resource.Layout.ListItemRow, parent, false);
Post item = this[position];
view.FindViewById<TextView>(Resource.Id.textView1).Text = item.title;
ProgressBar p = view.FindViewById<ProgressBar>(Resource.Id.progressBar1);
p.Max = 100;
p.Progress = 100;
//p.IncrementProgressBy(-10);
if(item.isselect==true)
{
view.FindViewById<ProgressBar>(Resource.Id.progressBar1).Visibility = ViewStates.Visible;
new Thread(new ThreadStart(delegate
{
while (p.Progress > 0)
{
p.Progress -= 20;
Thread.Sleep(1000);
}
})).Start();
}
else
{
view.FindViewById<ProgressBar>(Resource.Id.progressBar1).Visibility = ViewStates.Invisible;
}
return view;
}
Then please look at the screenshot:

It's clear that you are missing some knowledge on ListViews. This Xamarin university course has some examples and a video that explain the different components.
But basically, you can put your background progress bar as a View in your row AXML layout file, and depending on the value of the Progress property, that view can change widths.
Let me know if you have questions

Related

Take pictures from fragmento Android

I'm trying to take a picture from a Fragment but Activityresultlauncher won't fire, it works in Activity but I'm using Viewpager/Tablayout.
When I debugged only fire when I cancel the camera.
Please someone can Help me.
Here's my code.
public class FragmentUsuario extends Fragment {
FragmentUsuarioBinding dtb;
ActivityResultLauncher<Intent> launcherVoltAct = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
int rotation = MisFunciones.getImageOrientation(mFotoFile.getAbsolutePath());
File newImage = MisFunciones.reSaveBitmap(MisFunciones.ruta_fotos + File.separator, MisFunciones.getCode() + ".JPG", rotation, mFotoFile);
}
});
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
dtb = FragmentUsuarioBinding.inflate(inflater, container, false);
return dtb.getRoot();
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
dtb.fotoVoltAct.setOnClickListener(v -> takeFoto());
}
private void takeFoto() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.setPackage("net.sourceforge.opencamera");
if (cameraIntent.resolveActivity(requireActivity().getPackageManager()) != null) {
mFotoFile = MisFunciones.crearImageFile();
Uri uri = FileProvider.getUriForFile(requireContext(), requireActivity().getPackageName() + ".provider", mFotoFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
launcherVoltAct.launch(cameraIntent);
}
}
}

Xamarin.Android: Changing a specific value in a list during runtime

I have a simple Xamarin.Android-App.
On the Activity of this app, there is a list displayed. Please see following MyActivity.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation ="vertical"
android:layout_width ="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width ="match_parent"
android:layout_height="wrap_content"
android:text ="Click Me"
android:layout_marginTop ="20dp"
android:layout_marginLeft ="25dp"
android:layout_marginRight ="25dp"
android:id="#+id/button" />
<Space
android:layout_width="match_parent"
android:layout_height="25dp"
android:id="#+id/space" />
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list" />
</LinearLayout>
One row of this list contains simply two entries, please see ListRow.axml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft ="5dp"
android:layout_marginTop ="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight ="5dp" />
<TextView
android:id="#+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft ="5dp"
android:layout_marginTop ="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight ="5dp" />
</RelativeLayout>
So the code-behind of that activity looks like the following:
public class MyActivity : Activity
{
List<Entry> List = null;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.MyActivity);
List = PopulateList();
var lv = FindViewById<ListView>(Resource.Id.list);
var adapter = new ListAdapter(this, Resource.Layout.List, List);
lv.Adapter = adapter;
FindViewById<Button>(Resource.Id.button).Click += OnButtonClick;
}
}
For the sake of completeness, here is the code for my ListAdapter.cs-class:
class ListAdapter : ArrayAdapter
{
List<Entry> List;
public ListAdapter(Context Context, int ListId, List<Entry> List) : base(Context, ListId, List)
{
this.List = List;
}
public override int Count
{
get { return List.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater inflater = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
v = inflater.Inflate(Resource.Layout.List, parent, false);
}
v.FindViewById<TextView>(Resource.Id.name).Text = List[position].Name;
v.FindViewById<TextView>(Resource.Id.value).Text = "Original Value";
return v;
}
}
So my question now is the following: Assuming that there is more than one item within that List. On click of the button, I want to change one specific text within that list. Let's say of the second entry in the list the (Resource.Id.value).Text (which now says "Original Value") to "Changed Value" or something like that. But only of the second one. All the other items should stay the same.
Please see following example of output aimed, maybe it's easier to understand what I am trying to do:
Name Value
- - - - - - - - - - - - - - - -
No.1 Original Value
No.2 Original Value
No.3 Original Value
[Button Click]
Name Value
- - - - - - - - - - - - - - - -
No.1 Original Value
No.2 Changed Value
No.3 Original Value
Can anyone maybe help me / tell me how to do this? What does my private void OnButtonClick(object sender, EventArgs e)-method have to look like? How can I access a single entry in that list?
Thanks in advance for all answers and best regards
you could add a method in adapter to notifyDataSetChanged(); :
class ListAdapter : ArrayAdapter
{
List<Entry> List;
private int ItemPositionToChange = -1;
public ListAdapter(Context Context, int ListId, List<Entry> List) :
base(Context, ListId, List)
{
this.List = List;
}
//this method to let adapter notify
public void ChangeItemValue(int position)
{
ItemPositionToChange = position;
NotifyDataSetChanged();
}
public override int Count
{
get { return List.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater inflater =
(LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
v = inflater.Inflate(Resource.Layout.List, parent, false);
}
v.FindViewById<TextView>(Resource.Id.name).Text = List[position].Name;
//change the item value according to your needs
if(position == ItemPositionToChange ){
v.FindViewById<TextView>(Resource.Id.value).Text = "Changed Value";
}else{
v.FindViewById<TextView>(Resource.Id.value).Text = "Original Value";
}
return v;
}
}
then in your button click listener :
void OnButtonClick(object sender, EventArgs e)
{
//positionToChange is which item you want to change
adapter.ChangeItemValue(positionToChange);
}
I'm not too familiar with the Entry class, but it seems to have a Text property.
Assuming you know the index of the string (myIndex below):
private void OnButtonClick(object sender, EventArgs e) {
List[myIndex].Text = "Changed Value";
adapter.notifyDataSetChanged();
}

How to set image in list view by using string

I have two activities namely mainactivity and main2activity. Mainactivity has three buttons and main2activity has list view.
When I click button string is passed to main2activity and is displayed in main2activity.
So now I want also image in listview. corresponding image for button selected. Below are my codes.
MainActivity
public class MainActivity extends AppCompatActivity {
EditText editText;
Button addButton;
String text1 ="Item 1";
String text2 ="Item 2";
String text3 ="Item 3";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("text",text1);
startActivity(intent);
}
});
Button button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("text",text2);
startActivity(intent);
}
});
Button button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("text",text3);
startActivity(intent);
}
});
}
Main2activity
public class Main2Activity extends AppCompatActivity {
ListView listView;
ArrayAdapter<String> adapter;
#Override
protected void onRestart() {
super.onRestart();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Global.list1);
listView.setAdapter(adapter);
if (Global.list1.size() < 1) {
Global.list1 = new ArrayList<>();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent intent1 = getIntent();
listView = (ListView) findViewById(R.id.list);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Global.list1);
listView.setAdapter(adapter);
String havestring = intent1.getStringExtra("text");
if (havestring != null) {
Global.list1.add(havestring);
adapter.notifyDataSetChanged();
}
Button backbtn = (Button) findViewById(R.id.backbtn);
backbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Main2Activity.this, MainActivity.class);
startActivity(intent);
}
});
}
}
row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageview"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp" />
<TextView
android:id="#+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />
</LinearLayout>
So how to set custom adapter that will add corresponding image with string added.
Can it be done using if else statement in custom adapter???
Please do help me
for same.
Can anyone help me out???

On every button click data is added to listview but in an unexpected manner . Data is repeated in the listview

[Activity]
public class MainActivity : Activity
{
public string[] items = new string[100]; // string to store data from editText
int i =0;
ArrayAdapter<string > adapter;
protected override void OnCreate(Bundle savedInstance)
{
base.OnCreate(savedInstance);
SetContentView(R.Layouts.MainLayout);
var btn1= FindViewById <Button> (R.Ids.btn1);
btn1.Click += AddItem; // calls a function on button click to add data to listview
}
public void AddItem(object sender,EventArgs eventargs)
{
try
{
var et1 = FindViewById <EditText>(R.Ids.et1);
items[i] = et1.Text.ToString();
i++;
view(); //to add data to listview
}
catch(Exception ex)
{
error(ex.ToString());
}
}
public void view ()
{
adapter = new ArrayAdapter<string>(GetApplicationContext(),Android.R.Layout.Simple_list_item_1,items);
ListView listview = FindViewById<ListView>(R.Ids.listview1);
listview.SetAdapter(adapter);
listview.SetTextFilterEnabled(true);
}
}
You should not call view() from AddItem because all code in view is initialization code. Rename it initList or so and call it from OnCreate.
You also don't need items (or i). Just add the string to adapter.
-- EDIT
[Activity]
public class MainActivity : Activity
{
private ArrayAdapter<string> adapter;
protected override void OnCreate(Bundle savedInstance)
{
base.OnCreate(savedInstance);
SetContentView(R.Layouts.MainLayout);
ListView list = FindViewById<ListView>(R.Ids.list);
adapter = new ArrayAdapter<string>(this, Android.R.Layout.Simple_list_item_1);
list.SetAdapter(adapter);
Button button = FindViewById<Button>(R.Ids.button);
button.Click += button_Click;
}
void button_Click(object sender, EventArgs e)
{
EditText text = FindViewById<EditText>(R.Ids.text);
adapter.Add(text.Text.ToString());
}
}

how to refresh the linear layout view after deleting an element

I have a simple app, in one activity I take name and date of birth. I store it in the database. and in the main activity I have linearlayout which will show all the names.
When I click on any of the name in the main activity, it should delete that name from the database and also refresh the view.
I am able to delete the entry from database, but my linear layout view is not being updated. Can some one pls help.
public class child extends Activity {
private Intent intent;
private LinearLayout layout;
private LayoutInflater linflater;
private int i =0;
private Cursor cr;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.child);
layout = (LinearLayout)findViewById(R.id.layout);
Button addBtn = (Button)findViewById(R.id.AddButton);
Button remBtn = (Button)findViewById(R.id.RemoveButton);
intent = new Intent(this,login.class);
layout = (LinearLayout) findViewById(R.id.mylayout1);
linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Check the database if there are any entries available. If available, then
//list them on the main screen
final myDBAdapter mydb = new myDBAdapter(getApplicationContext());
mydb.open();
cr = mydb.GetMyData();
if(cr.getCount()>0)
{
cr.moveToFirst();
for (int i=0;i<cr.getCount();i++)
{
cr.moveToPosition(i);
buildList(cr.getString(1),cr.getString(2));
}
}
//Start the login activity which will return the newly added baby name
addBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivityForResult(intent, 1001);
}
});
//Remove all the entries from Database
remBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(cr.getCount()>0)
{
cr.moveToFirst();
for (int i=0;i<cr.getCount();i++)
{
Toast.makeText(getApplicationContext(), cr.getString(1),
Toast.LENGTH_LONG).show();
mydb.RemoveEntry(cr.getString(1));
cr.moveToPosition(i);
}
}
}
});
mydb.close();
}
private void buildList(final String bname,String bsex)
{
final View customView = linflater.inflate(R.layout.child_view,
null);
TextView tv = (TextView) customView.findViewById(R.id.TextView01);
//tv.setId(i);
tv.setText(bname);
tv.setTextColor(getResources().getColor(R.color.black));
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myDBAdapter mydb = new myDBAdapter(getApplicationContext());
mydb.open();
if (mydb.RemoveEntry(bname)>0)
{
Toast.makeText(getApplicationContext(), "Row deleted",
Toast.LENGTH_LONG).show();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
WHAT IS REQUIRED HERE TO UPDATE THE VIEW???
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
else
{
Toast.makeText(getApplicationContext(), "Row not deleted",
Toast.LENGTH_LONG).show();
}
}
});
layout.addView(customView);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 1001)
{
if(resultCode == RESULT_OK)
{
Bundle extras = data.getExtras();
buildList(extras.getString("bname"),extras.getString("bsex"));
}
}
}
}
Hmm I'm also trying to get this to work, Have you tried looking at maybe refreshing the LinearLayout every say 5 seconds using a game loop? This may prove to be useful as it helped me with my problem http://aubykhan.wordpress.com/2010/04/25/android-game-programming-the-game-loop/
You may also be able to call onCreate(Bundle) function again while this is a terrible terrible way to do this it will work.