2012-04-26 94 views
0

我工作的Android应用程序是这样的:从XML文件链接的ListView按钮

(ListView的开始)

名称 - 游戏

日期

链接(按钮)


名2 - GAME2

日期2

链路2(按钮)

(ListView的端)

即在ListView。 所以一个ListView Elemnt是:姓名,游戏,日期(TextView的)和链路(按钮) 所有的数据都在一个XML文件

所以我的问题: ?我怎样才能在按钮的链接?像从XML文件中的link1到ListView Element1中的按钮等等。 已经查了一些教程,但不能让它与我的代码工作:

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.listplaceholder); 

     ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 


     String xml = XMLfunctions.getXML(); 
     Document doc = XMLfunctions.XMLfromString(xml); 

     int numResults = XMLfunctions.numResults(doc); 

     if((numResults <= 0)){ 
      Toast.makeText(Main.this, "...", Toast.LENGTH_LONG).show(); 
      finish(); 
     } 

     NodeList nodes = doc.getElementsByTagName("result"); 

     for (int i = 0; i < nodes.getLength(); i++) {        
      HashMap<String, String> map = new HashMap<String, String>();  

      Element e = (Element)nodes.item(i); 
      if(i==0){ 
       TextView update= (TextView) findViewById(R.id.textView6); 
       update.setText("last Update: "+XMLfunctions.getValue(e, "update")); 
      } else { 

      map.put("name", XMLfunctions.getValue(e, "name")+": " + XMLfunctions.getValue(e, "game")); 
      map.put("date", XMLfunctions.getValue(e, "date")); 
      mylist.add(map);  
      }  
     }   
     ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
         new String[] { "name", "date" }, 
         new int[] { R.id.item_title, R.id.item_date }); 

     setListAdapter(adapter); 

     final ListView lv = getListView();   
     lv.setTextFilterEnabled(true);  

编辑XML的

例子我解析:

<result> 
<id>1</id> 
<name>test</name> 
<game>gamename</game> 
<date>27.04</date> 
<link>www.google.com</link> 
</result> 
<result> 
<id>2</id> 
<name>test2</name> 
<game>gamename2</game> 
<date>27.04</date> 
<link>www.google.com</link> 
</result> 

和listplaceholder布局

<ListView 
    android:id="@id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="280dp" 
    android:drawSelectorOnTop="false" android:scrollbars="horizontal|vertical"/> 

和主布局(ListView的布局)

<TableLayout 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" > 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/item_title" 
       android:layout_width="146dp" 
       android:layout_height="wrap_content" 
       android:padding="2dp" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:textSize="10dp" /> 



     </TableRow> 
     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 
      <TextView 
       android:id="@+id/item_date" 
       android:layout_width="150dp" 
       android:layout_height="wrap_content" 
       android:padding="2dp" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:textSize="10dp" /> 
      </TableRow> 



     <TableRow 
      android:id="@+id/tableRow3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 


      <Button 
       android:id="@+id/buttonlink" 
       style="?android:attr/buttonStyleSmall" 
       android:layout_width="10dp" android:layout_height="fill_parent" 
       android:text="Link" 
       /> 





     </TableRow> 

    </TableLayout> 
+0

请张贴在那里你读出的数据和您的main.xml XML文件的格式。 – Sam 2012-04-26 18:57:51

+0

在第一篇文章中完成;) – user1359390 2012-04-26 20:44:25

回答

0

我只是增加了一个新的自己的适配器,它的工作原理,所以如果有人正在寻找它:

public class DataAdapter extends ArrayAdapter<Data> 
     { 

      private ArrayList<Daten> items; 
      String url; 

      public DatenAdapter(Context context, int textViewResourceId, ArrayList<Daten> items) { 
        super(context, textViewResourceId, items); 
        this.items = items; 
      } 
      @Override 
      public View getView(final int position, View convertView, ViewGroup parent) { 
        View v = convertView; 
        if (v == null) { 
         LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
         v = vi.inflate(R.layout.list_element, null); 
        } 
        Daten o = items.get(position); 

        url = o.getUrl(); 
        if (o != null) { 

          TextView name = (TextView) v.findViewById(R.id.item_title); 
          TextView datum = (TextView) v.findViewById(R.id.item_date); 
          if (name != null) { 
            name.setText(o.getName()+" - "+o.getSpiel());       } 
          if(datum != null){ 
            datum.setText("date: "+ o.getDate()); 

          } 
        } 
        Button but = (Button) v.findViewById(R.id.buttonlink); 

        but.setOnClickListener(new View.OnClickListener() { 
         public void onClick(View view) { 
         openWebURL(getUrl(position)); 
         } 
        }); 



        return v; 

      } 



      private String getUrl(int index) { 
       return d.get(index).getUrl(); 
      }