gpt4 book ai didi

android - 将 ListView 行最小化为一行

转载 作者:行者123 更新时间:2023-11-30 03:25:03 26 4
gpt4 key购买 nike

我正在使用 ListView 显示来自数据库的消息..当我添加一条消息时获取所有字符串并显示在 ListView 上..这是我的 xml 文件和 java..我需要使用“...”在每行的单行中获取消息。我研究了这个问题,我发现,输入 android:singleLine="true"在 TextView 中,但我不知道它们在 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();
}
}

最佳答案

您正在为 ListView 项目使用默认布局 android.R.layout.simple_list_item_1。而是使用启用单行的 TextView 创建一个布局。并将其传递给 ListView 。并为 ListView 使用自定义数组适配器。

这样做:创建列表项 View XMLlistview_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;
}
}

并在您的 Activity 中这样使用:

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);

希望对您有所帮助!!!

关于android - 将 ListView 行最小化为一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18371722/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com