将列表视图行最小化为一行

时间:2013-08-22 04:30:09

标签: android android-listview

我正在使用listview来显示来自数据库的消息..当我添加消息时 获取所有字符串并在listview上显示..还有我的xml文件和java .. 我需要用'......'在每行的单行中获取消息。我研究了这个问题,我找到了,输入      android:singleLine =“true”在textview中,但我不知道他们的意思'在textview'.becauz我正在使用listview.please帮助。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/wave" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/SearchMessageExit"
        android:focusableInTouchMode="true"
         >

    </ListView>

</LinearLayout>

message.java

    public void detailsOfMessage(){
        try{
            Database_message info = new Database_message(this);
            String data = info.getData();
            info.close();

            if(data.equals(""))
            {
                Toast.makeText(getApplicationContext(), "Empty Message", Toast.LENGTH_LONG).show();
            }


            StringTokenizer token = new StringTokenizer(data,"\t");
            int rows = token.countTokens();

            classes = new String[rows];

            int i=0;

            while (token.hasMoreTokens())
            {
                classes[i]=token.nextToken();
                i++;
            }


        listView = (ListView) findViewById(R.id.list);
        listView.setOnItemClickListener(this);
        listView.setOnLongClickListener(this);

        inAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,0);
        listView.setAdapter(inAdapter);


        for (int r = 0; r < classes.length; r++) {
            inAdapter.add(classes[r]);
        }
        }catch(Exception e){

            Toast.makeText(getApplicationContext(), "Empty Message", Toast.LENGTH_LONG).show();
        }
    }   

2 个答案:

答案 0 :(得分:1)

您使用默认布局android.R.layout.simple_list_item_1作为列表视图项。而是使用带有单行启用的textview创建一个布局。并将其传递给listview。并为listview使用自定义数组适配器。

这样做: 创建列表itemview XML 的 listview_item_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp">

     <TextView android:id="@+id/txtTitle"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:textStyle="bold"
        android:textSize="22dp"
        android:textColor="#000000"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

</LinearLayout>

一类如

<强> Weather.java

public class Weather {

    public String title;
    public Weather(){
        super();
    }

    public Weather(String title) {
        super();

        this.title = title;
    }
}

然后创建数组适配器类

public class WeatherAdapter extends ArrayAdapter<Weather>{

    Context context; 
    int layoutResourceId;    
    Weather data[] = null;

    public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        WeatherHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new WeatherHolder();
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

            row.setTag(holder);
        }
        else
        {
            holder = (WeatherHolder)row.getTag();
        }

        Weather weather = data[position];
        holder.txtTitle.setText(weather.title);

        return row;
    }

    static class WeatherHolder
    {
        TextView txtTitle;
    }
}

在您的活动中使用这样的内容:

Weather weather_data[] = new Weather[]
        {
            new Weather("Cloudy"),
            new Weather("Showers"),
            new Weather("Snow"),
            new Weather("Storm"),
            new Weather("Sunny")
        };

        WeatherAdapter adapter = new WeatherAdapter(this, 
                R.layout.listview_item_row, weather_data);

        listView1.setAdapter(adapter);

希望它能帮助!!!

答案 1 :(得分:0)

您需要为ListView实现自定义适配器 除此之外,您还应该为列表视图的每一行实现custom_row.xml文件。

例如:
1.自定义适配器:

public class CustomAdapterListView extends BaseAdapter
{
private Activity activity;
private static LayoutInflater inflater=null;
private YOUR_DATA_TYPE data;

public CategoryDetailsCustomGridviewAdapter(Activity a, ArrayList<YOUR_DATA_TYPE> d)
{
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() 
{
    // TODO Auto-generated method stub
    return data.size();
}

@Override
public Object getItem(int position) 
{
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) 
{
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    // TODO Auto-generated method stub
    View vi=convertView;
    if(convertView==null)
    {
    vi = inflater.inflate(R.layout.YOUR_LISTVIEW_ROW_XML_FILE, null);
    }

    //Fetching the Data from ArrayList for each row.
                    TextView custom_row_tv = vi.findViewById(R.id.YOUR_TEXT_VIEW_ID)
    YOUR_DATA_TYPE dataToBePopulated = new YOUR_DATA_TYPE;
                    custom_row_tv.setText(YOUR_DATA_TYPE.ToString());
    dataToBePopulated = data.get(position);     
    return vi;
}
}


2。 YOUR_LISTVIEW_ROW_XML_FILE.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:id="@+id/YOUR_TEXT_VIEW_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true" >
    </TextView>   
</RelativeLayout>


3。设置自定义适配器:

Public Class YourClass extends Activity
{ 
 onCreate(....)
 {
  setContentView(R.Layout.YOUR_LAYOUT_FILE);
  ArrayList<YOUR_DATA_TYPE> data = new ArrayList<YOUR_DATA_TYPE>();
  data.add(...) //Fetch your Data from Data source into this ArrayList.
  ListView yourListView = (ListView)findViewById(R,id.YOUR_LISTVIEW_ID);
  CustomAdapterListView adapter = new CustomAdapterListView (YourClass.this, data);
  yourListView.setAdapter(adapter);
  adapter.notifyDataSetChanged();
 //YOUR OTHER FUNCTIONALITY......
 }

//YOUR OTHER METHODS.....
....
....
}


通过这种方式,您可以简单地实现自定义ListView,其中单线属性附加到ListView中嵌入的TextView。
我希望这能解决你的问题。