How to get the value for each seek bar created in list view Android?

十年热恋 提交于 2019-12-25 05:26:27

问题


may I know how to get the specific value for each SeekBar which I have created in custom ListView? I am able to create the static one but what if i want to retrieve the value for each SeekBar in the custom ListView based on my sliding? Thank you.

--Activity code--

public class DisplayAssessmentActivity extends ActionBarActivity {

ListDatabaseHandler listCriteria;
private SimpleCursorAdapter dataAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_assessment);

    //Open connection and get instance from database
    listCriteria = new ListDatabaseHandler(this);
    listCriteria.open();

    //Delete all data
    listCriteria.deleteAllCriteria();

    //Add data
    listCriteria.insertSomeCriteria();

    //Retrieve data from database and display in list view
    displayCriteriaListView();
}

private void displayCriteriaListView() {

    Cursor cursor = listCriteria.fetchAllCriteria();

    // The desired columns to be bound
    String[] columns = new String[] {
            ListDatabaseHandler.KEY_CRITERIANAME,
            ListDatabaseHandler.KEY_CRITERIAWEIGHTAGE,

    };

    // the XML defined views which the data will be bound to
    int[] to = new int[] {
            R.id.criteriaName,
            R.id.criteriaWeightage
    };

    // create the adapter using the cursor pointing to the desired data
    //as well as the layout information
    dataAdapter = new SimpleCursorAdapter(
            this, R.layout.activity_list_criteria,
            cursor,
            columns,
            to,
            0);

    ListView listView = (ListView) findViewById(R.id.criteriaListView);
    // Assign adapter to ListView
    listView.setAdapter(dataAdapter);

    //Action when click on the item
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> listView, View view,
                                int position, long id) {
            // Get the cursor, positioned to the corresponding row in the result set
            Cursor cursor = (Cursor) listView.getItemAtPosition(position);

            //Get matric number from this row in the database
            String criteria = cursor.getString(cursor.getColumnIndexOrThrow("criteria_name"));

            // Get the state's capital from this row in the database.
            String weightage =
                    cursor.getString(cursor.getColumnIndexOrThrow("criteria_weightage"));

            //Toast.makeText(getApplicationContext(),
            //subjectName, Toast.LENGTH_SHORT).show();


            Intent intentResult = new Intent(getApplicationContext(), ResultActivity.class);

            startActivity(intentResult);

        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_display_assessment, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

activity_list_criteria.xml

enter code here

    //Label criteria
    <TextView
        android:id="@+id/txtViewCriteria"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Criteria      : " />

    //Label criteria from db
    <TextView
        android:id="@+id/criteriaName"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:text="Criteria" />

    <TextView
        android:layout_width="180dp"
        android:layout_height="fill_parent" />


</LinearLayout>

//SeekBar layout
<SeekBar
    android:id="@+id/sbBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:progress="0"
    android:max="5"
    android:paddingTop="20dp" />

//TextViews for SeekBarlegend
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tvLabel1"
        android:paddingLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0" />

    <TextView
        android:id="@+id/tvLabel3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:paddingRight="10dp"
        android:text="5" />

</RelativeLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="35dp">

    //Label weightage
    <TextView
        android:id="@+id/txtViewWeightage"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Weightage: "/>

    //Label weightage from db
    <TextView
        android:id="@+id/criteriaWeightage"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/stuMatric"
        android:layout_toRightOf="@+id/textView2"
        android:text="Weightage" />

</LinearLayout>

activity_display_assessment.xml

//Title Layout
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:background="@drawable/orangehome"
        android:orientation="horizontal">

        <TextView
            android:layout_width="110dp"
            android:layout_height="fill_parent" />

        <TextView
            android:layout_width="170dp"
            android:text="Evaluate"
            android:layout_gravity="center_horizontal"
            android:gravity="center"
            android:textColor="#FFFFFF"
            android:textSize="30sp"
            android:layout_height="60dp" />

        <TextView
            android:layout_width="50dp"
            android:layout_height="fill_parent" />

        <Button
            android:id="@+id/btnLogout"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="center_vertical"
            android:background="@drawable/logout"/>

    </LinearLayout>

</LinearLayout>

//Criteria layout
<TextView
    android:id="@+id/txtViewSubject"
    android:paddingTop="10dp"
    android:layout_gravity="center_horizontal"
    android:gravity="center"
    android:layout_width="300dp"
    android:layout_height="60dp"
    android:textSize="18sp"
    android:textColor="#000000"
    android:text="Criteria"/>

//List view for criteria
<ListView
    android:id="@+id/criteriaListView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />


回答1:


So far, the method below solve my question.

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final ArrayList<String> values;

    public MySimpleArrayAdapter(Context context, ArrayList<String> values) {
        super(context, R.layout.rowlayout, values);

        this.context = context;
        this.values = values;
    }

    /**
     * Here we go and get our rowlayout.xml file and set the textview text.
     * This happens for every row in your listview.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.rowlayout, parent, false);

        TextView textView = (TextView) rowView.findViewById(R.id.criteriaName);
        final TextView textView2 = (TextView)rowView.findViewById(R.id.txtView2);
        SeekBar seekBar = (SeekBar)rowView.findViewById(R.id.sbBar);

        // Setting the text to display
        textView.setText(values.get(position));

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                textView2.setText(""+i);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        return rowView;
    }
}



回答2:


Try this:

final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
final int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

I hope it helps.




回答3:


Simple and elegant:

yourListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
     int position, long id) {
     seekbar = (SeekBar)yourListView.getItemAtPosition(position);
     seekbar.setFocusable(true);
     seekbar.setEnabled(true);
     int value = seekbar.getProgress(); // get value here


      }
});

Try this and let me know if it works or not.

This is not Tested, so if you only enter SeekBars into your ListView only then this will work.



来源:https://stackoverflow.com/questions/30398561/how-to-get-the-value-for-each-seek-bar-created-in-list-view-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!