Search code examples
androidtextviewandroid-linearlayoutandroid-alertdialoghorizontalscrollview

Detect LinearLayout's selected item in the HorizontalScrollView


I have LinearLayout in the HorizontalScrollView which is horizontal scrolable.

  <HorizontalScrollView
android:id="@+id/scrollMessageFiles"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:layout_below="@+id/editMessage"
android:orientation="horizontal"
android:weightSum="1.0" >
        <LinearLayout
    android:id="@+id/panelMessageFiles"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    >

        </LinearLayout>

        </HorizontalScrollView>

I add TextView's to my LinearLayout and ScrollView programmaticaly next way:

public void addFiles()
 {

    if(!FileManagerActivity.getFinalAttachFiles().isEmpty())
    {
             for (File file: FileManagerActivity.getFinalAttachFiles())
              {
                    View line = new View(this);
                    line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT));
                    line.setBackgroundColor(0xAA345556);
                    informationView = new TextView(this);
                    informationView.setId(102);
                    informationView.setTextColor(Color.BLACK);
                    informationView.setTextSize(12);
                    informationView.setCompoundDrawablesWithIntrinsicBounds(
                            0, R.drawable.file_icon, 0, 0);
                    informationView.setText(file.getName().toString());
                    layout.addView(informationView, 0);
                    layout.addView(line, 1);


              }

      }

}

It works properly. I want to detect what item of LinearLayout was selected in the HorizontalScrollView. I do it next way:

layout.setOnLongClickListener(new View.OnLongClickListener() {

        public boolean onLongClick(View v) {
            final CharSequence[] items = {"Open", "Delete", "Details"};

            final AlertDialog.Builder builder = new AlertDialog.Builder(NewMessageActivity.this);
            int childCount = layout.getChildCount()/2;


            for (int i = 0; i < childCount; i++) 
            {
                final View child = layout.getChildAt(i);

                if (child instanceof TextView)
                   builder.setTitle(((TextView)child).getText().toString());
            }



            builder.setItems(items, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {

                }
            });
            AlertDialog alert = builder.create();
            alert.show();
            return false;
        }
    });

But the title of the AlertBuilder is setting with first item from the list, but not those which I had chosed. How can I detect what item was selected and set its name to the AlertDialog title?


Solution

  • I solved this by changing my adding TextView's to the LinearLayout in this way:

    public void addFiles()
     {
    
        if(!FileManagerActivity.getFinalAttachFiles().isEmpty())
        {
            TextView tv[] = new TextView[FileManagerActivity.getFinalAttachFiles().size()];
                 for (int i = 0; i< FileManagerActivity.getFinalAttachFiles().size();i++)
                  {
    
    
                        View line = new View(this);
                        line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT));
                        line.setBackgroundColor(0xAA345556);
                        tv[i] = new TextView(this);
                        tv[i].setId(i);
                        tv[i].setTextColor(Color.BLACK);
                        tv[i].setTextSize(12);
                        tv[i].setCompoundDrawablesWithIntrinsicBounds(
                                0, R.drawable.file_icon, 0, 0);
                        tv[i].setText(FileManagerActivity.getFinalAttachFiles().get(i).getName().toString());
                        tv[i].setOnLongClickListener(onclicklistener);
                        layout.addView(tv[i], 0);
                        layout.addView(line, 1);
    
    
                  }
    
          }
    
    
    
    }
    

    I added setOnLongClickListener to the array of my TextView's which length is equal to my list's length. My Listener:

    OnLongClickListener onclicklistener = new OnLongClickListener() {
    
    
        public boolean onLongClick(View arg0) {
            final CharSequence[] items = {"Open", "Delete", "Details"};
    
    
            final AlertDialog.Builder builder = new AlertDialog.Builder(NewMessageActivity.this);
    
    
            builder.setTitle(((TextView)arg0).getText().toString());
            builder.setItems(items, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    if (item == 1)
                        {  
                           FileManagerActivity.getFinalAttachFiles().remove(item);
                           layout.invalidate();
                        }
    
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
            return false;
        }
    };
    

    That solved my problem.