1

我正在使用 listview 来显示来自数据库的消息..当我添加一条消息时,它需要所有字符串并显示在 listview 上..这是我的 xml 文件和 java..我需要在单行中获取消息带有'...'的行。我研究了这个问题,我发现,在 textview 中键入 android:singleLine="true",但我不知道它们在 textview 中是什么意思。因为我正在使用 listview。请帮助。

<?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>

消息.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();
        }
    }   
4

2 回答 2

1

您正在使用默认布局 android.R.layout.simple_list_item_1 作为列表视图项。而是使用启用单行的 textview 创建一个布局。并将其传递给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>

一类如

天气.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);

希望能帮助到你!!!

于 2013-08-22T04:42:22.267 回答
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。
我希望这能解决你的问题。

于 2013-08-22T05:10:38.310 回答