2013-08-22 30 views
1

我使用列表視圖來顯示從database..when我添加了消息時 通吃的字符串,並顯示在listview..here消息是我的xml文件和java .. 我需要以'...'的每行以單線形式獲取消息。我研究了這個問題,我發現,類型 機器人:單線=「真」中的TextView,但我不知道他們是什麼意思「在textview'.becauz我使用listview.please幫助。最小化的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> 

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

回答

1

您使用的默認佈局android.R.layout.simple_list_item_1的ListView項。用單行啓用創建一個帶有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

謝謝。當我嘗試運行這段代碼時,我得到一個異常'arrayadapter需要資源ID爲textview'。然後我刪除了linearlayout,並創建了只有textview的佈局。現在它完美地工作... –

+0

多數民衆贊成在很大程度上..如果答案已經幫助你,那麼你可以接受它作爲答案,並upvote它,使其他人也可以發現它有用。 –

+1

但是,想到的是我對這個網站新..所以我需要15聲望voteup.sorry。 –

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..... 
.... 
.... 
} 


這樣你就可以簡單地實現與連接到您的TextView嵌入你的ListView內單行的屬性自定義的ListView。
我希望這可以解決您的問題。

+1

thanks.and沒有相對佈局,只是我輸入textview xml。它完美的作品。 –

+0

@Vinoch我的榮幸。 –