logo
down
shadow

ListView - change items from 1 to 2 lines


ListView - change items from 1 to 2 lines

By : user2955050
Date : November 22 2020, 10:33 AM
fixed the issue. Will look into that further How can I modify the code below so that each item has two lines instead of one? I want the code in the mSample section the same layout as I've posted so please do not inappropriately modify it unless it is necessary. Below is an image depicting what the list looks like with one line. , maybe it will help you
code :
public class MainActivity extends ListActivity {

   /*
     current code
   */

    public void onCreate(Bundle savedInstanceState) {
     //
     //

       setListAdapter(new SampleAdapter(this,
            android.R.layout.simple_list_item_1,
            mSamples));
    }

    @Override
    protected void onListItemClick(ListView listView, View view, int position, long id) {
        // Launch the sample associated with this list position.
        startActivity(new Intent(MainActivity.this, mSamples[position].activityClass));
    }

    public static class SampleAdapter extends ArrayAdapter<Sample>{

            SampleAdapter(Context context, int resource, Sample[] objects) {
                super(context, resource, objects);
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View rootView = super.getView(position, convertView, parent);
                TextView firstLine = rootView.findViewById(R.id.list_item_entry_title);
                TextView secondLine = rootView.findViewById(R.id.list_item_entry_summary);
                firstLine.setText(getItem(position)./*Sample class field*/);
                secondLine.setText(getItem(position)./*Sample class field*/);
                return rootView;
            }
        }
}


Share : facebook icon twitter icon
How do i add the items to the listView so i will see them each two lines one under the other one?

How do i add the items to the listView so i will see them each two lines one under the other one?


By : Maria Guzenko
Date : March 29 2020, 07:55 AM
around this issue If I understand your problem correctly then there is some text at index 0 and some date at index 1 and you want to concat them and add it to your list. Similarly for index 2 and 3 and so on.
I have tried to address your problem in the below code. See if it suits your need and let me know.
code :
        //Dummy values for testing
        List<string> lines = new List<string>();
        lines.Add("Hello from text");
        lines.Add("Wed, 29 Jan 2014 06:01:56 +0200");
        lines.Add("Hello from text1");
        lines.Add("Wed, 29 Jan 2014 06:01:56 +0200");

        StringBuilder strToAddToList = new StringBuilder();

        for (int i = 0; i < lines.Count; i++)
        {
            if (i % 2 == 0)
            {
                strToAddToList.Append(lines[i]);
                continue;
            }

            if (i % 2 != 0)
            {
                strToAddToList.Append(" ");
                strToAddToList.Append(lines[i]);
                Console.WriteLine(strToAddToList);//You can replace this with your ListView.Items.Add code
                strToAddToList.Clear();
            }
        }
ListViewItem item = new ListViewItem();
item.Text = strToAddToList.ToString();
listView1.Items.Add(item);
listView1.Items.Add(strToAddToList.ToString());
Items ImageButton drawable change upon click in ListView seems to change for 2 items at a time

Items ImageButton drawable change upon click in ListView seems to change for 2 items at a time


By : Rich
Date : March 29 2020, 07:55 AM
this will help You need to reset your bookmark image resource & tag values a non-null convertView is passed in to getView. Your onClick handler is setting them to these values:
code :
bookmark.setImageResource(R.drawable.ic_bookmarked);
bookmark.setTag(R.drawable.ic_bookmarked);
Windows 10 Development: How to refresh ListView whenever there is a change in the items inside ListView?

Windows 10 Development: How to refresh ListView whenever there is a change in the items inside ListView?


By : Jose Hernandez
Date : March 29 2020, 07:55 AM
around this issue First I would advise you to read about MVVM, and try to follow some basic tutorials like this one.
You can use MVVM Light to avoid managing the INotifyPropertyChanged by yourself at first (but it's really good to know how MVVM light work under the hood).
Change background color of ListView Items if MATCH found (Android ListView)

Change background color of ListView Items if MATCH found (Android ListView)


By : Stanislav Burov
Date : March 29 2020, 07:55 AM
this will help In my opinion, an Activity shouldn't take care how an item from ListView is displayed, letting this job for an adapter (you're doing this in onCreate without having the rows for ListView created, resulting in a crash).
So what I would do is to create a class that has 2 fields: String value where you have your actual data to be displayed and a boolean hasBackground that indicates if that item should be displayed with a custom background or not. That class should look like this:
code :
public class ListItem {
  public String value;
  public boolean hasBackground = false;

  public ListItem(String value, boolean background) {
    this.value = value;
    this.hasBackground = background;
  }
}
private ListItem getDefaultItem(String value) {
    return new ListItem(value, false);
} 
private void updateCommonElements() {
    for (int j = 0; j < selectedGlobalInstruments.size(); j++) {
      for (int i = 0; i < forexInstruments.size(); i++) {
        String instrumentList = "'" + forexInstruments.get(i).value + "'";
        if (selectedGlobalInstruments.get(j).equals(instrumentList)) {
          forexInstruments.get(i).hasBackground = true;
        }
      }
    }
}
forexInstruments = new ArrayList<>();
forexInstruments.add(getDefaultItem("USDGPY"));
forexInstruments.add(getDefaultItem("AUDUSD"));
forexInstruments.add(getDefaultItem("EURGBP"));
forexInstruments.add(getDefaultItem("USDJPY"));
forexInstruments.add(getDefaultItem("GBPUSD"));
forexInstruments.add(getDefaultItem("EURGPY"));
forexInstruments.add(getDefaultItem("USDCHF"));

updateCommonElements();

forexListView = (ListView) findViewById(R.id.listView);
MyAdapter adapter = new MyAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1);
adapter.addAll(forexInstruments);
forexListView.setAdapter(adapter);
public class MyAdapter extends ArrayAdapter<ListItem> {
  int res = 0;
  int textRes = 0;

  public MyAdapter(Context context, int resource, int textViewResourceId) {
    super(context, resource, textViewResourceId);
    res = resource;
    textRes = textViewResourceId;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
      convertView = LayoutInflater.from(parent.getContext()).inflate(res, parent, false);
    }
    ListItem item = getItem(position);
    if (item.hasBackground) {
      convertView.setBackgroundColor(Color.BLUE);
    } else {
      convertView.setBackgroundColor(Color.TRANSPARENT);
    }
    TextView textView = (TextView) convertView.findViewById(textRes);
    textView.setText(item.value);
    return convertView;
  }
}
On clicking the button in listview item, change occurs in some others listview items too

On clicking the button in listview item, change occurs in some others listview items too


By : Ashish Roy
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further That's when you use an int flag for the list item's position as a conditional statement before updating the ListView's items.
So in your case, the Adapter class would look something like this:
code :
...

private int mPosition = -1; // Int flag that doesn't take effect UNTIL it has been set

...

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    a = getItem(position);

    View view = convertView;
    try {
        if (convertView == null) {
            v = new ViewHolder();
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_list_item, parent, false);
            v.imgView = (ImageView) convertView.findViewById(R.id.iv_list_image);
            v.desc = (Button) convertView.findViewById(R.id.btn_desc);
            v.des = (TextView) convertView.findViewById(R.id.tv_list_desc);
            v.ll = (LinearLayout) convertView.findViewById(R.id.ll_desc);

            convertView.setTag(v);
        } else {
            v = (ViewHolder) convertView.getTag();
        }

        v.desc.setTag(position);

        //Picasso.with(getContext()).load(a.getFile()).fit().into(v.imgView);
        Glide.with(context).load(a.getFile()).centerCrop().placeholder(R.drawable.dualring)
                .into(v.imgView);

        // Runs the following functionality if the positions match. Otherwise, hides the layout.
        if (mPosition == position) {
            Toast.makeText(getContext(), ""+v.desc.getTag(), Toast.LENGTH_SHORT).show();
            v.des.setText(""+a.getDescription());
            v.ll.setVisibility(View.VISIBLE);
        } else {
            v.ll.setVisibility(View.INVISIBLE);
        }

        v.desc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v1) {
                mPosition = position; // Sets the flag to the position that was clicked

                notifyDataSetChanged(); // Updates the data instantly
            }
        });

        return convertView;

    } catch (Exception e) {
        return null;
    }
}
Related Posts Related Posts :
  • Passing HttpServletResponse in Spring Boot controller method
  • Failed to load libraries: [netty_tcnative_linux_arm_32, netty_tcnative_linux_arm_32_fedora, netty_tcnative_arm_32, netty
  • How to build a small documents storage?
  • Getting pixel position of a text in an image
  • How do I get a list of Methods called from a Class in Eclipse IDE?
  • Convert string to day of week (not exact date)
  • Unable to understand how rounding is working for decimal digits (Java)
  • How to avoid java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.copy(Ljava/io/InputStream;Ljava/io/OutputStream;)
  • JTable Renderer not working with RowSorter?
  • From java.util.concurrent.Future<T> to play.libs.F.Promise<T>: How to do that in Java?
  • jUnit tests work in Eclipse, but fail in ant - java.lang.RuntimeException: Stub
  • How do i add a tooltip to existing gwt Textbox?
  • actor replying to non-actor
  • Google Cloud Messaging - server connection
  • Which classes are unmodifiable and cannot be intrumented and is there a way to bypass it?
  • Java & Jersey 1.17: Custom default response headers
  • Java rounding error
  • Setting custom RejectedExecutionHandler for ThreadPoolExecutor after executing some tasks?
  • Arquillian test class variables lose value
  • java.util.ConcurrentModificationException while inserting in ArrayList
  • How to write generic Java API without method Overloading
  • Java Serialization / Deserialization of an ArrayList works only on first program execution
  • Conway's Game of Life's rules dont apply properly
  • call a java method when Click on a html button without using javascript
  • Why does SSLSocketFactory.createSocket returns a Socket instead of SSLSocket?
  • How to schedule a task in Tomcat
  • compare two pdf files (approach) using java
  • What is the use of allowCoreThreadTimeout( ) in ThreadPoolExecutor?
  • Describe the Strategy Design Pattern in Java?
  • MongoDB too many open connections
  • Issue with generate JAXB classes with value constructors
  • Moving from one frame to another in java swing
  • Get a thread by Id
  • how to open front camera and back camera at same time?
  • Messed up with Java Declaration
  • Extracting chart in EXcel as image using java
  • How to get latitude and longitude in Android phone?
  • Do GC pauses and kill -STOP produce the same behavior?
  • Android HTML.ImageGetter Images resized
  • The way to instantiate map<String, List<String>> in Java
  • Java Making objects move while buttons held
  • Androidx Navigation View - `setNavigationItemSelectedListener` Doesn't Work
  • after compiling code JFrame does not appear
  • Netbeans project, JFrames ran from the main JFrame close all JFrames in project
  • Square Retrofit Client: How to enable/disable followRedirects? How to intercept redirect URL?
  • Java Circular Reference - Can’t Compile
  • Mouse click on specific time JAVA
  • android 4 onCreateDialog return type incompatible when overriding
  • Java Implementation of Oracle's DBMS_UTILITY.GET_HASH_VALUE Function
  • javac cannot be run, and furthermore does not seem installed
  • Function to obtain a thread given its ID
  • Java File path to Executable has spaces
  • Minimum Oracle Java VM heap memory setting?
  • Elegant way to create one of a large number of classes
  • Java fx binding styles
  • Summing weights based on string in large file
  • Android ListView setOnItemClickListener not working
  • How is the Java Bootstrap Classloader loaded?
  • Check object is blank
  • Add image between text in JEditorPane
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org