我在listview中定制了某些项目,但我想在textview中的所有项目中使用不同的textview

时间:2016-04-19 18:57:49

标签: android listview

This is my list view , you can see a same text view in front of all the items , this is what i want to change i want a different textview for every item in the list view.

这是我的活动,包含三个类Mainactivity1,singlerow2和shivvadapter

package com.example.shivnandan.fit;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity1 extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener


{

    ListView list;

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


 list = (ListView) findViewById(R.id.listView2);
list.setAdapter(new shivvAdapter(this));
list.setOnItemClickListener(new AdapterView.OnItemClickListener()


        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                if (position == 0)

                {

Toast.makeText(getApplicationContext(), "Gender  Selected ", Toast.LENGTH_SHORT).show();
                }


                if (position == 1)

                {

                   Toast.makeText(getApplicationContext(), "Age Selected ", Toast.LENGTH_SHORT).show();


                }

                if (position == 2)

                {

                    Toast.makeText(getApplicationContext(), "Height Selected", Toast.LENGTH_SHORT).show();

                }

                if (position == 3)

                {

                    Toast.makeText(getApplicationContext(), "Weight Selected", Toast.LENGTH_SHORT).show();

                }
                if (position == 4)

                {

                    Toast.makeText(getApplicationContext(), "Reset Selected", Toast.LENGTH_SHORT).show();

                }

            }
        });
    }
class SingleRow2
{
    String title;

    int  image;
    SingleRow2(String title )
    {

        this.title =title;
    }

}
class shivvAdapter extends BaseAdapter
{
    ArrayList<SingleRow> list;
    Context context;
    shivvAdapter(Context c)
    {
        context = c;
        list=new ArrayList<SingleRow>();
        Resources res = c.getResources();
        String[] titles=res.getStringArray(R.array.profiletitle);
        // String[] descriptions=res.getStringArray(R.array.description);
        int[] images = {R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.two,R.drawable.two,R.drawable.two};
        for (int i =0 ; i<5 ; i++)
        {


            list.add(new SingleRow(titles[i],images[i]));
        }



    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup)
    {

        LayoutInflater inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View  row =  inflater.inflate(R.layout.single_row2,viewGroup,false);
        TextView title= (TextView) row.findViewById(R.id.textView);
        SingleRow temp = list.get(i);
        title.setText(temp.title);
        return row;
    }

}

这是single_row2.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:paddingTop="30dp"

    android:layout_width="match_parent"
    android:layout_height="82dp"
    android:id="@+id/single_row"
    android:background="@drawable/side_nav_bar"
    android:foregroundTint="#0a0a0a">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Main Activity"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="-6dp" />

    <TextView
        android:layout_width="50dp"
        android:layout_height="40dp"
        android:id="@+id/hometext"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:hint="Gender" />


</RelativeLayout>

and this is how single_row2 looks like

1 个答案:

答案 0 :(得分:0)

您可以创建一个List并将其发送给shivvAdapter构造。 例如:

List<String> stringList = new ArrayList<>();
stringList.add("my text 1");
stringList.add("my text 2");
stringList.add("my text 3");
list.setAdapter(new shivvAdapter(this, stringList));

所以在shivvAdapter中保存此List并在getView()方法中使用它 例如:

public View getView(int position, View convertView, ViewGroup parent) {
    TextView title = (TextView) convertView.findViewById(R.id.title);
    title.setText(myStringList.get(position));
    return convertView;
}