2012-04-01 8 views
1

Ich habe eine Listenansicht implementiert, aber es funktioniert gut, aber wenn ich auf die Liste klicke, erhält ich Rohdaten, die mir getItemAtPosition (position) zurückgeben.Android-Listenansicht getItemAtPosition() gibt Rohdaten zurück

folgenden ist der Code, i

public class FirstTab extends Activity{ 

    private TabHostProvider tabProvider; 
    private TabView tabView; 
    private static String[] country1; 
    private ListView speakerList; 
    final Handler mHandler = new Handler(); 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Context context = getApplicationContext(); 
     tabProvider = new TabbarView(this); 
     tabView = tabProvider.getTabHost("main"); 
     tabView.setCurrentView(R.layout.firsttab); 
     setContentView(tabView.render(0)); 
     String temp[] = new String[]{"furqan","furqan1","furqan2"}; 
     Weather weather_data[] = new Weather[temp.length]; 
     for(int i = 0;i<temp.length;i++) 
     { 
      weather_data[i] = new Weather(R.drawable.ic_launcher, temp[i]); 
     } 

     WeatherAdapter adapter = new WeatherAdapter(this,R.layout.listview_item_row, weather_data); 
     speakerList = (ListView)findViewById(R.id.speakerList); 
     View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null); 
     speakerList.addHeaderView(header); 
     speakerList.setAdapter(adapter); 

     final ViewFlipper viewflipper = (ViewFlipper) findViewById(R.id.viewflipper); 
     speakerList.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(final AdapterView<?> arg0, View arg1, final int arg2,long arg3) { 
       // TODO Auto-generated method stub 


       String item = speakerList.getItemAtPosition(arg2).toString(); 
       Log.d("the value is", item); 
         /*View item = (View) (speakerList.getItemAtPosition(arg2)); 
         String item = arg0.getItemAtPosition(arg2).toString(); 
         Log.d("the value is", item);*/ 
         //Toast.makeText(getApplicationContext(), arg2, 1).show(); 



      } 

     }); 

    } 
} 

Die oben bin mit dem Haupt activity.Following ist der Adapter

public class weatheradapter { 



    public static 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; 
     } 

     static class WeatherHolder 
     { 
      ImageView imgIcon; 
      TextView txtTitle; 
     } 



     @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.imgIcon = (ImageView)row.findViewById(R.id.imgIcon); 
       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); 
      holder.imgIcon.setImageResource(weather.icon); 

      return row; 
     } 


    } 
} 

dies die XML-Datei

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center" android:gravity="center" android:background="#00FFcc"> 

    <ViewFlipper android:id="@+id/viewflipper" android:layout_width="fill_parent" android:layout_height="fill_parent" > 

     <ListView android:id="@+id/speakerList" android:layout_width="fill_parent" android:layout_height="fill_parent" /> 

     <ListView android:id="@+id/categoryList" android:layout_width="fill_parent" android:layout_height="wrap_content" /> 


    </ViewFlipper> 

</LinearLayout> 

Das ist Das folgende ist das Layout für die Listenansicht

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

    <ImageView android:id="@+id/imgIcon" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:gravity="center_vertical" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentBottom="true" 
     android:layout_marginRight="15dp" 
     android:layout_marginTop="5dp" 
     android:layout_marginBottom="5dp" /> 



     <TextView android:id="@+id/txtTitle" 
     android:text="@+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> 

Hilfe benötigen dank

+0

http://stackoverflow.com/a/29072140/3717188 –

Antwort

6
public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
    Cursor c = (Cursor) parent.getAdapter().getItem(position); 
    c.getString(c.getColumnIndex("col_name"))); 
} 
+0

i noch eine Frage haben, wie Sie bin ich mit Flipper mit in ich habe sehen zwei Listenansicht erklärt, was ich Möchte fragen ist auf Listenansicht klicken Ich muss die nächste Listenansicht mit Flipper anzeigen Was ich tun kann Ich in einem solchen Szenario verwende ich den folgenden Code, aber es funktioniert nicht –

+0

add viewflipper.showNext(); am unteren Rand Ihres onClickListener –